I am having trouble with dynamically adding pages to a Gtk.Notebook. Pages created before Gtk.main() is called are displayed fine, but pages created afterwards do not show up - nothing changes in the GUI.

#!/usr/bin/env python

from gi.repository import Gtk

class MyApp:
    def __init__(self):
        self.window = Gtk.Window()

        self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
        self.window.add(self.box)

        self.notebook = Gtk.Notebook()
        self.box.pack_start(self.notebook, True, True, 0)

        self.button = Gtk.Button(label='Add Page')
        self.button.connect('clicked', self.on_button_clicked)
        self.box.pack_start(self.button, False, False, 0)

        for _ in range(2):
            numpage = self.notebook.get_n_pages() + 1
            label = Gtk.Label(label='label{}'.format(numpage))
            tab = Gtk.Label('tab{}'.format(numpage))

            self.notebook.append_page(label, tab)
            print(self.notebook.get_n_pages())

    def on_button_clicked(self, widget):
        numpage = self.notebook.get_n_pages() + 1
        label = Gtk.Label(label='label{}'.format(numpage))
        tab = Gtk.Label('tab{}'.format(numpage))

        self.notebook.append_page(label, tab)
        print(self.notebook.get_n_pages())


app = MyApp()
app.window.connect('delete-event', Gtk.main_quit)
app.window.show_all()
Gtk.main()

The console output, however, suggests that they are indeed being created:

C:\dev>python notebook.py
1
2
3
4
5

I tried doing self.notebook.hide() and self.notebook.show() inside on_button_clicked(), but that didn't help. Am I missing something?

Thanks.

有帮助吗?

解决方案

Widgets are always created invisible by default. You have to show the individual widgets that you add to the notebook:

label.show()
tab.show()

or show everything at once:

self.notebook.show_all()

Hiding and showing the notebook won't change the visible status of widgets inside the notebook.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top