简体   繁体   English

在执行命令之前,Python 3 TKinter窗口将不会打开

[英]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.

相关问题 禁用 tkinter 菜单选项,直到另一个完全执行 - Disable tkinter menu option until another one has been fully executed Tkinter按钮命令disablebackground是否已在python 3中删除? - Has Tkinter button command disabledbackground been removed in python 3? Python Tkinter [无法调用命令:应用程序已被破坏] - Python Tkinter [ can't invoke command: application has been destroyed ] 如何检查命令是否已经执行 - How to check if a command has already been executed Python-保持Tkinter窗口打开吗? - Python - keeping Tkinter window open? Python/Tkinter - 新窗口命令 - Python/Tkinter - new window command Tkinter-我如何在新窗口中创建按钮,该窗口是由调用函数创建的? Python 3 - Tkinter - How would I create buttons in a new window, which has been created by a function being called? Python 3 Python & macOS:从 Python 打开新的终端窗口,传递要执行的命令 - Python & macOS: open new Terminal window from Python, passing command to be executed Python:有没有办法在脚本用完之前系统地打开脚本中的文件? - Python: Is there a way to systematically open files in a script until a directory has been exhausted? Python:tkinter.TclError:无法调用“标签”命令:应用程序已被破坏 - Python: tkinter.TclError: can't invoke "label" command: application has been destroyed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM