简体   繁体   English

Gtk 笔记本标签大小

[英]Gtk Notebook tab size

I am using a gtkNotebook to show a number of widgets on the bottom of my app.我正在使用 gtkNotebook 在我的应用程序底部显示许多小部件。 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.我发现这是因为gtk.ICON_SIZE_MENU比文本大,但我找不到任何更小的常量,我不想给它一个确切的像素大小,因为它可能会在不同的屏幕上混乱决议。 Is the any way to get the button to always scalse to the size of the text on the label next to it?有什么方法可以让按钮始终缩放到旁边 label 上的文本大小?

Here is the code that generates the button (the hbox it's in is the widget that the tab displays):这是生成按钮的代码(它所在的 hbox 是选项卡显示的小部件):

    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. 如果您只是想使“关闭”按钮变小,请看一下[Epiphany]如何删除按钮周围的大多数填充。

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.) (这是他们代码的稍旧版本; Ephy干线使用GTK + 3.0 CSS样式,但思路相同。)

I guess what you could do is 我想你能做的是

  1. supply a custom widget for your notebook tab labels via set_tab_label 通过set_tab_label为您的笔记本标签标签提供自定义小部件
  2. use set_size_request to set label widget's size 使用set_size_request设置标签小部件的大小

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:当您使用较新的 Python/Gtk 版本时,@erge_gubenko 的最新答案:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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