1

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

$ echo "断开连接" | 数控本地主机 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()
4

2 回答 2

2

gtk.main()是一个阻塞函数,因此下一行代码gtk.main_quit()在被调用(异步)之前不会执行/解释。实现您正在寻找的一种可能的方法是在调用之前添加一个timeoutusing ,如下所示: gobject.timeout_addgtk.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()
...

不需要后续调用 sleep 和 main 退出。
也没有必要 gobject.MainLoop.run()s.run()

希望这可以帮助!

于 2013-09-24T17:20:08.197 回答
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()
于 2013-09-24T20:04:04.290 回答