Tk : linking canvases to DoubleVar

Dave Dench (dave@zeus.hud.ac.uk)
Fri, 13 Jan 95 13:48:19 GMT

I am trying to learn tkinter and have created a little
application that manages an animal feed bin - something
that I had previously done using SUIT for the interface.
I have accomplished the exercise but have used a
somewhat inelegant way of updating the bin 'contents'
widget.
What I wanted to do was 'shadow' the display via a DoubleVar()
variable to achieve automatic update when I changed the variable.
What I actually did , follows.
Could anyone point out alternatives to actually calling the 'display'
function as I have done?
Many thanks,
David Dench

class BinChip:

def __init__ (Self,name,capacity,master=None):
Self.capacity = capacity
Self.name = name
Self.percentfull = DoubleVar()
Self.percentfull.set(0.0)

Self.frame = Frame(master, { 'name': name ,
'relief' : 'groove',
'borderwidth' : '1m',
Pack: {'expand': 1, 'fill': 'both'}})
Self.identity = Label (Self.frame,{"text" : name })
Self.identity.pack({'side' : 'top',
'expand': 1, 'fill': 'both'})
Self.canvas = Canvas(Self.frame, { 'name': name ,
'relief' : 'ridge',
Pack: {'expand': 1, 'fill': 'both'},
'bg' : 'white' })
Self.canvas.bind('<Button-1>',Self.errorBinChip)
Self.blacked = Canvas(Self.canvas, { 'name': name ,
# wanted something like this
#'variable' : 'Self.percentfull', Pack: {'expand': 1, 'fill': 'x'},
'bg' : 'black' })
Self.display_howfull()

def display_howfull(Self):
Self.blacked.place( {'relx' : '0.0',
'rely' : `1.0 - Self.percentfull.get()`,
'anchor' : 'nw'})

def resetBinChip (Self):
Self.percentfull.set(0.0)
Self.display_howfull() # don't really want to do this explicitly

def refreshBinChip (Self):
Self.display_howfull()

def fillBinChip (Self, percent):
Self.percentfull.set(percent)
Self.display_howfull()

def incrBinChip (Self, amount):
Self.percentfull.set( Self.percentfull.get() + (amount/Self.capacity))
Self.display_howfull()

def decrBinChip (Self, amount):
Self.percentfull.set( Self.percentfull.get() - (amount/Self.capacity))
Self.display_howfull()

def errorBinChip (Self,event):
inform = ErrorDialog(Self.canvas,' This is not an active object')
inform.Show()
inform.DialogCleanup()
del inform

Ps. as another question when should(?) you use del