【发布时间】:2016-12-03 11:42:37
【问题描述】:
我有一个 Tkinter GUI,我想生成一个子进程并找出子进程何时结束而不等待子进程终止,这意味着我的 GUI 仍然是完全可交互的/没有冻结。
我尝试了很多方法,例如在以下链接(主要是 stackoverflow)中找到的方法:1、2、3 和 4。
我发现我不能使用任何使用 for 或 while 循环来读取行的方法,否则我的 GUI 会等待循环完成所有内容的读取。根据我的决定,我需要某种线程。但是,通过上面的链接使用一些示例似乎并不能解决我的问题;例如,调整 [4] 中的代码以使其与我的代码一起工作和理解会导致我的 GUI 冻结,直到程序终止。
我的代码格式:
class MyClass(tk.Frame):
def _init_(self,parent):
# calls constructor of inherited class
# other relevant code to initiate
self.initUI()
def runButtonFunction(self):
# self.process = Popen(program_I_want_to_open)
# ?? Need some way to determine when subprocess has
# exited so I can process the results created by that subprocess
def stopButtonFunction(self):
# terminates the subprocess created in run and its children at
# any moment subprocess is running
def initUI(self):
# creates all UI widgets, one of which is a button starts the
# subprocess and another button that can terminate the subprocess
我应该采用什么方法来实现我想要的功能?任何概念性建议、伪代码或实际代码示例都会很有帮助。
我可以澄清任何没有意义的事情。
【问题讨论】:
标签: python multithreading tkinter subprocess python-3.5