Python - 确定子进程何时完成且父进程 Tkinter GUI 仍可交互
Python - Determining when subprocess has completed with parent process Tkinter GUI still interactable
我有一个 Tkinter GUI,我想生成一个子进程并找出子进程何时结束而不等待子进程终止,这意味着我的 GUI 仍然是完全可交互的/没有被冻结。
我尝试了很多方法,例如在以下(主要是 Whosebug)链接中找到的方法:1, 2, 3, and 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
我应该研究什么方法来实现我想要的功能?任何概念性建议、伪代码或代码的实际示例都会很有帮助。
我可以澄清任何没有意义的事情。
在 Linux 上,您收到一个子进程死亡的信号。
在信号处理程序中捕获该信号。
如果您需要跨平台代码(包括Windows),您应该创建一个与self.process
通信的新线程。
我有一个 Tkinter GUI,我想生成一个子进程并找出子进程何时结束而不等待子进程终止,这意味着我的 GUI 仍然是完全可交互的/没有被冻结。
我尝试了很多方法,例如在以下(主要是 Whosebug)链接中找到的方法:1, 2, 3, and 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
我应该研究什么方法来实现我想要的功能?任何概念性建议、伪代码或代码的实际示例都会很有帮助。
我可以澄清任何没有意义的事情。
在 Linux 上,您收到一个子进程死亡的信号。 在信号处理程序中捕获该信号。
如果您需要跨平台代码(包括Windows),您应该创建一个与self.process
通信的新线程。