Search code examples
python-3.xgtk3pygtkpygobject

Is it possible to use other widget in the tab lable other than Gtk.Label?


In python gtk docs for notebook widget, there is a mention of append_page which adds a page/tab to the notebook.

The second parameter in append_page is a widget which goes in the title of the page (inplace of the default Page 1), but when any other widget is provided it does not show anything.

Is it possible to add a a custom widget that has two child widgets like (Gtk.Label, Gtk.Button) to close specific tabs from the notebook when needed.

A custom widget implementation would be

class PageTitle(Gtk.Box):
    def __init__(self):
        super().__init__()
        self.title = Gtk.Label("title")
        self.close = Gtk.Button("close")

Again the goal is to have two widgets in the page title.

I tried passing an object of the class PageTitle but was not successful as this resulted in an empty page title


Solution

  • You did not add the childern widgets to the custom widget PageTitle and you should call self.show_all() in __init__ in PageTitle class to show the label and button, here is another example

    The code you want to use looks something like this

    import gi
    gi.require_version("Gtk", "3.0")
    from gi.repository import Gtk
    
    class PageTitle(Gtk.Box):
        def __init__(self):
            super().__init__()
            self.title = Gtk.Label("title")
            self.close = Gtk.Button("close")
            self.add(self.title)
            self.add(self.close)
            self.show_all()
    
    class GUI(Gtk.Window):
        def __init__(self):
            super().__init__()
            self.set_default_size(400, 500)
            self.connect("destroy", Gtk.main_quit)
            self.notebook = Gtk.Notebook()
            self.add(self.notebook)
            self.textview = Gtk.TextView()
            self.notebook.append_page(self.textview, PageTitle())
            self.show_all()
    
    app = GUI()
    Gtk.main()