[Tkinter-discuss] Labels not updating its display data

Michael Lange klappnase at web.de
Thu Apr 1 12:27:12 CEST 2010

Hi Guido,

On Thu, 01 Apr 2010 00:02:36 -0600
Guido Carballo-Guerrero <charras at me.com> wrote:

> Is it possible to display and update data using labels in a window,
> created by another window?
> This is a program I did that is not working as expected:
> def newcount():
>     root2 = Tkt.Tk()
>     var2 = Tkt.IntVar()
>     frameLabels2 = Tkt.Frame(root2)
>     frameLabels2.pack()
>     def changeVal():
>         var2.set(var2.get()+1)
>         print 'var2: ',var2.get()
>     Tkt.Label(frameLabels2,text='var2: ').pack(side=Tkt.LEFT)
>     Tkt.Label(frameLabels2,textvariable=var1).pack(side=Tkt.LEFT)
>     Tkt.Button(root2,text="Change value", command=changeVal).pack()
>     root2.mainloop()

There are two problems in this function, first you should _never_ use
two instances of Tk() in one program; if you need a second window, use
a Tkinter.Toplevel() instead.
Second, you assign var1 as textvariable for the Label; I think what you
actually intended is to set the current value of var1 as initial value
for var2 and then use var2 as textvariable for the label ? If yes,
simply change the first lines of newcount() into:

def newcount():

    root2 = Tkt.Toplevel()
    var2 = Tkt.IntVar()

I hope this helps


More information about the Tkinter-discuss mailing list