Osama
Osama

Reputation: 5

Tkinter label text isn't being updated

import tkinter

win = tkinter.Tk()

c=0

def click():
   global c

   c+=1

b=tkinter.Button(win, text='click', command=click).pack()

l=tkinter.Label(win, text=c).pack()

win.mainloop()

Upvotes: 0

Views: 32

Answers (1)

Live Forever
Live Forever

Reputation: 178

The main problem is that your click function just changes the value of the c variable. After this you should send the new c value to your l label. Also, try to avoid using objects initializing together with pack, place etc methods because you lose the reference to your objects. In this case Label type will not be passed to your variable and l will be NoneType.

import tkinter

win = tkinter.Tk()

c=0

def click():
   global c

   c+=1
   l.configure(text=c)

b=tkinter.Button(win, text='click', command=click).pack()

l=tkinter.Label(win, text=c)
l.pack()

win.mainloop()

Upvotes: 3

Related Questions