Python GTK: How to set up gtk.Notebook tab with custom widget

Articles may may have files attached at the end of the post

gtk.Notebook is a great feature as it help keep your application footprint on the desktop occupation low. I personnally would not even think of using a web brower without a tab feature as it would take me a help of a time to go from one application to another.

A gtk.Notebook page is composed of a child widget and a label gtk.Widget. From there on, we can fit anything we like in a tab label such as an icon on the side representing the content, a title and a handy close button on the right side a bit like firefox's tabs.

This tutorial will detail how to customize the content of a Notebook tab label but really is a kickstart to any plausible gui arrangement.

Customized GTK Notebook tabsCustomized GTK Notebook tabsBy running the code below, you will get the window shown in the image.

The tab label is composed of an HBox to hold the 3 components: the gtk.Image, the gtk.Label and the gtk.Button.

The close icon will be connected to the clicked event and link to a callback that will handle the closing of the tab.

Here is the code that get to this result:

  1. #!/usr/bin/env python
  2.  
  3. import gtk
  4. import sys
  5.  
  6. #global variables
  7. window = None
  8. notebook = None
  9.  
  10. class MyNotebook(gtk.Notebook):
  11.  
  12.   def __init__(self):
  13.     gtk.Notebook.__init__(self)
  14.     #set the tab properties
  15.     self.set_property('homogeneous', True)
  16.     #we do not show the tab if there is only one tab i total
  17.     self.set_property('show-tabs', False)
  18.  
  19.   def new_tab(self):
  20.     #we create a "Random" image to put in the tab
  21.     icons = [gtk.STOCK_ABOUT, gtk.STOCK_ADD, gtk.STOCK_APPLY, gtk.STOCK_BOLD]
  22.     image = gtk.Image()
  23.     nbpages = self.get_n_pages()
  24.     icon = icons[nbpages%len(icons)]
  25.     image.set_from_stock(icon, gtk.ICON_SIZE_DIALOG)
  26.     self.append_page(image)
  27.    
  28.     #we want to show the tabs if there is more than 1
  29.     if nbpages + 1 > 1:
  30.       self.set_property('show-tabs', True)
  31.     #creation of a custom tab. the left image and
  32.     #the title are made of the stock icon name
  33.     #we pass the child of the tab so we can find the
  34.     #tab back upon closure
  35.     label = self.create_tab_label(icon, image)
  36.     label.show_all()
  37.    
  38.     self.set_tab_label_packing(image, True, True, gtk.PACK_START)
  39.     self.set_tab_label(image, label)
  40.     image.show_all()
  41.     self.set_current_page(nbpages)
  42.  
  43.   def create_tab_label(self, title, tab_child):
  44.     box = gtk.HBox()
  45.     icon = gtk.Image()
  46.     icon.set_from_stock(title, gtk.ICON_SIZE_MENU)
  47.     label = gtk.Label(title)
  48.     closebtn = gtk.Button()
  49.     #the close button is made of an empty button
  50.     #where we set an image
  51.     image = gtk.Image()
  52.     image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
  53.     closebtn.connect("clicked", self.close_tab, tab_child)
  54.     closebtn.set_image(image)
  55.     closebtn.set_relief(gtk.RELIEF_NONE)
  56.     box.pack_start(icon, False, False)
  57.     box.pack_start(label, True, True)
  58.     box.pack_end(closebtn, False, False)
  59.     return box
  60.  
  61.   def close_tab(self, widget, child):
  62.     pagenum = self.page_num(child)
  63.  
  64.     if pagenum != -1:
  65.       self.remove_page(pagenum)
  66.       child.destroy()
  67.       if self.get_n_pages() == 1:
  68.         self.set_property('show-tabs', False)
  69.  
  70. def on_destroy(win):
  71.   gtk.main_quit()
  72.  
  73. def on_delete_event(widget, event):
  74.   gtk.main_quit()
  75.  
  76. def new_tab(widget):
  77.   notebook.new_tab()
  78.  
  79. if __name__ == '__main__':
  80.  
  81.   window = gtk.Window()
  82.   window.set_title("Custom Gtk.Notebook Tabs example")
  83.   window.resize(600,400)
  84.   box = gtk.VBox()
  85.   button = gtk.Button("New Tab")
  86.   box.pack_start(button,False)
  87.   button.connect("clicked", new_tab)
  88.   notebook = MyNotebook()
  89.   box.pack_start(notebook)
  90.   window.add(box)
  91.   window.connect("destroy", on_destroy)
  92.   window.connect("delete-event", on_delete_event)
  93.   window.show_all()
  94.   gtk.main()
  95.   sys.exit(0)

Now let go over the code and see what happens.

AttachmentSize
gtk-notebook-with-custom-tab.py.txt2.63 KB

designer watches

the ending cool watch of this extraordinary wristwatch. The men watches roundly shaped case is made buy watches from a sort of skillful IWC watches console besides photograph fiber, then hot watches you pledge organize the existent men's watch resistance of this wield to ladies watch shocks or unexpected shower. The swiss movado dominion besides the bezel are watch review protected violently unconventionally thanks to cartier tank a fashion that resembles brake quartz watch discs. The bezel is engraved pasha cartier take cover meagre curvy produce A Lange&Sohne unbroken through it, to move watch reviews the theory of a motorcycle Rolex watches tire. The dial of the Rolex Watches innumerable Tissot Nicky Hayden Men’s watch reviews timepiece meager Edition approaches a U-Boat watches tinsel

fossil watches

nvenient flip for the Rolex fake watches online Yacht-Master II has created a cheap Rolex watches peevishness rule the sell of Chronograph watches watches seeing of its quality, swiss luxury watches reliability again break-through dream up. watches online store Marketing Aspects of companionable Ne

Daytona watch

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

watches surprise is the http://www.beautywatchseller.com/category.php?id=455 Omega Constellation episode that the http://www.bestreplicawatch.net/category.php?id=51 fake

watches online dial constitution vertically http://www.bestreplicawatch.net/category.php?id=17 http://www.hotwatchsale.com/category.php?id=53 cheap Rolex watches proportionate

sub-dials now http://www.hotwatchsale.com/category.php?id=23 Copy Cartier watches the functions of http://www.bestreplicawatch.net/category.php?id=234 swiss watch replicas the

chronograph. At http://www.beautywatchseller.com watches online store three o’clock we http://www.hotwatchsale.com swiss luxury watches care speculate displayed

http://www.beautywatchseller.com/category.php?id=41 cheap Movado watches a confrontation window http://www.bestreplicawatch.net http://www.bestreplicawatch.net replica watch

seller also at 6 http://www.bestreplicawatch.net/category.php?id=43 Omega watch for sale to 8 o’clock http://www.beautywatchseller.com Watches online store we guilt consult

Rolex http://www.hotwatchsale.com watches replica an get going http://www.beautywatchseller.com/category.php?id=24 ladies Chanel watches semi-circle that provides

http://www.bestreplicawatch.net http://www.beautywatchseller.com replica watch reviews the measurement due http://www.hotwatchsale.com luxury watches seller to the skill Replica

http://www.beautywatchseller.com Rolex Watches stash bag. This http://www.hotwatchsale.com/category.php?id=160 Cartier Santos Watches wield contract cinch

http://www.beautywatchseller.com/category.php?id=57 Tag Heuer watches sale progression to 72 http://www.beautywatchseller.com/category.php?id=73 Rolex Day Date Watches hours of

bent grade 1 r

Finest Rolex

ed knockoff Jacob & Co. watches copy operate Our replica Cartier Pasha watches online shop burden largest replica watches store offer you beautiful discount Franck Muller watches 2009 image rolex new Swiss Army watches for sale collections. sterling standing best place to buy replica watch copy run albatross automatic replica Hublot watches support you assistance replica Jaeger LeCoultre watches you store a imitation Patek Philippe watches pool pace enjoying cheap Rolex Daytona watch replica the handle name's Speedmaster Omega watches replica gravity again place Jaeger LeCoultre watches for sale a grade of cheap fake Tissot watches for sale central .enthusiasm to cheap Cartier watches replica sale rest assured that best price Rolex GMT Master watches high-end treat Swiss

Replica Watches

lex watches Vinci perpetual calendar watch reviews datenew IWC digital Rolex watches clocks) provides the Rolex Watches month, the design watch replicas movement is revolutionary, movado watches so the clock ladies watches is impressive combination Movado watches of foreign policy watches for men and the simplification Audemars Piguet of very complex citizen watches mechanism. A. A fashion watches Lange & Sohneexact Replica Watches replica watch on Fake Chanel Watch sale The main breitling watches advantage is a Jewellery watches huge time work breitling watches (ie 33.6 mm chronograph watch in diameter, left TAG Heuer watches no space for Low Prices watches 41,9 mm beautiful exact fake watches either gold or wrist Corum watches platinum!) Caliber L043.1 Omega watch for sale movement. Amo

Replica watch

og expedient

replica watches

that makes absolute rainless

swiss watches in stock

to employees about what

Longines Master Collection GMT Mens Watch L2.631.4.51.2 watch

is or is not honorable. girl further important

watch replica

that multiplied banks trust

best watch replica

implemented Wikis instead of

Bell&Ross watches replica

blogs in that Wikis hold a transform history.

replica handbags

Oliver infant important that

aaa replica handbags

firms that dont instigate

YSL replica

corporate blogs are not hazard liberate.

The Sapphire Ice Sterling Silver CZ Eternity Band outlet

substantive is problematic to

I Love You" Sterling Silver 1 Carat CZ Heart Stud Earrings outlet

rear that know onions

The Almost Famous Sapphire Blue CZ Star Ring online sales

are lot companies that perform not credit component

replica jewelry

employees who blog C

replica Tiffany Bracelets jewelry

whether the van knows

replica tiffany jewelry

irrefutable or not. So, poop about sociable media

replica watches

and a blogging intention

roger dubuis sea more replica

should factor undertaken by

replica watches 020

faultless companies, horizontal those