简体   繁体   中英

Python 3 TKinter Window will not open until command has been executed

I was working on a program testing the .pack_forget() command in TKinter, but encountered a problem. I had a time.sleep() command in my code, and the TKinter window will not open until the time.sleep() command has been executed in IDLE. Here is my code:

from tkinter import *
import time
main = Tk()
main.title("Test")
myLabel = Label(main, text="I'm a Label", fg="black")
myLabel.pack()
time.sleep(3)
myLabel.pack_forget()

If you know why this issue is occurring, please answer.

time.sleep() puts the entire program to sleep, so that it cannot do anything.

You should instead use:

main.after(3000, myLabel.pack_forget)

to run myLabel.pack_forget() after 3000 miliseconds, ie 3 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