Roberto
Roberto

Reputation: 178

Connect a method for window destroy

I have a main window with a Gtk Button named openDialog. If I click on this button another Window (addName) popups. I would like to write a method (or a function, don't know which is the right name in python) in my main window file, called printHi. I would like to run this printHi method (in my main window file), when addName window is destroyed.

I tried something like this:

def on_addName_destroy():
    printHi()

But it doesn't work. Any suggestion?

Upvotes: 0

Views: 2337

Answers (1)

another.anon.coward
another.anon.coward

Reputation: 11395

You can make use of "delete-event" signal of gtk.Widget. It is also possible to make use of "destroy" signal of gtk.Object. Here is a sample which connects to both the signals although in your case connecting to any one of them should suffice.

#!/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()

Hope this helps!

Upvotes: 1

Related Questions