简体   繁体   English

after() 方法暂时冻结 tkinter GUI

[英]after() method temporarily freezes tkinter GUI

I am trying to make the text from a tkinter label change after ten seconds using the after method.我正在尝试使用 after 方法在十秒后从 tkinter label 更改文本。 but when I run the program, nothing happens for ten seconds and the the GUI appears and the label has the second text, not the first.但是当我运行程序时,十秒钟内什么也没有发生,GUI 出现了,label 有第二个文本,而不是第一个。 What I want is for the GUI to appear, and the label to have the first text, ten seconds later the text in label should change to the second text.我想要的是 GUI 出现,并且 label 有第一个文本,十秒钟后 label 中的文本应该更改为第二个文本。

import tkinter as tk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("sistema de registro de peso aromaticas")
        self.container = tk.Frame(self)
        self.container.pack(side="top", fill="both", expand=True)
        self.container.grid_rowconfigure(0, weight=1)
        self.container.grid_columnconfigure(0, weight=1)
        self.etiqueta=tk.Label(text="1")
        self.etiqueta.pack()
        def cambio():
            self.after(10000, None)
            self.etiqueta.configure(text="2")
        cambio()

app = SampleApp()
app.mainloop()

I know I can make it work without using class inheritance but I am making an interface with various windows and I am using different classes for every window.我知道我可以在不使用 class inheritance 的情况下使其工作,但我正在与各种 windows 建立接口,并且我为每个 Z05B8AF74FCBD49 使用不同的类。 So I need this to work inside the class.所以我需要这个在 class 中工作。

self.after(10000, None) is the same as self.after(10000) , which is effectively the same as time.sleep(10) . self.after(10000, None)self.after(10000)相同,实际上与 time.sleep time.sleep(10)相同。 It freezes the UI until the timer period is up.它会冻结 UI,直到计时器周期结束。

If you want to call self.etiqueta.configure(text="2") then you can do it like this:如果你想调用self.etiqueta.configure(text="2")那么你可以这样做:

def cambio():
    self.etiqueta.configure(text="2")
self.after(10000, cambio)

That will cause the cambio function to run after 10 seconds.这将导致cambio function 在 10 秒后运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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