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 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:
- #!/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)
Now let go over the code and see what happens.
| Attachment | Size |
|---|---|
| gtk-notebook-with-custom-tab.py.txt | 2.63 KB |
Similar entries
- Python GTK: How to set up gtk.Notebook tab with custom widget -- page 2
- Python GTK: How to customize the size of a button in a notebook tab label
- Python: Embedding a Virtual Terminal in a GTK widget with python vte library
- Python GTK: Treeview with rows of different colors
- Python GTK: Treeview with rows of different colors -- page 2













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