Python GTK: How to set up gtk.Notebook tab with custom widget -- page 2
Articles may may have files attached at the end of the post
Now, let's go over the code and see what happens.
From line 1 to 10, we import the required module to run the script and we define to global variables: window, the main window, and notebook, our customized gtk notebook.
Line 10, we define our class MyNotebook which inherit from gtk.Notebook.
Line 12 to 18, we initialized our Notebook. By setting the property "shows-tabs" to False, we make sure that a tab is not going to be visible to the user. We will set it to True later when the number of tabs will be greater than 1.
- 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)
Line 19 to 41, this is the method that is going to be called when a new tab is created.
Up to line 25, we generate and append an image to the notebook.
Line 29-30, we make the tabs visible if there is more than 1.
35-41, we create our customized tab label widget, set it as a tab label and finally, give focus to the new tab.
- 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)
Line 43 to 59 is the method creating the tab label and connecting the close button to our close_tab.
Our tab is basically a gtk.HBox in which we pack:
- An image on the left side which will match the content of the tab
- The title of the tab
- A close button on the right side
- 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)
The close button is hooked to the signal clicked and pass the child of the tab as a user parameter so we can find the tab back and close it.
The last method (line 61-68) of our class MyNotebook is the callback invoked when a "clicked" signal is intercepted.
By using the child of the tab, we can find the page number. Then, we remove the page and, if there is only one tab left, we hide the tab label to the user.
the rest of the code, simply handle the closure of the top level window along with the initialization of the widget.
hope this helped
Similar entries
- Python GTK: How to set up gtk.Notebook tab with custom widget
- Python GTK: How to customize the size of a button in a notebook tab label
- Python GTK: Treeview with rows of different colors -- page 2
- Python GTK: Treeview with rows of different colors
- Python: Embedding a Virtual Terminal in a GTK widget with python vte library













gucci handbag
dismissed the
IWC Portuguese F.A. Jones Automatic replica watch
charges that NHLBI violated
New Replica Rolex watches
the Shelby amendment. Lawsuit
exact replicas
Appeal (added Jan. 25, 2005) The
Fake rolex
Salt Institute and the
Breitling Professional Aerospace Mens Watch E79362 replica watches
U.S. Chamber of Commerce
Replica Breguet watch
filed an appeal Jan. 11 that would review the U.S. District
replica knockoffs fake handbags
Court decision which found
replica handbags YSL Muse Tote Dark Coffee for sale
that the plaintiffs lacked
replica handbags Balenciaga "Giant City" Bag Ruby 173084 for sale
legal standing in the case, and it also
replica Fendi Fendi handbags bags
determined that the DQA
cheap replica bags
is not judicially reviewable.
replica bags
"Our appeal is for more transparency in the
New Replica 2009 New Tiffany & Co Necklaces20090825014
use of science," claimed
Tiffany & Co Rings jewelry
Richard L. Hanneman, President
replica tiffany jewelry
of the Salt Institute, "and we are asking the
watch replica
court to banish the
cheap Panerai Radiomir Mechanical PANE-113-759 watch replica
games-playing and data manipulation
cheap Blancpain 36mm Mechanical BP-2-28 watch replica
that has compromised implementation o