简体   繁体   中英

i am not able to update the label text in tkinter in python

I am trying to update the tkinter label text in a loop. The text of the label is not updated with the value if i . Below is my code and its output, can anyone help me why is it not working? Thanks in advance!

import tkinter as tk
import time
from tkinter import messagebox
class Test():
   def __init__(self):
       self.root = tk.Tk()
       self.v = tk.StringVar()
       self.text='yo'
       self.v.set(self.text)
       self.label = tk.Label(self.root, text=self.v.get())
       self.root.after(500,self.callback)
       self.button = tk.Button(self.root,text="RUN Timer",command=lambda:self.runtimer(5))
       self.button.pack()
       self.label.pack()
       self.root.mainloop()
   def runtimer(self,n):
       messagebox.showinfo("information","Information")  
       print(n,' value of n')
       for i in range(0,int(n)):
               print(i)
               self.text=i
               print('value of text',self.text)
               time.sleep(1)
               self.root.after(500,self.callback)
   def callback(self):
       print('in callback')
       print(self.text)
       self.v.set(self.text) 
app=Test()

in callback
yo
5 value of n
0
value of text 0
1
value of text 1
2
value of text 2
3
value of text 3
4
value of text 4
in callback
4
in callback
4
in callback
4
in callback
4
in callback
4

The issue is when you declare the Label() tag. StringVar() creates an object that can manipulate the text. The set() and get() method are only used for setting and retrieval of the value. when you call self.v.get() , You're getting the value of the StringVar. It's equivalent to setting a static string. To fix this pass self.v to the Label as textvariable instead of setting self.v.get() to text .

The corrected code would look something like:

import tkinter as tk
import time
from tkinter import messagebox
class Test():
   def __init__(self):
       self.root = tk.Tk()
       self.v = tk.StringVar()
       self.text = 'yo'
       self.v.set(self.text)
       self.label = tk.Label(self.root, textvariable=self.v)
       self.root.after(500, self.callback)
       self.button = tk.Button(self.root, text="RUN Timer", command=lambda:self.runtimer(5))
       self.button.pack()
       self.label.pack()
       self.root.mainloop()

   def runtimer(self, n):
       messagebox.showinfo("information","Information")  
       print(n, 'value of n')
       for i in range(int(n)):
           print(i)
           self.text = i
           print('value of text', self.text)
           self.callback()
           time.sleep(1)

   def callback(self):
       print('in callback')
       print(self.text)
       self.v.set(self.text)
       self.root.update()

app=Test()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM