簡體   English   中英

Tkinter 按鈕 function 不能正常工作

[英]Tkinter button function doesn't work properly

我有一個從按鈕調用的 function。 它有一個 time.sleep()

def func():
    button.place_forget()
    text = Label(root, text = "Text"
    text.place(x = 0, y = 0, in_ = root)
    time.sleep(1)
    text.place_forget()

button = Button(root, text = "Button", command = func)

當它被調用時,我希望按鈕首先消失,然后文本出現,然后等待 1 秒鍾,然后文本消失。 但是會發生什么是程序等待 1 秒然后添加文本立即將其刪除,然后使按鈕消失。 我如何讓它以我想要的方式工作?

使用after()而不是sleep()

def func():
    button.place_forget()
    text = Label(root, text="Text")
    text.place(x=0, y=0)
    # schedule to remove the text after 1 second
    text.after(1000, text.place_forget)
button = Button(root, text = "Button", command = func()) button.pack()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM