The stange behaviour of Tkinter.Canvas

James Yu cyu021 at gmail.com
Tue Mar 11 07:04:06 EDT 2008


I tried to update the rectangle on a canvas to get the visual effect of
progressbar.
It works all right if I delete the existing objects (rectangle and text) and
create a new one.
However, if I invoke canvas.itemconfig() to update the existing objects'
options, gui just went nuts.
I am more than happy to receive any help and advise.

My code is provided as follows:

------------------------------------Start of
Code------------------------------------
import Tkinter
import threading
import time

class Progressbar(Tkinter.Frame):
    def __init__(self, parent=None, color='white', width=200, height=15):
        Tkinter.Frame.__init__(self, parent)
        self.pack(expand=Tkinter.YES,
                  fill=Tkinter.BOTH)
        canv = Tkinter.Canvas(self, bg=color, relief=Tkinter.SUNKEN)
        canv.config(width=width, height=height)
        canv.pack(side=Tkinter.LEFT, expand=Tkinter.YES, fill=Tkinter.BOTH)
        self.canv = canv
        self.width = width
        self.height = height
        progress = -10
        rect = canv.create_rectangle(0, 0, 0, height,
                                     fill='beige')
        text = canv.create_text(width/2, height*2/3,
                                text='0%')
        self.rect = rect
        self.text = text
        self.progress = progress
        self.parent = parent
        parent.progressbar = self
        self.UpdateProgress()

    def SetProgress(self, progress=0):
        canv = self.canv
        width = self.width
        height = self.height
        rect = self.rect
        text = self.text
##        canv.delete(rect)
##        canv.delete(text)
##        rect = canv.create_rectangle(0, 0, progress*width/100, height,
##                                     fill='beige')
##        text = canv.create_text(width/2, height*2/3,
##                                text='%s' %(str(progress)) + '%')
        canv.itemconfig(rect, width=width*progress/100)              ##
comment this
        canv.itemconfig(text, text='%s' %(str(progress)) + '%')      ##
comment this
##        self.rect = rect
##        self.text = text

    def UpdateProgress(self):
        progress = self.progress
        progress = progress + 10
        self.progress = progress
        self.SetProgress(progress)
        if progress < 100:
            self.after(500, self.UpdateProgress)


if __name__ == '__main__':
    root = Tkinter.Tk()
    Progressbar(parent=root)
    root.mainloop()
------------------------------------End of
Code------------------------------------
uncomment the lines and comment out the itemconfig() lines to see the
difference in gui's behaviours.

Thanks,

-- 
This is a UTF-8 formatted mail
-----------------------------------------------
James C.-C.Yu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080311/dc70ce73/attachment.html>


More information about the Python-list mailing list