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 每个线程不能打开一个tk窗口,tkinter_Python_Multithreading_Tkinter - Fatal编程技术网

Python 每个线程不能打开一个tk窗口,tkinter

Python 每个线程不能打开一个tk窗口,tkinter,python,multithreading,tkinter,Python,Multithreading,Tkinter,我想写一个程序,每次接受一个弹出窗口在桌面角落的消息 我使用web服务器来接受消息 我的Tk代码是这样的: import threading from Tkinter import * import time def alert(): alert = Tk() alert.protocol("WM_DELETE_WINDOW", alert.quit) alert.mainloop() def run(): th = threading.Thread(target=alert

我想写一个程序,每次接受一个弹出窗口在桌面角落的消息

我使用web服务器来接受消息

我的Tk代码是这样的:

import threading
from Tkinter import *
import time

def alert():
  alert = Tk()
  alert.protocol("WM_DELETE_WINDOW", alert.quit)
  alert.mainloop()

def run():
  th = threading.Thread(target=alert)
  th.start()

if __name__ == 'main':
  run()
  time.sleep(5)

  run()
  time.sleep(5)

  run()
当我运行这个时,只弹出一个窗口。当第二个线程运行“alert=Tk()”时,程序似乎挂断了,我不确定

第三个线程从未运行过

如果我没有在第二个线程启动之前关闭窗口,窗口将失去响应。

我想知道我的代码有什么问题以及tkinter是如何工作的


谢谢

为什么要使用线程?您可以在一个线程中打开数百或数千个窗口。@BryanOakley mainloop()挂起您需要在一个线程中运行web服务器,并将tkinter保持在主线程中。其他代码可以将消息放入队列,主线程可以轮询队列并显示消息。tkinter不能在多个线程中运行。@BryanOakley mainloop()挂起RequestHandler的线程,请求永远不会响应。我不熟悉tk,我只是想使用线程。@BryanOakley好的,我会试试这个。但为什么tkinter必须在主线程中运行。它似乎真的与java不同。