我有一个用状态信息更新的标签。如果状态不变,我想在10秒后清除它的内容。例如,我将标签设置为“第一步”,然后在10秒后清除“”。现在,如果在10秒到期之前,我将标签设置为“第二步”,它将更新并重置10秒计时器
编辑: 好吧,我永远都不能让它正常工作。我制作了一个小应用程序来测试,它有3个按钮,其中两个按钮只是改变标签的文本。我想要发生的是以下几点
import tkinter as tk
class Window(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.master = master
self.myafter = None
# widget can take all window
self.pack(fill= tk.BOTH, expand=1)
# create button, link it to clickExitButton()
startButton = tk.Button(self, text="MSG_1", command=self.clickButton1 )
startButton.place(x=10,y=10,width=50,height=50)
stopButton = tk.Button(self, text="MSG_2", command=self.clickButton2)
stopButton.place(x=100,y=10,width=50,height=50)
exitButton = tk.Button(self, text="EXIT", command=self.clickExitButton)
exitButton.place(x=190,y=10,width=50,height=50)
lblstatus = tk.Label(self, text = "Status:")
lblstatus.place(x=10, y=150)
self.lblStatus = tk.Label(self, text = "READY")
self.lblStatus.place(x=75,y=150)
def clickButton1(self):
if(self.myafter != None):
self.lblStatus.after_cancel(self.myafter)
app.UpdateMessage("Set message 1")
def clickButton2(self):
if(self.myafter != None):
self.lblStatus.after_cancel(self.myafter)
app.UpdateMessage("Set message 2")
def ClearStatus(self):
self.lblStatus["text"] = ""
def UpdateMessage(self,msg= "test"):
self.lblStatus["text"] = msg
self.lblStatus.update()
if(self.myafter == None):
self.myafter = self.lblStatus.after(2000,self.ClearStatus())
def clickExitButton(self):
root.quit()
root = tk.Tk()
app = Window(root)
root.wm_title("Label Clear Test")
root.geometry("300x200")
root.mainloop()
谢谢 PBSnake
threading
模块李>threading.Timer
是一个与GUI并行运行的对象,因此 不会冻结的李>下面的代码应该可以做到这一点:
相关问题 更多 >
编程相关推荐