<div>I tried to update the rectangle on a canvas to get the visual effect of progressbar.</div>
<div>It works all right if I delete the existing objects (rectangle and text) and create a new one.</div>
<div>However, if I invoke canvas.itemconfig() to update the existing objects' options, gui just went nuts.</div>
<div>I am more than happy to receive any help and advise.</div>
<div> </div>
<div>My code is provided as follows:</div>
<div> </div>
<div>------------------------------------Start of Code------------------------------------</div>
<div>import Tkinter<br>import threading<br>import time</div>
<p>class Progressbar(Tkinter.Frame):<br> def __init__(self, parent=None, color='white', width=200, height=15):<br> Tkinter.Frame.__init__(self, parent)<br> self.pack(expand=Tkinter.YES,<br> fill=Tkinter.BOTH)<br>
canv = Tkinter.Canvas(self, bg=color, relief=Tkinter.SUNKEN)<br> canv.config(width=width, height=height)<br> canv.pack(side=Tkinter.LEFT, expand=Tkinter.YES, fill=Tkinter.BOTH)<br> self.canv = canv<br>
self.width = width<br> self.height = height<br> progress = -10<br> rect = canv.create_rectangle(0, 0, 0, height,<br> fill='beige')<br> text = canv.create_text(width/2, height*2/3,<br>
text='0%')<br> self.rect = rect<br> self.text = text<br> self.progress = progress<br> self.parent = parent<br> parent.progressbar = self<br> self.UpdateProgress()</p>
<p> def SetProgress(self, progress=0):<br> canv = self.canv<br> width = self.width<br> height = self.height<br> rect = self.rect<br> text = self.text<br>## canv.delete(rect)<br>
## canv.delete(text)<br>## rect = canv.create_rectangle(0, 0, progress*width/100, height,<br>## fill='beige')<br>## text = canv.create_text(width/2, height*2/3,<br>
## text='%s' %(str(progress)) + '%')<br> canv.itemconfig(rect, width=width*progress/100) ## comment this<br> canv.itemconfig(text, text='%s' %(str(progress)) + '%') ## comment this<br>
## self.rect = rect<br>## self.text = text</p>
<p> def UpdateProgress(self):<br> progress = self.progress<br> progress = progress + 10<br> self.progress = progress<br> self.SetProgress(progress)<br> if progress < 100:<br> self.after(500, self.UpdateProgress)<br>
</p>
<p>if __name__ == '__main__':<br> root = Tkinter.Tk()<br> Progressbar(parent=root)<br> root.mainloop()<br></p>
<div>------------------------------------End of Code------------------------------------</div>
<div>uncomment the lines and comment out the itemconfig() lines to see the difference in gui's behaviours.</div>
<div> </div>
<div>Thanks,</div>
<div><br>-- <br>This is a UTF-8 formatted mail<br>-----------------------------------------------<br>James C.-C.Yu</div>