close sub-window without closing main window PyGTK in python

1.4k views Asked by At

I am creating a gui in python with PyGtk and I would like to create a sub-window that branches off from my main window but unlike a dialog the user will be able to interact with the main window without having to close the sub-window. The following code was demonstrated to me by @jcoppens in a previous question I asked about how to implement a sub-window and I am currently using it to test my ideas:

from gi.repository import Gtk

class AnotherWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="GCT")
        self.connect("destroy", lambda x: Gtk.main_quit())

        self.add(Gtk.Label("This is another window"))
        self.show_all()

class Main(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="GCT")
        self.connect("destroy", lambda x: Gtk.main_quit())

        self.box = Gtk.Box()
        self.set_default_size(300, 300)
        self.set_position(Gtk.WindowPosition.CENTER)
        self.table = Gtk.Table(6, 5)

        self.button = Gtk.Button("sub-window")
        self.button.connect("clicked", self.open_window)
        self.table.attach(self.button, 0, 2, 0, 1)

        self.box.add(self.table)
        self.add(self.box)
        self.show_all()

    def open_window(self, win):
        subw = AnotherWindow()


def main():
    m = Main()
    Gtk.main()
    return 0

if __name__ == '__main__':
    main()

When I run this code I am able to open my sub-window and still be able to interact with my main window like I would like but unfortunately when I close my sub-window the main window closes with it. How can I code to only close the sub-window without exiting the application?

1

There are 1 answers

1
Wolfgang On BEST ANSWER

It is possible to hide a window simply:

class AnotherWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="GCT")
        self.connect("destroy", self.on_destroy)

        self.add(Gtk.Label("This is another window"))
        self.show_all()

    def on_destroy(self, widget):
        widget.hide()