Tkinter: how to refresh a canvas without duplicates?

S

Stewart Midwinter

I've got a Tkinter app that draws three histograms. At this point I am
simulating real data by drawing rectangles whose size is defined by random
numbers; in the future there would be real data coming from a server.

I want to redraw the rectangles every time I press the Again button. This works
for me, but I get an extra set of rectangles each time I press the Again button.
How to get rid of this effect?

Here's the app so you can see the behaviour:

Thanks
S

---
title = 'Histogram summary'

# Import Pmw from this directory tree.
import sys
sys.path[:0] = ['../../..']

from Tkinter import *
import Pmw
import random

global canwidth, canheight, sectors, barcolors
sectors = 'SECT_1','SECT_2','SECT_3'
barcolors = 'green','yellow','red','violet'
canwidth = 200
canheight = 20


class Summary:
def __init__(self, parent):
# Create the dialog.
self.dialog = Pmw.Dialog(parent,
buttons = ('OK', 'Apply', 'Cancel', 'Help'),
defaultbutton = 'OK',
title = 'My dialog',
command = self.execute)
self.dialog.withdraw()

# Add some contents to the dialog.
w = Label(self.dialog.interior(),
text = 'Sector Summary',
background = 'black',
foreground = 'white',
pady = 20)
w.pack(expand = 1, fill = 'both', padx = 2, pady = 2)


def drawRec(self):
global row,can
for sector in sectors:
row = Frame(self.dialog.interior())
lab = Label(row, width=10,text=sector)
can = Canvas(row,
width=canwidth,
height=canheight
)

h=canheight
m=canwidth-20 # alarm level (100%) is less than available space
for rectangle d=canwidth/2
p=random.randrange(-1,2,2) # create random + or - imbalance
f=random.randrange(-110,110,1)/100.0 # create random fractional
height of bar w=f*m
startx=d+f*d
starty=0
endx=d
endy=h
if (f<-1.0):
rcolor='violet'
elif (-1.0<=f<0.85):
rcolor='green'
elif (0.85<f<=1.0):
rcolor='yellow'
elif (f>1.0):
rcolor='red'
else:
rcolor='grey'

can.create_rectangle(startx,starty,endx,endy,width=1,fill=rcolor)
row.pack(side=TOP, fill=X) lab.pack(side=LEFT)
can.pack(side=LEFT, expand=0, padx = 1, pady = 1) #grow
horizontal self.dialog.activate(globalMode = 'nograb')

def redrawRec(self):
self.dialog.deactivate()
for sector in sectors:
row.destroy()
self.drawRec()
self.dialog.activate()

def execute(self, result):
print 'You clicked on', result
if result not in ('Apply', 'Help'):
self.dialog.deactivate(result)
if (result =='Apply'):
self.redrawRec()

######################################################################

# Create demo in root window for testing.
if __name__ == '__main__':
root = Tk()
#Pmw.initialise(root)
#root.title(title)

widget = Summary(root)
goButton = Button(root, text = 'Again', command = lambda: widget.drawRec())
goButton.pack(side = 'bottom')
exitButton = Button(root, text = 'Exit', command = root.destroy)
exitButton.pack(side = 'bottom')
widget.drawRec()
root.mainloop()
---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top