关闭窗口不是退出应用程序

问题描述:

我正在阅读有关 builder.connect_signals 的文章,它使用您的 Python 文件中的方法映射了 Glade 文件的处理程序.显然有效,除了主窗口,当您关闭它时不会破坏它.如果您从终端运行它仍在运行并且必须按 Ctrl-C 才能完全关闭该应用程序.

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.

这是我的python代码:

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

在关闭窗口时你的窗口销毁但程序的主循环没有停止,你必须将 destroy 事件连接到方法/函数从最后一行代码运行的循环中退出.在以下代码行中进行一些更改:

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)

改为:

if (window):
    window.connect("destroy", self.Destroy)