Search code examples
pythonwidgetgtkdisplayreparenting

Python Gtk: How to use the same widget on different pages of the same notebook?


I'm currently using gtk on Python to create some graphical interfaces. And I'm struggling with a little issue : I want to display a gtk.widget (HBox or Button for example) on several pages of a notebook but I can't succeed. The widget is only displayed on the first page where it is used but never on the following ones. I've tried the reparenting method but inverted problem, the widget is only displayed on the last one.

import gtk
w = gtk.Window(gtk.WINDOW_TOPLEVEL)
w.connect("destroy", gtk.main_quit)
w.set_title("Some tests")
legnth = 850
width = 700
w.set_size_request(length, width)

VBoxWindow = gtk.HBox()

hbox1 = gtk.HBox()
notebook1 = gtk.Notebook()
page1 = gtk.Notebook()
page12 = gtk.VBox()
page13 = gtk.VBox()
page14 = gtk.VBox()

page1.append_page(page12, gtk.Label(' PAGE1 ')
page1.append_page(page13, gtk.Label(' PAGE2 ')
page1.append_page(page14, gtk.Label(' PAGE3 ')

box1 = gtk.VBox()
button1 = gtk.Button("BTN 1")
box1.pack_start(button1, True, True, 5)
page12.pack_start(box1, False, False, 5)

box2 = gtk.VBox()
box2.pack_start(button1, True, True, 5)
page13.pack_start(box2, False, False, 5)

# reparenting method test
#box3 = gtk.VBox()
#button1.reparent(box3)
#box3.pack_start(button1, True, True, 5)
#page13.pack_start(box3, False, False, 5)

page1.props.border_width = 12
page1.add(page12)
page1.add(page13)
notebook1.append_page(page1, gtk.Label('Some MAIN Page'))
VBoxWindow.add(notebook1)
w.add(VBoxWindow)
displayReady = True
w.show_all()
gtk.main()

Solution

  • Sorry for the wait but I completely forgot to share my solution with you guys !

    Here's what I found :

    def reparent_on_switch(self, widget, page):
        for p in range(widget.get_n_pages()):
            container = widget.get_nth_pages(p)
            if not (p == page):
                pass
            else:
                self.foo.reparent(container)
                container.pack_start(self.foo)
    

    It works like a charm !