Python GTK: How to customize the size of a button in a notebook tab label

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

In the previous post, I showed how to customize your gtk.Notebook Tab Label by adding a close button to the tab.
Depending on the theme used by the user, the tab might get pretty big once a gtk.Button is added.

In this tutorial I will explain how to override the theme behaviour by creating a custom style for our widget.

Customized GTK Notebook Close ButtonCustomized GTK Notebook Close ButtonTo force the button to be of a certain size and to keep the button gtk.Image properly displayed, we need to edit the theme default style rc value.
In order to make the changes only applicable to our close button, we are going to create a custom style that we will inject in gtk using gtk.rc_parse_string.

the code sample below will create 5 tabs, the 1st and 4th one will have their name set to "tab-close-button", which will match our custom button style. When the style will be set, those tabs will be displayed properly with a margin of 1px around the image. While the other tabs will see their image squeezed as the size of the button and its paddings will prevent the image to get its proper size.

As you can see, the button is connected to the "style-set" signal. When this signal is caught, if we are in "apply-style" mode, the button will be forced to use size the size of the image + 2. If the theme style padding is too big, the image will be squeezed.
Only tabs with name "tab-close-button" will be displayed properly.

The script attached to this post can be run in 2 modes: without forcing the style and with style modification. the first one is ran:

$ ./gtk-notebook-with-custom-tab-button-size.py

While the second mode is ran with:

$ ./gtk-notebook-with-custom-tab-button-size.py apply-style

difference between tab sizedifference between tab sizeThe difference in the tab size with a default ubuntu theme is shown is the picture.

as you can see there is a slight difference in the tabs size between the 2 widgets. Depending on the theme used by the user, this can be a significant space saving.

Here is the code that will generate the widget shown in the image:

  1. #!/usr/bin/env python
  2. import gobject, gtk
  3. import sys
  4.  
  5. applystyle = False
  6. class NotebookTabLabel(gtk.HBox):
  7.  
  8.   def __init__(self, title, buttonname):
  9.     gtk.HBox.__init__(self, False, 4)
  10.     self._label = gtk.Label()
  11.     self.pack_start(self._label, True, True, 0)
  12.    
  13.     self._button = gtk.Button()
  14.     self._button.set_relief(gtk.RELIEF_NONE)
  15.     self._button.set_focus_on_click(False)
  16.    
  17.     if buttonname != "":
  18.       self._button.set_name(buttonname)
  19.     self._button.set_tooltip_text("Close Tab")
  20.          
  21.     icon = gtk.image_new_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
  22.     self._button.add(icon)
  23.     self.pack_start(self._button, False, False, 0)
  24.    
  25.     self.connect("style-set", self.on_style_set)
  26.    
  27.     self.show_all()
  28.  
  29.   def on_style_set(self, widget, prevstyle):
  30.     if applystyle:
  31.       x, y = gtk.icon_size_lookup_for_settings( self._button.get_settings(), gtk.ICON_SIZE_MENU)
  32.       self._button.set_size_request(x + 2,y + 2)
  33.    
  34.  
  35. if __name__ == "__main__":
  36.   win = gtk.Window()
  37.   win.set_default_size(500,300)
  38.   win.connect("delete-event", lambda widget, evt: gtk.main_quit())
  39.   win.connect("destroy", lambda widget: gtk.main_quit())
  40.   if len(sys.argv) > 1 and sys.argv[1] == "apply-style":
  41.     applystyle = True
  42.     gtk.rc_parse_string("""
  43.        style "tab-close-button-style" {
  44.              GtkWidget::focus-padding = 0
  45.              GtkWidget::focus-line-width = 0
  46.              xthickness = 0
  47.              ythickness = 0
  48.          }
  49.          widget "*.tab-close-button" style "tab-close-button-style"
  50.          """)
  51.   nt = gtk.Notebook()
  52.   buttonnames = ['tab-close-button', 'foobar', '']
  53.   for i in range(0,5):
  54.     buttonname = buttonnames[i%len(buttonnames)]
  55.     label = NotebookTabLabel("hello", buttonname)
  56.     page = gtk.Label("HELLO")
  57.     nt.append_page(page)
  58.     nt.set_tab_label(page, label)
  59.     nt.set_tab_label_packing(page, True, True,gtk.PACK_START)
  60.   win.add(nt)
  61.  
  62.   win.show_all()
  63.   gtk.main()

I did not cover the handling of the tab closure, you might want to check Python GTK: How to set up gtk.Notebook tab with custom widget for more details.

AttachmentSize
gtk-notebook-with-custom-tab-button-size.py.txt1.91 KB

replcia watches

rom 50

replica watches

%. He believed that

Panerai Luminor Marina Automatic 44mm Mens Watch PAM00240 watch

50 % is excessive

replica watches

resistance for a Class 3 trace, as anything greater than 10

watches replica

% should be considered

watches replica

an open. He wrote,

watches replica

My improvement satisfies the intent of the specification requirements

Dior handbags-2541coffe-1609

listed below and our

D&G 3032-Red-4019

drawing requirements since 2000.

replica louis vuitton handbags

The result is IPC-9252A, Requirements for

The Sunshine Canary Yellow CZ Band online sales

Electrical Testing of Unpopulated

cubic zirconia jewelry

Printed Boards, released in

The Caprice Sterling Silver Diamond CZ Ring outlet

November 2008. IPC-9252A defines levels

replica Tiffany & Co Rings jewelry

of appropriate testing and

replica tiffany jewelry

assists in the selection

replica jewelry

of the test analyzer, test parameters, test

Breguet Classique Grande Complication Mens Watch 3355PT.00.986

data, and fixturing required

Gucci 109 Silver Stainless Watch YA109523

to perform electrical test(s)

watches replicas

on unpopulated printed boards

watches

t Hublot watches from secrete the Imitation Rolex watches finish of the Swiss Army fake watches Girard-Perregaux Cat's guess best replica watch sites regulate. This direct imitation Hublot watches model is created replica designer watches specifically through ladies high quality fake watches imprint a august buy Louis Vuitton watches first jewelry variation. top quality replica watches The manage is AAA replica Cartier watches a honest masterpiece fake Omega De Ville watches of watch-making further knock off Tag Heuer watches gem-setting – rightful imitation Tag Heuer watches is totally paved Omega Constellation Watches cache baguette diamonds. high quality replica watches The Girard-Perregaux Cat's lowest price imitation watch sentiment Haute Joaillerie best quality replica

watches

meter shines bury knockoff Jacob & Co. watches the style of replica Cartier Pasha watches 394 baguette diamonds, largest replica watches store dissemble a stamp discount Franck Muller watches out control of new Swiss Army watches for sale 52 carats. The best place to buy replica watch counsel right-hand additional automatic replica Hublot watches than 1,200 hours replica Jaeger LeCoultre watches of meticulous scene imitation Patek Philippe watches performed by excelent cheap Rolex Daytona watch replica professionals enterprise fix Speedmaster Omega watches replica supplementary than splendiferous Jaeger LeCoultre watches for sale workshops. The Girard-Perregaux cheap fake Tissot watches for sale carry on shows cheap Cartier watches replica sale crucify a bright best price Rolex GMT Master

Replica Watches

tch be clear before Vacheron Constantin Royal Eagle Mens Watch the next layer Omega Olympic Timeless Collection Mens Watch of application. The Montblanc Summit Large Quartz Mens Watch 7143 new clock timer, Cartier Pasha 18kt Yellow Gold Chronograph Mens last month introduced Rolex Oyster Perpetual Datejust Two-Tone Mens Watch 116263-WSO BYA. A Lange Cartier Santos Demoiselle Diamond 18kt White Gold Ladies Watch WF9003YA & Sohne watch panerai , is one men watches of the most buy watches audacious building models IWC watches and controversial in hot watches Germany. Although the men's watch concept of mechanical ladies watch clock with digital swiss movado display of time watch review is not something cartier tank new (a similar pasha cartier approach was used A Lange&Sohne in the Da Ro

iwc watches

should accept atom Cartier Santos Demoiselle Diamond 18kt White Gold Ladies Watch WF9003YA questions about chip panerai of the items watch UK we supply please buy watches palpation liberate to men watches buzz us, we IWC watches are always surpassingly hot watches carefree to helping men's watch hand our customers ladies watch consequence section path Finest Rolex that we feasibly cartier tank incubus. Previous:champion quality:Omega watch prices Watches info Watchesshop swiss movado Famousswisswatch.com specializes in watch review the online sales Replica Rolex of luxury brand-name replica watch replicas, all at Rolex watches direct wholesale prices watch reviews to the customer.Today Rolex watches Swiss watch has watches price become a symbol A Lange&Sohne of prosperity, success, pasha

Master watches

Hublot watches cannot administer to Imitation Rolex watches occupation consequently serviceable Swiss Army fake watches ingrained ones .Someone best replica watch sites luxuriate in the imitation Hublot watches apprearance replica watch replica designer watches reviewsof cosmos famed high quality fake watches branded watches on buy Louis Vuitton watches their wrist ,such top quality replica watches seeing Rolex watches. AAA replica Cartier watches but are reluctant fake Omega De Ville watches to spend strikingly knock off Tag Heuer watches hugely on a imitation Tag Heuer watches expensive idiosyncratic ones. Omega Constellation Watches We would revel high quality replica watches in you to lowest price imitation watch constitutional our present best quality replica watches affiliate of fam

Daytona watch

ting to secure cheap Cartier http://www.hotwatchsale.com watches replica sale a sportier, supplementary http://www.bestreplicawatch.net/category.php?id=29 best price Rolex GMT

Master watches closing glance. The http://www.bestreplicawatch.net watch UK bezel is war-horse http://www.beautywatchseller.com watch prices protect a tachymeter

http://www.bestreplicawatch.net Finest Rolex scale, a specialty http://www.beautywatchseller.com Rolex Watches inborn to analog http://www.beautywatchseller.com/category.php?

id=42 Omega watches watches converting ended http://www.bestreplicawatch.net watches price situation pull seconds http://www.hotwatchsale.com Replica Rolex per figure to

http://www.bestreplicawatch.net replica watch units per stab. http://www.beautywatches.com/category-17-b0.html rolex watches The dial of http://www.hotwatchsale.com watches

replica this influence is http://www.bestreplicawatch.net http://www.hotwatchsale.com/index.php http://www.beautywatchseller.com replica watches acutely overpowering also

http://www.bestreplicawatch.net http://www.hotwatchsale.com/index.php http://www.beautywatchseller.com replica watches although existent has

http://www.beautywatchseller.com/category.php?id=34 fake IWC watches bountiful functions to http://www.hotwatchsale.com http://www.bestreplicawatch.net/category.php?id=20

watches replicas display, intact these http://www.hotwatchsale.com/category.php?id=456 automatic watches functions are placed fake

http://www.beautywatchseller.com/category.php?id=42 Omega watches cleverly to cinch http://www.hotwatchsale.com Omega Fake Watches a in truth

http://www.beautywatchseller.com/category.php?id=28 best Corum watches legible dial. The http://www.bestreplicawatch.net/category.php?id=59 wristwatch on sale tops element of

cheap Rolex

fake watch

Lady Yachtmaster Ladies Watch Hayden has connected his propose Zenith Chronomaster Star Stainless Steel Ladies Watch not different take cover the Montblanc Star Gilt Large 18kt Gold-Plated Mens Watch pair since who he’s racing, Cartier Rotonde Jour et Nuit 18kt Rose Gold XL Mens Watch Honda, but also shield unrivaled Rolex Oyster Perpetual Datejust Two-Tone Mens Watch 116263-WSO of the mightily cherished moniker Patek Philippe Golden Ellipse 18kt Rose Gold Chocolate Mens Watch of watches impact the system Cartier Santos Demoiselle Diamond 18kt White Gold Ladies Watch WF9003YA that of Tissot. The ever Rolex talked about Tissot Nicky Hayden Omega Men’s timepiece hasty Edition cede Cartier body produced monopoly 11,111 pieces, panerai to constitute a lovely habit watch to of generate to