PyGTK:如何使图像自动缩放以适合其父窗口小部件?
我有一个 PyGTK 应用程序需要加载未知大小的图像,但是我遇到的问题是,如果图像非常大或非常小,窗口布局会变得扭曲并且难以使用。我需要某种方法使图像自动缩放以适合其父窗口小部件。不幸的是,经过一些研究后,似乎没有内置或其他代码可以满足我的要求。
我怎样才能写一些东西来做到这一点?我本以为有人已经为此编写了一些代码;有什么我错过的吗?
I have a PyGTK app that needs to load an image of unknown size, however I am having the problem that if the image is either very big or very small, the window layout becomes distorted and hard to use. I need some way of making the image automatically scale to fit its parent widget. Unfortunately, after doing some research, it seems there is no code, built in or otherwise, that does what I'm looking for.
How could I go about writing something to do this? I would have thought that somebody would have written some code for this already; is there something that I missed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 widget.get_allocation() 找出父窗口小部件的大小,并使用 pixbuf.scale_simple 缩放图像,如下所示:
如果您希望每次调整窗口大小时图像都缩放,则必须将上面的代码(或类似的代码,以避免每次都从磁盘加载图像)在连接到父窗口小部件的 size_allocate 信号的函数中。为了避免无限循环,请确保放入小部件中的图像不会再次改变其大小。
参考文献:
You can use widget.get_allocation() to find out the size of the parent widget and pixbuf.scale_simple to scale the image, like this:
If you want the image to scale each time the window is resized, you'll have to put the code above (or something similar, to avoid loading the image from disk every time) in a function connected to the size_allocate signal of the parent widget. To avoid infinite loops, make sure that the image you put in the widget doesn't alter its size again.
References:
下面是在绘图区域完成此任务的一个例外:
Here is an except that accomplishes this task on a drawing area:
这里有一些小片段类,允许您使用自动缩放图像。
和小用法示例:
Here some small snippet class which allows you to use auto scaled image.
And small usage example: