Alb
Alb

Reputation: 1330

Opening and closing a subwindow on pygtk

I'm trying to create an application with pygtk that opens a subwindow, but I'm having some trouble with the creation/showing and the destruction/hiding process of this window. As a test, I've made a simple application that opens a main window with a single button that, when clicked, opens a subwindow with three buttons. I've used glade to design the interface. My code is as following:

main.py

import gobject
import pygtk
import gtk.gtkgl
import gtk.glade
from interface import *

if __name__ == "__main__":
    gtk.gdk.threads_init()
    settings = gtk.settings_get_default()
    settings.props.gtk_button_images = True
    intf = interface()
    gtk.main()

and interface.py

import pygtk
import gtk.gtkgl
import gtk.glade

class interface(object):

    def __init__(self):

        self.builder = gtk.Builder()
        self.builder.add_from_file("web-interface.glade")

        self.main_window = self.builder.get_object("main_window")
        self.camera_window = self.builder.get_object("cam_window")
        self.main_window.show()

        self.builder.connect_signals(self)

    def on_main_window_destroy(self, widget):
        gtk.main_quit()

    def on_camera_clicked(self, widget):
        self.camera_window.show()

    def on_bt1_clicked(self, widget):
        self.camera_window.hide()

    def on_cam_window_destroy(self, widget):
        self.camera_window.hide()
        return

where 'bt1' is a button on camera_window to hide it and 'camera' is a button on the main window that opens the subwindow.

Now, when I try to open the sub window with 'camera' and close it with 'bt1', it works just fine. But if I try to close the sub window with the 'X' button, the next time I open it I get a gray, empty window, even though I changed the destroy event to only hide the window. What am I doing wrong?

Thanks in advance!

Upvotes: 2

Views: 3347

Answers (1)

ptomato
ptomato

Reputation: 57870

Connect to the delete-event signal instead of destroy. The destroy signal can't be blocked.

Also, make sure you return True from your delete-event handler. This means that you have handled the event, and further handling will be blocked. If you don't, then the default handler will still be called after yours, and so the window will be hidden and then still destroyed anyway.

Upvotes: 8

Related Questions