我在尝试退出多窗口程序时遇到了一些问题,确切地说是2。 有一个连接窗口和一个主窗口。如果用户单击连接窗口的本机X按钮,它将关闭,程序结束,一切正常。
但是如果用户连接并且主窗口打开,那么如果用户点击主窗口的X按钮,则窗口将关闭但该过程不会终止。
我尝试了不同的方法,但似乎都没有用,所以这是代码的当前状态:
连接窗口(self.window.connect('delete-event', gtk.main_quit)
行被注释,因为delete-event被绑定到我的.glade文件中的quit方法):
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gio
class ConnectWinHandler(object):
def __init__(self, window, builder, gtk):
self.window = window
self.builder = builder
self.gtk = gtk
# self.window.connect('delete-event', self.gtk.main_quit)
def connect_button_clicked(self, widget):
ip, port = self.ip_port_entry.get_text().split(':')
try:
# some irrelevant connection logic
except IOError as ex:
# irrelevant error handling
else:
main_window = MainWin(self.window, self.gtk)
self.window.close()
main_window.show_all()
def quit(self, widget, data=None):
self.gtk.main_quit()
class ConnectWin(object):
def __init__(self, gtk):
self.gtk = gtk
self.builder = self.gtk.Builder()
self.builder.add_from_file(r'some_path_to_glade_file')
self.window = self.builder.get_object('main')
self.builder.connect_signals(ConnectWinHandler(self.window, self.builder, self.gtk))
def show_all(self):
self.window.show_all()
self.gtk.main()
主窗口(同样,self.window.connect('delete-event', self.gtk.main_quit)
被注释,因为delete-event被绑定到我的.glade文件中的quit方法):
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gio, GObject
class MainWinHandler(object):
def __init__(self, connect_win, window, builder, gtk):
self.connect_win = connect_win
self.window = window
self.builder = builder
self.gtk = gtk
# self.window.connect('delete-event', self.gtk.main_quit)
def quit(self, widget, data=None):
self.gtk.main_quit()
class MainWin(object):
def __init__(self, connect_win, gtk, krpc_handler):
self.gtk = gtk
self.builder = self.gtk.Builder()
self.builder.add_from_file(r'path_to_glade_file')
self.window = self.builder.get_object('main')
self.builder.connect_signals(MainWinHandler(connect_win, self.window, self.builder, self.gtk, krpc_handler))
def show_all(self):
self.window.show_all()
self.gtk.main()
我认为这不重要,但每个窗口都在它自己的.py文件中,程序从第三个文件启动:
from gui.connect_win import ConnectWin
from gi.repository import Gtk
connect_win = ConnectWin(Gtk)
connect_win.show_all()