不使用函数替换Tkinter中的while循环

2024-04-26 23:06:57 发布

您现在位置:Python中文网/ 问答频道 /正文

有数百个问题涉及到Tkinter中的while True:循环(这当然会使它崩溃,因为它从不执行mainloop()(参见here和{a2}数百个示例中的两个))。显而易见的(目前为止)答案需要某种函数。在

我的问题是:有没有什么方法可以让Tkinter中的while True:循环类似,在不使用函数的情况下,不会崩溃?我没发现有这样的问题。在


Tags: 方法函数答案truea2示例heretkinter
1条回答
网友
1楼 · 发布于 2024-04-26 23:06:57

我对python和tkinter不是很熟悉,但这应该可以:

import tkinter as tk
import threading

root = tk.Tk()
thread = threading.Thread(target=root.mainloop)
thread.start()

while True:
    print("Hello, World!");
    # TODO - add more stuff to this while loop

相关问题 更多 >