[英]In PyGTK, how do I use a thread?
我有一个使用gtk绘制GUI的类。
单击按钮将调用将运行某些外部程序的方法。
但GUI可能不会在此期间重绘。
一种解决方案可能是使用线程。 此示例在GUI类之外创建一个线程,并在调用gtk.main()之前启动它。
如何在GUI类之外创建一个线程来检测按钮单击事件并调用适当的方法?
您不需要另一个线程来启动外部程序,您可以使用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!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.