Search code examples
pythontabsgtkpygtk

How can i make tabs in pygtk closable?


i am using python with gtk, trying to make a simple text editor with tabs, i am still new, so i wonder how can i make the tabs closable and reordable ? it is much easier in qt, but i loved gtk more. this is my code:

import gtk
from tab import *

 class Dash(gtk.Notebook):
    def __init__(self):
        super(gtk.Notebook,self).__init__()
        self.defaultTab()

    def defaultTab(self):
        tab = Tab()
        self.append_page(tab.child,tab.label)

the other module "tab" has some variables :

from launchers import *

class Tab():
    def __init__(self):
        self.label = gtk.Label("New Tab")
        self.type = "dash"
        launchers = Launchers()
        self.child = launchers

so what i gotta do ?


Solution

  • Instead of using a gtk.Label when appending a new page to the gtk.Notebook, you need to create a gtk.HBox that contains both a gtk.Label and a gtk.Button. More or less, something like this:

    class Dash(gtk.Notebook):
    ...
        def defaultTab(self):
            self.append_page(tab.child,tab.header)
    ...
    class Tab():
        def __init__(self):
            ...
            header = gtk.HBox()
            title_label = gtk.Label()
            image = gtk.Image()
            image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
            close_button = gtk.Button()
            close_button.set_image(image)
            close_button.set_relief(gtk.RELIEF_NONE)
            self.connect(close_button, 'clicked', self.close_cb)
    
            header.pack_start(title_label,
                              expand=True, fill=True, padding=0)
            header.pack_end(close_button,
                            expand=False, fill=False, padding=0)
            header.show_all()
            self.header = header
            ...
    

    This is just to display the close button. To actually close the tab, you'll need to handle the clicked signal from the button.