我有一个带有名为 openDialog 的 Gtk 按钮的主窗口。如果我单击此按钮,则会弹出另一个窗口(addName)。我想在我的主窗口文件 printHi. 当 addName 窗口被销毁时,我想运行这个 printHi 方法(在我的主窗口文件中)。
我试过这样的事情:
def on_addName_destroy():
printHi()
但它不起作用。有什么建议吗?
您可以使用 的"delete-event"
信号gtk.Widget
。也可以使用 的"destroy"
信号gtk.Object
。这是一个连接到两个信号的示例,尽管在您的情况下连接到其中任何一个就足够了。
#!/usr/bin/env python
import gtk
def on_addName_destroy(gtkobject, data=None):
print "This is called later after delete-event callback has been called"
print "Indication that the reference of this object should be destroyed"
print "============================================"
def on_addName_delete(widget, event, data=None):
print "This is called on delete request"
print "Propagation of this event further can be controlled by return value"
print "--------------------------------------------"
return False
def show_popup(widget, data=None):
dialog = gtk.Window(gtk.WINDOW_TOPLEVEL)
dialog.set_size_request(100, 100)
label = gtk.Label("Hello!")
dialog.add(label)
dialog.connect("delete-event", on_addName_delete)
dialog.connect("destroy", on_addName_destroy)
dialog.show_all()
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(100, 100)
button = gtk.Button("Popup")
button.connect("clicked", show_popup)
window.add(button)
window.connect("destroy", lambda x: gtk.main_quit())
window.show_all()
gtk.main()
希望这可以帮助!