简体   繁体   中英

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. 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. 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.

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. So I need this to work inside the class.

self.after(10000, None) is the same as self.after(10000) , which is effectively the same as time.sleep(10) . It freezes the UI until the timer period is up.

If you want to call self.etiqueta.configure(text="2") then you can do it like this:

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

That will cause the cambio function to run after 10 seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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