[英]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. 我正在开发一个程序,在TKinter中测试.pack_forget()命令,但遇到了问题。 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.
我的代码中有一个time.sleep()命令,直到在IDLE中执行了time.sleep()命令后,TKinter窗口才会打开。 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. time.sleep()
使整个程序进入睡眠状态,因此它无法执行任何操作。
You should instead use: 您应该改用:
main.after(3000, myLabel.pack_forget)
to run myLabel.pack_forget()
after 3000 miliseconds, ie 3 seconds. 在3000毫秒(即3秒
myLabel.pack_forget()
后运行myLabel.pack_forget()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.