Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python线程Tkinter:为什么线程的第二次启动不能正常运行?_Python_Multithreading_Tkinter - Fatal编程技术网

Python线程Tkinter:为什么线程的第二次启动不能正常运行?

Python线程Tkinter:为什么线程的第二次启动不能正常运行?,python,multithreading,tkinter,Python,Multithreading,Tkinter,我正在尝试学习如何在终端工作时托管第二个助手窗口来托管一些数据。其中一部分涉及定期更改窗口的内容,我希望第二个窗口不会阻塞主进程。因此,我希望能够启动tkinter文本作为一个线程 下面是我目前拥有的。第一次调用启动线程时,tkinter文本框就会出现,一旦关闭,线程就会加入。在第二次启动时,不会创建窗口,并且您可以看到从未调用“启动后”。有人知道我做错了什么吗?谢谢 >>> import threading >>> import tkinter as tk

我正在尝试学习如何在终端工作时托管第二个助手窗口来托管一些数据。其中一部分涉及定期更改窗口的内容,我希望第二个窗口不会阻塞主进程。因此,我希望能够启动tkinter文本作为一个线程

下面是我目前拥有的。第一次调用启动线程时,tkinter文本框就会出现,一旦关闭,线程就会加入。在第二次启动时,不会创建窗口,并且您可以看到从未调用“启动后”。有人知道我做错了什么吗?谢谢

>>> import threading
>>> import tkinter as tk
>>> def build_textbox():
    root = tk.Tk()
    T= tk.Text(root, height=100, width=100)
    T.pack()
    T.insert(tk.END, "Hello World")
    tk.mainloop()


>>> def launch_thread():
    print("Function called")
    thread = threading.Thread(target=build_textbox)
    print("Thread created")
    thread.start()
    print("After start")
    thread.join()
    print("Success")


>>> launch_thread()
Function called
Thread created
After start
Success
>>> launch_thread()
Function called
Thread created

首先需要调用
root.mainloop()
。。。这段代码中有很多东西需要更改。使用交互式shell时,Tkinter的编程方式应该稍有不同。首先需要调用
root.mainloop()
。。。这段代码中有很多东西需要更改。当使用交互式shell时,Tkinter的编程应该稍有不同。