我正在尝试循环更新tkinter标签文本。标签文本不会使用i
的值更新。以下是我的代码及其输出,有人可以帮助我为什么它不起作用吗?预先感谢!
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()
在回调中哟n的5值0文字值01个文字1的值2文字2的值3文字值34文字值4在回调中4在回调中4在回调中4在回调中4在回调中4
text