运行 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()