【问题标题】:Python - Determining when subprocess has completed with parent process Tkinter GUI still interactablePython - 确定子进程何时完成与父进程 Tkinter GUI 仍然可交互
【发布时间】:2016-12-03 11:42:37
【问题描述】:

我有一个 Tkinter GUI,我想生成一个子进程并找出子进程何时结束而不等待子进程终止,这意味着我的 GUI 仍然是完全可交互的/没有冻结。

我尝试了很多方法,例如在以下链接(主要是 stackoverflow)中找到的方法:1234

我发现我不能使用任何使用 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


    【解决方案1】:

    在 Linux 上,您会收到一个子进程死亡的信号。 在 Signal 处理程序中捕获该信号。

    如果您需要跨平台代码(包括 Windows),您应该创建一个与self.process 通信的新线程。

    【讨论】:

      猜你喜欢
      • 2017-07-11
      • 2023-03-09
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      相关资源
      最近更新 更多