设置时间后清除tkinter标签内容

2024-10-03 21:30:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用状态信息更新的标签。如果状态不变,我想在10秒后清除它的内容。例如,我将标签设置为“第一步”,然后在10秒后清除“”。现在,如果在10秒到期之前,我将标签设置为“第二步”,它将更新并重置10秒计时器

编辑: 好吧,我永远都不能让它正常工作。我制作了一个小应用程序来测试,它有3个按钮,其中两个按钮只是改变标签的文本。我想要发生的是以下几点

  1. 用文本更新标签(按钮1或2)
  2. 启动2秒计时器。(2秒后清除标签)
  3. 如果在2秒钟之前按下按钮1或2,则更新标签并重置计时器。 我所看到的是计时器正在停止工作。如果我按下按钮1,标签将更新,计时器将启动。但我不能再按任何一个按钮,直到计时器结束
    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


Tags: textselfmasternonedefplacebuttonroot
1条回答
网友
1楼 · 发布于 2024-10-03 21:30:43
  • 为此使用threading模块
  • threading.Timer是一个与GUI并行运行的对象,因此 不会冻结的

下面的代码应该可以做到这一点:

import tkinter as tk
import threading

def myLoop():
    var.set(var.get() + 1) #Increment the value
    threading.Timer(1, myLoop).start()

window = tk.Tk()

var = tk.IntVar()
var.set(1)

button = tk.Button(window, text = 'Threading', command = lambda: threading.Timer(1, myLoop).start()) 
button.pack()
label = tk.Label(window, textvariable = var)
label.pack()

window.mainloop()

相关问题 更多 >