在PyGTK中,我如何使用线程?

时间:2011-05-13 19:28:51

标签: python multithreading gtk pygtk

我有一个使用gtk绘制GUI的类。

单击按钮将调用将运行某些外部的方法 程序

但GUI可能不会在此期间重绘。

一种解决方案可能是使用线程。这个example创建了一个线程 在GUI类之外,在调用gtk.main()之前启动它。

如何在GUI类之外创建一个线程来检测按钮单击事件并调用 适当的方法?

1 个答案:

答案 0 :(得分:4)

您不需要另一个线程来启动外部程序,您可以使用Gtk的空闲循环。以下是我编写的一些程序。它必须读取程序的标准输出以在GUI上显示它的一部分,所以我把它留在那里。变量“job_aborted”与“Abort”按钮绑定,允许提前终止。

class MyWindow ...

    # here's the button's callback
    def on_simulate(self, button):
      self.job_aborted = False
      args = self.makeargs()  # returns a list of command-line args, first is program
      gobject.idle_add(self.job_monitor(args).next)


    def job_monitor(self, args):
       self.state_running()  # disable some window controls
       yield True  # allow the UI to refresh

       # set non-block stdout from the child process
       p  = subprocess.Popen(args, stdout=subprocess.PIPE)
       fd = p.stdout.fileno()
       fl = fcntl.fcntl(fd, fcntl.F_GETFL)
       fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)

       while True:

         if self.job_aborted:
           os.kill(p.pid, signal.SIGTERM)
           break

         poll = p.poll()
         if poll is not None:
           break

         try:
           line = p.stdout.readline()
           if line:
              line = line.strip()
              # update display

         except IOError:
           pass

         yield True

       self.state_ready()  # re-enable controls
       if self.job_aborted:
         # user aborted
       else:
         # success!