如何在我的应用程序中制作小库存图标



我的应用程序中有一个按钮,我希望它的尺寸为 23x23 像素,库存图标为 16x16 像素。

我想出了:

closeButton = gtk.ToolButton(gtk.STOCK_CLOSE)
closeButton.set_size_request(23, 23)

但是此代码仅更改按钮的大小,而不更改图标的大小:/

如何缩放按钮上的股票图标?有没有小版本的库存物品?类似于 gtk 的东西。STOCK_SMALL_CLOSE?

编辑这是测试创建小库存项目的可能性的示例程序

import gtk
class Tab(gtk.VBox):
    def __init__(self, caption):
        gtk.VBox.__init__(self)
        self.Label = self.__createLabel(caption)
        self.show_all()
    def __createLabel(self, caption):
        hbox = gtk.HBox()
        label = gtk.Label(caption)
        hbox.pack_start(label, True, True)
        closeButton = gtk.ToolButton(self._getCloseIcon())
        hbox.pack_start(closeButton, False, False)
        hbox.show_all()
        return hbox
    def _getCloseIcon(self):
        raise NotImplementedError
class TabWithNormalIcons(Tab):
    def __init__(self, caption):
        Tab.__init__(self, caption)
    def _getCloseIcon(self):
        return gtk.STOCK_CLOSE
class TabWithImage16Icons(Tab):
    def __init__(self, caption):
        Tab.__init__(self, caption)
    def _getCloseIcon(self):
        return gtk.image_new_from_stock(gtk.STOCK_CLOSE, 16)
class TabWithSmallToolbarIcons(Tab):
    def __init__(self, caption):
        Tab.__init__(self, caption)
    def _getCloseIcon(self):
        return gtk.image_new_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_SMALL_TOOLBAR)
class TabWithMenuIcons(Tab):
    def __init__(self, caption):
        Tab.__init__(self, caption)
    def _getCloseIcon(self):
        return gtk.image_new_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
class App(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
        self.connect('destroy', lambda event: gtk.main_quit())
        self.set_default_size(640, 480)
        notebook = gtk.Notebook()
        tabNumber = 1
        #Icon normal sizes
        for i in range(3):
            tab = TabWithNormalIcons('Tab %s' % str(tabNumber))
            notebook.append_page(tab, tab.Label)
            tabNumber += 1
        #Icons with 16 pixel Image
        for i in range(3):
            tab = TabWithImage16Icons('Tab %s' % str(tabNumber))
            notebook.append_page(tab, tab.Label)
            tabNumber += 1
        #Icons with small toolbar images
        for i in range(3):
            tab = TabWithSmallToolbarIcons('Tab %s' % str(tabNumber))
            notebook.append_page(tab, tab.Label)
            tabNumber += 1
        #Icons with menu images
        for i in range(3):
            tab = TabWithMenuIcons('Tab %s' % str(tabNumber))
            notebook.append_page(tab, tab.Label)
            tabNumber += 1
        self.add(notebook)
        self.show_all()
a = App()
gtk.main()

set_size_request仅定义您希望小部件具有的最小大小。所以事实上,它不会缩放孩子,当它的孩子需要这个时,它也不会阻止它的成长。如果是这样,那不是你想要的:它仍然会绘制大图标,但将其夹在按钮上。

所以你想要的是让你的库存物品有不同的尺寸。我认为最简单的是创建一个 gtk。图像,您可以在其中指定库存尺寸。然后将该图像小部件插入按钮而不是股票价值。

所以在代码中:

image = gtk.image_new_from_stock (gtk.STOCK_CLOSE, gtk.ICON_SIZE_SMALL_TOOLBAR)
closeButton = gtk.Button ()
closeButton.add (image)
closeButton.set_size_request (23, 23)

我有一个类似的问题,并在 C 中解决了这个问题,虽然我不是 Python 程序员,但我可以给你一个相关函数的提示,所以你也许能够完成你的任务:

首先,您使用 gtk 从库存图标创建一个像素。Widget.render_icon。然后,通过gtk.gdk.Pixbuf.scale_simple,您可以将其大小调整为所需的任何大小。最后一步,您可以从此pixbuf创建图像并将其用于按钮。

使用相应的 C 函数非常适合我。

最新更新