问题
I was reading about builder.connect_signals which maps handlers of glade files with methods in your python file. Apparently works, except for the Main Window, which is not destroying when you close it. If you run it from terminal is still running and have to Ctrl-C to completely close the application.
Here is my python code:
#!/usr/bin/env python
import pygtk
import gtk
#from gi.repository import Gtk
import gtk.glade
class Mixer:
def __init__(self):
self.gladefile = "mixer3.glade"
self.wTree = gtk.Builder()
self.wTree.add_from_file(self.gladefile)
window = self.wTree.get_object("window1")
#if (window):
# window.connect("destroy", gtk.main_quit)
#line_btn = self.wTree.get_object("toggle_linein")
#line_btn.connect("on_toggle_linein_activate", btn_linein_activated)
self.wTree.connect_signals(self)
window.show_all() # must have!
def on_toggle_linein_clicked(self, widget):
print "Clicked"
def Destroy(self, obj):
gtk.main_quit()
if __name__ == "__main__":
m = Mixer()
gtk.main()
回答1:
On closing window your window destroying but main loop of program don't stop, you must connect destroy event to the method/function that quit from this loop that ran from last line of code. Make some change in below lines of codes:
#if (window):
# window.connect("destroy", gtk.main_quit)
change to:
if (window):
window.connect("destroy", self.Destroy)
回答2:
You can use GtkApplication
and GtkApplicationWindow
to manage it for you. When Application has no more open windows, it will automatically terminate.
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gio
class Mixer(Gtk.Application):
def __init__(self):
super(Mixer, self).__init__(application_id="org.test",
flags=Gio.ApplicationFlags.FLAGS_NONE)
def do_activate(self):
self.gladefile = "mixer3.glade"
self.wTree = Gtk.Builder()
self.wTree.add_from_file(self.gladefile)
# window1 must be an ApplicationWindow in glade file
window = self.wTree.get_object("window1")
self.add_window(window) # window should be added to application
# but only after 'activate' signal
window.show_all()
if __name__ == "__main__":
m = Mixer()
m.run() # No gtk.main(), GtkApplication manages it
来源:https://stackoverflow.com/questions/28147183/closing-window-is-not-quitting-the-application