我无法在python的tkinter中更新标签文本

问题描述 投票:0回答:1

我正在尝试循环更新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

python tkinter label
1个回答
0
投票
更正后的代码将类似于:

text

© www.soinside.com 2019 - 2024. All rights reserved.