开发者

Scaling an image to its parent button size in GTK?

开发者 https://www.devze.com 2022-12-13 03:16 出处:网络
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

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 s开发者_开发知识库olutions 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.)


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消