1

我正在使用以下代码来检查 Tkinter 是否与多线程一起工作。但是下面的代码不起作用(Gui 一运行就没有响应)。谁能解释为什么它不起作用?

from threading import Thread 
import tkinter as tk

window = tk.Tk()
label = tk.Label(window, text='Hello')
label.pack()

def func():
    i = 1
    while True:
        label['text'] = str(i)
        i += 1

Thread(target=func).start()
Thread(target=window.mainloop).start()
4

1 回答 1

2

It doesn't work because Tkinter doesn't support multithreading. Everything that interacts with a Tkinter widget needs to run in the main thread. If you want to use multithreading, put the GUI in the main thread and your other code in a worker thread, and communicate between them with a thread safe queue.

于 2013-02-24T22:22:28.373 回答