简体   繁体   中英

Gtk Notebook tab size

I am using a gtkNotebook to show a number of widgets on the bottom of my app. The problem is that when the tabs are showing they take up a lot of space and look awkward in general. I figured out that it is cause by the gtk.ICON_SIZE_MENU being bigger that the text, but I can't find any constants that are smaller and I don't want to give it a exact pixel size since it may mess up on different screen resolutions. Is the any way to get the button to always scalse to the size of the text on the label next to it?

Here is the code that generates the button (the hbox it's in is the widget that the tab displays):

    box = gtk.HBox(False,0)
    btn = gtk.Button()
    image = gtk.Image()
    image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
    btn.set_image(image)
    btn.set_relief(gtk.RELIEF_NONE)
    btn.show()

    if type(label) != type(gtk.Label()):
        label = gtk.Label('Untitled')
    box.pack_start(label)
    box.pack_end(btn)

If you just want to make the Close buttons smaller, have a look at how—for example—Epiphany removes most of the padding around the button.

Result:

主显节中的选项卡,来自http://projects.gnome.org/epiphany/images/customizable.png

(This is a slightly older version of their code; Ephy trunk uses the GTK+ 3.0 CSS styling, but same idea.)

I guess what you could do is

  1. supply a custom widget for your notebook tab labels via set_tab_label
  2. use set_size_request to set label widget's size

See if an example below would work for you:

import gtk
import sys;

class TestNotebook(gtk.Notebook):
    def __init__(self):
        gtk.Notebook.__init__(self)

    def add_new_tab(self, icon):
        image = gtk.Image()
        image.set_from_stock(icon, gtk.ICON_SIZE_DIALOG)
        image.show_all()        

        tab_image = gtk.Image()
        tab_image.set_from_stock(icon, gtk.ICON_SIZE_MENU)

        box = gtk.HBox()
        box.pack_start(tab_image, False, False)
        box.pack_start(gtk.Label(icon), True, True)
        # set tab size here
        box.set_size_request(50, 50)        
        box.show_all()

        self.set_current_page(self.append_page(image))
        self.set_tab_label(image, box)

if __name__ == '__main__':
    notebook = TestNotebook()
    notebook.add_new_tab(gtk.STOCK_ABOUT)
    notebook.add_new_tab(gtk.STOCK_ADD)
    notebook.add_new_tab(gtk.STOCK_APPLY)

    box = gtk.VBox()
    box.pack_start(notebook)

    window = gtk.Window()
    window.resize(600, 400)
    window.add(box)
    window.show_all()

    gtk.main()
    sys.exit(0)

hope this helps, regards

More up to date answer of @erge_gubenko, when you're using a newer Python/Gtk version:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
import sys

class TestNotebook(Gtk.Notebook):
    def __init__(self):
        Gtk.Notebook.__init__(self)

    def add_new_tab(self, icon):
        image = Gtk.Image()
        image.set_from_icon_name(icon, Gtk.IconSize.DIALOG)
        image.show_all()        

        tab_image = Gtk.Image()
        tab_image.set_from_icon_name(icon, Gtk.IconSize.MENU)

        label = Gtk.Label(icon) # Deprecated
        box = Gtk.HBox()
        box.pack_start(tab_image, False, False, 2)
        box.pack_start(label, True, True, 2)
        # set tab size here
        box.set_size_request(50, 50)        
        box.show_all()

        self.set_current_page(self.append_page(image))
        self.set_tab_label(image, box)

if __name__ == '__main__':
    notebook = TestNotebook()
    notebook.add_new_tab(Gtk.STOCK_ABOUT)
    notebook.add_new_tab(Gtk.STOCK_ADD)
    notebook.add_new_tab(Gtk.STOCK_APPLY)

    box = Gtk.VBox()
    box.pack_start(notebook, True, True, 2)

    window = Gtk.Window()
    window.resize(600, 400)
    window.add(box)
    window.show_all()

    Gtk.main()
    sys.exit(0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM