PyGTK:如何使图像自动缩放以适合其父窗口小部件? [英] PyGTK: How do I make an image automatically scale to fit its parent widget?

查看:79
本文介绍了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?

推荐答案

您可以使用widget.get_allocation()来查找父窗口小部件的大小,并使用pixbuf.scale_simple来缩放图像,如下所示:

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:

allocation = parent_widget.get_allocation()
desired_width = allocation.width
desired_height = allocation.height

pixbuf = gtk.gdk.pixbuf_new_from_file('your_image.png')
pixbuf = pixbuf.scale_simple(desired_width, desired_height, gtk.gdk.INTERP_BILINEAR)
image = gtk.image_new_from_pixbuf(pixbuf)

如果要在每次调整窗口大小时缩放图像,则必须在连接到size_allocate信号的函数中的上方放置代码(或类似的方法,以避免每次从磁盘加载图像).父小部件.为避免无限循环,请确保您在小部件中放置的图像不会再次更改其大小.

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.

参考:

  • How to resize an image (I think you already visited that)
  • About the "resize" event
  • Other link about resizing, in Stack Overflow

这篇关于PyGTK:如何使图像自动缩放以适合其父窗口小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆