Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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建议中的线程_Python_Multithreading_Python 3.x - Fatal编程技术网

Python建议中的线程

Python建议中的线程,python,multithreading,python-3.x,Python,Multithreading,Python 3.x,我目前正在尝试在python中执行两个循环。一个是tkinter循环,显示我设置的gui,另一个是p2p聊天功能。使用“导入线程”,定义线程并单独启动它们似乎不起作用。对于我可以使用什么方法使这两个循环同时运行,有什么建议吗 我用于启动线程的代码: thread1 = threading.Thread(target=x.mainloop()) thread1.start() thread2 = threading.Thread(target=root.mainloop()) thread2.st

我目前正在尝试在python中执行两个循环。一个是tkinter循环,显示我设置的gui,另一个是p2p聊天功能。使用“导入线程”,定义线程并单独启动它们似乎不起作用。对于我可以使用什么方法使这两个循环同时运行,有什么建议吗

我用于启动线程的代码:

thread1 = threading.Thread(target=x.mainloop())
thread1.start()
thread2 = threading.Thread(target=root.mainloop())
thread2.start()

您需要传递函数而不调用它们。实际上,您正在尝试调用它们,并将返回值作为线程的目标传递;因为它们永远不会返回,所以您永远不会启动第二个线程。尝试:

thread1 = threading.Thread(target=x.mainloop)  # Removed call parens on target
thread1.start()
thread2 = threading.Thread(target=root.mainloop)  # Removed call parens on target
thread2.start()

这是问题的一部分,是的,但我还了解到Tkinter主要基于单线程事件模型。mainloop、回调、事件处理程序和引发tkinter异常都在单个线程中处理。