#!/usr/bin/env python import gtk import sys #global variables window = None notebook = None class MyNotebook(gtk.Notebook): def __init__(self): gtk.Notebook.__init__(self) #set the tab properties self.set_property('homogeneous', True) #we do not show the tab if there is only one tab i total self.set_property('show-tabs', False) def new_tab(self): #we create a "Random" image to put in the tab icons = [gtk.STOCK_ABOUT, gtk.STOCK_ADD, gtk.STOCK_APPLY, gtk.STOCK_BOLD] image = gtk.Image() nbpages = self.get_n_pages() icon = icons[nbpages%len(icons)] image.set_from_stock(icon, gtk.ICON_SIZE_DIALOG) self.append_page(image) #we want to show the tabs if there is more than 1 if nbpages + 1 > 1: self.set_property('show-tabs', True) #creation of a custom tab. the left image and #the title are made of the stock icon name #we pass the child of the tab so we can find the #tab back upon closure label = self.create_tab_label(icon, image) label.show_all() self.set_tab_label_packing(image, True, True, gtk.PACK_START) self.set_tab_label(image, label) image.show_all() self.set_current_page(nbpages) def create_tab_label(self, title, tab_child): box = gtk.HBox() icon = gtk.Image() icon.set_from_stock(title, gtk.ICON_SIZE_MENU) label = gtk.Label(title) closebtn = gtk.Button() #the close button is made of an empty button #where we set an image image = gtk.Image() image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU) closebtn.connect("clicked", self.close_tab, tab_child) closebtn.set_image(image) closebtn.set_relief(gtk.RELIEF_NONE) box.pack_start(icon, False, False) box.pack_start(label, True, True) box.pack_end(closebtn, False, False) return box def close_tab(self, widget, child): pagenum = self.page_num(child) if pagenum != -1: self.remove_page(pagenum) child.destroy() if self.get_n_pages() == 1: self.set_property('show-tabs', False) def on_destroy(win): gtk.main_quit() def on_delete_event(widget, event): gtk.main_quit() def new_tab(widget): notebook.new_tab() if __name__ == '__main__': window = gtk.Window() window.set_title("Custom Gtk.Notebook Tabs example") window.resize(600,400) box = gtk.VBox() button = gtk.Button("New Tab") box.pack_start(button,False) button.connect("clicked", new_tab) notebook = MyNotebook() box.pack_start(notebook) window.add(box) window.connect("destroy", on_destroy) window.connect("delete-event", on_delete_event) window.show_all() gtk.main() sys.exit(0)