Python - 1秒后我无法退出GUI,当调用gtk.main()时它永远不会退出,我该如何退出?

时间:2013-09-24 12:29:33

标签: python linux gtk pygtk gnome

运行gtk.main()后如何退出GUI?它在1秒后没有退出 它退出的唯一方法就是使用它:

$ echo“disconnect”| nc localhost 58887

import re,os, time, socket, datetime, threading, subprocess
import gobject, gtk

gobject.threads_init()

class gui(object):

  def listener(self, sock, *args):
    conn, addr = sock.accept()
    gobject.io_add_watch(conn, gobject.IO_IN, self.handler)
    return True

  def handler(self, conn, *args):
    line = conn.recv(4096)
    if not len(line):
      return False
    else:
      if line:
        if line.startswith("disconnect"):
          self.quit(self.window)
      return True

  def __init__(self):
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(6400, 6400, 6440))
    self.window.set_size_request(300, 220)
    self.window.set_border_width(1)
    self.window.set_decorated(False)
    self.window.set_title("TPT")
    self.window.move(250,200)
    self.window.set_name("main window")
    self.window.connect("delete-event", gtk.main_quit)
    self.drawingarea = gtk.DrawingArea()
    self.window.add(self.drawingarea)

  def run(self):
    self.sock = socket.socket()
    self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.sock.bind(('', 58887))
    self.sock.listen(1)
    gobject.io_add_watch(self.sock, gobject.IO_IN, self.listener)    
    self.window.show_all()
    self.window.set_keep_above(True)
    if(self.window.get_window().get_state() == gtk.gdk.WINDOW_STATE_MAXIMIZED):
      self.window.unmaximize()    
    gtk.main()

    time.sleep(1)
    gtk.main_quit()

  def quit(self, window):
    gtk.main_quit()

if __name__=='__main__':
    os.system("lsof -i tcp:58887 | grep LISTEN | awk '{print $2}' | xargs kill -9;")
    s=gui()
    s.run()
    gobject.MainLoop.run()

2 个答案:

答案 0 :(得分:3)

gtk.main()是一个阻塞函数,因此在调用gtk.main_quit()(异步)之前不会执行/解释下一行代码。实现所需内容的一种可能方法是在调用timeout之前使用gobject.timeout_add添加gtk.main(),其中包含以下内容:

...
    if(self.window.get_window().get_state() == gtk.gdk.WINDOW_STATE_MAXIMIZED):
      self.window.unmaximize()
    gobject.timeout_add(1000, gtk.main_quit)
    gtk.main()
...

不需要随后的睡眠和主要戒烟呼叫 gobject.MainLoop.run()s.run()也不是必需的。

希望这有帮助!

答案 1 :(得分:1)

例如,这几乎可以工作。

第1步:要触发的外部程序

#!/bin/bash
sleep 2
# do other task
ps aux | grep test.py | awk '{print $2}' | xargs kill -9;
# do other task, its my world i do what i want.... bla bla bla...:P

第2步:线程

import re,os, time, socket, datetime, threading, subprocess
import gobject, gtk

gobject.threads_init()

""" :::::::::::: Threading :::::::::::: """
def task1():
  os.system("export DISPLAY=:0.0 && xterm &")
  subprocess.Popen(["/var/tmp/test.sh"], stdout=subprocess.PIPE)
""" :::::::::::: Threading :::::::::::: """

class gui(object):

  def listener(self, sock, *args):
    conn, addr = sock.accept()
    gobject.io_add_watch(conn, gobject.IO_IN, self.handler)
    return True

  def handler(self, conn, *args):
    line = conn.recv(4096)
    if not len(line):
      return False
    else:
      if line:
        if line.startswith("disconnect"):
          self.quit(self.window)
      return True

  def __init__(self):
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.modify_bg(gtk.STATE_NORMAL, gtk.gdk.Color(6400, 6400, 6440))
    self.window.set_size_request(300, 220)
    self.window.set_border_width(1)
    self.window.set_decorated(False)
    self.window.set_title("TPT")
    self.window.move(250,200)
    self.window.set_name("main window")
    self.window.connect("delete-event", gtk.main_quit)
    self.drawingarea = gtk.DrawingArea()
    self.window.add(self.drawingarea)

  def run(self):
    self.sock = socket.socket()
    self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.sock.bind(('', 58887))
    self.sock.listen(1)
    gobject.io_add_watch(self.sock, gobject.IO_IN, self.listener)    
    self.window.show_all()
    self.window.set_keep_above(True)
    if(self.window.get_window().get_state() == gtk.gdk.WINDOW_STATE_MAXIMIZED):
      self.window.unmaximize()    

    """ :::::::::::: Threading :::::::::::: """
    t1 = threading.Thread(target=task1)
    t1.start()
    t1.join()
    """ :::::::::::: Threading :::::::::::: """

    gtk.main()


  def quit(self, window):
    gtk.main_quit()

if __name__=='__main__':
    os.system("lsof -i tcp:58887 | grep LISTEN | awk '{print $2}' | xargs kill -9;")
    s=gui()
    s.run()
    gobject.MainLoop.run()