Gtk Notebook 标签大小
我在用gtkNotebook来显示我应用底部的一些小部件。问题是,当标签页显示出来时,它们占了很多空间,看起来也不太好。我发现这是因为gtk.ICON_SIZE_MENU
的大小比文字要大,但我找不到更小的常量,而且我不想给它一个具体的像素大小,因为在不同的屏幕分辨率下可能会出问题。有没有办法让按钮的大小总是和旁边标签的文字大小一致呢?
下面是生成按钮的代码(它所在的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)
3 个回答
0
这是@erge_gubenko的更新回答,适用于你使用较新版本的Python/Gtk时:
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)
1
我想你可以这样做:
- 通过 set_tab_label 提供一个自定义的小部件来设置你的笔记本标签的名称。
- 使用 set_size_request 来设置标签小部件的大小。
看看下面的例子是否对你有帮助:
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)
希望这能帮到你,祝好。