[英]Gtk Notebook tab size
我正在使用 gtkNotebook 在我的应用程序底部显示许多小部件。 问题是,当标签显示它们占用很多空间时,总体上看起来很尴尬。 我发现这是因为gtk.ICON_SIZE_MENU
比文本大,但我找不到任何更小的常量,我不想给它一个确切的像素大小,因为它可能会在不同的屏幕上混乱决议。 有什么方法可以让按钮始终缩放到旁边 label 上的文本大小?
这是生成按钮的代码(它所在的 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)
我想你能做的是
查看下面的示例是否适合您:
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)
希望这会有所帮助,问候
当您使用较新的 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.