在 GTK 中将图像缩放到其父按钮大小?

发布于 2024-08-13 18:32:55 字数 552 浏览 8 评论 0原文

我有一个 GTK 布局,HBox 左侧有一个小部件,决定我想要的最大高度,右侧有一个 VBox,包含三个按钮,每个按钮仅包含图像,不包含文本。这些图像是 GTK 库存图标,因此具有库存存储类型。

使用 Expand=True, fill=True 打包没有图像的按钮正是我想要的高度 - 一些任意的小宽度,以及 HBox 左半部分高度的 1/3。然而,对于图像,它们要么太高 - 3 * ICON_SIZE_MENU 在许多主题中太高 - 或者如果我强制按钮的高度请求,它们就会被剪裁。我宁愿将它们缩放到父按钮的大小。

我是否必须为我正在使用的每个库存图标制作自己的 pixbuf(如果大小发生变化则重新生成它们)?或者 GTK 可以自动调整图像大小以适合按钮,而不是相反?由于图像与我尝试过的每个主题相距仅几个像素,有没有办法禁用按钮的剪辑?

该应用程序是使用 PyGTK 用 Python 编写的,但任何语言的解决方案都值得赞赏。

(我尝试将其设置为图标名称存储类型,但其中一个图标是 REVERT_TO_SAVED,当我试图将其强制到特定的像素高度时,它变成了损坏的库存图像。另外,我不想将其强制为任何固定像素高度。)

I have a GTK layout with a widget on the left of an HBox deciding the maximum height I want, and a VBox on the right containing three buttons, each containing only an image and no text. The images are a GTK stock icon, and so have the stock storage type.

Using expand=True, fill=True packing the buttons without images are exactly the height I want - some arbitrary small width, and 1/3 each the height of the left half of the HBox. However with an image, they are either too tall - 3 * ICON_SIZE_MENU is too tall in many themes - or if I force the button's height request, they get clipped. I would rather scale them to the size of the parent button.

Do I have to make my own pixbufs for each of the stock icons I'm using (and regenerate them if the size changes)? Or can GTK automatically size the image to fit the button, rather than the other way around? Since the images are only a few pixels off from fitting in every theme I've tried, is there a way to just disable the button's clipping?

This application is written in Python using PyGTK, but solutions in any language are appreciated.

(I tried just making it an icon name storage type, but one of the icons is REVERT_TO_SAVED, which turned into the broken stock image when I tried to force it to a particular pixel height. Plus, I'd rather not force it to any fixed pixel height.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凤舞天涯 2024-08-20 18:32:55

没有办法自动做你想做的事。您可能想要子类化 gtk.Image 并在子类中将 pixbuf 缩放到小部件的分配大小。这样做的优点是您将拥有一个可重复使用的小部件,并且可以让它动态调整图像大小。

缺点是你会得到一个丑陋的、放大的像素图。您可能想研究可缩放矢量图形。

There's no way to automatically do you want you want. You might want to subclass gtk.Image and in your subclass, scale a pixbuf to your widget's allocation size. The advantage of this is that you'll have a reusable widget and you'll be able to have it resize your image on the fly.

The downside is that you'll have an ugly, scaled up pixmap. You may want to look into scalable vector graphics.

你又不是我 2024-08-20 18:32:55

我一直很幸运地使用 SVG 文件并缩放它们,因为将 SVG 合并到 pyGtk 程序中很容易。

img = gtk.gdk.pixbuf_new_from_file(fname)

我所做的是将 svg 复制到临时文件并修改第一个 '

然后我读到修改后的 svg,它将作为缩放的 pixbuf 加载,并具有所有 alpha 通道的优点。

请参阅 http://code.google.com /p/key-mon/source/browse/src/keymon/lazy_pixbuf_creator.py

I've been having luck using SVG files and scaling them since it's easy to incorporate SVG in pyGtk programs.

img = gtk.gdk.pixbuf_new_from_file(fname)

What I do is copy the svg to a temporary file and modify the first '

Then I read i that modified svg and it'll load as a pixbuf scaled and with all the alpha channel goodness.

see http://code.google.com/p/key-mon/source/browse/src/keymon/lazy_pixbuf_creator.py

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文