我GtkImage在一个可调整大小的窗口中有一个小部件和一个GdkPixBuf存储我想要填充的图像的引用GtkImage.
我可以使用此方法缩放GdkPixBuf以填充GtkImage小部件:
def update_image(self, widget=None, data=None):
# Get the size of the source pixmap
src_width, src_height = self.current_image.get_width(), self.current_image.get_height()
# Get the size of the widget area
widget = self.builder.get_object('image')
allocation = widget.get_allocation()
dst_width, dst_height = allocation.width, allocation.height
# Scale preserving ratio
scale = min(float(dst_width)/src_width, float(dst_height)/src_height)
new_width = int(scale*src_width)
new_height = int(scale*src_height)
pixbuf = self.current_image.scale_simple(new_width, new_height, gtk.gdk.INTERP_BILINEAR)
# Put the generated pixbuf in the GtkImage widget
widget.set_from_pixbuf(pixbuf)
Run Code Online (Sandbox Code Playgroud)
当我update_image手动调用时,它按预期工作.现在我希望在调整GtkImage小部件时自动进行缩放.我带来的最佳解决方案是将update_image方法绑定到configure-event窗口的GTK事件.在窗口的每次尺寸改变之后,图像确实被适当地缩放.但是我对此解决方案有两个问题:
我很抱歉这个小问题的长期解释,我希望你能帮助我.
ser*_*nko 10
我相信你可以使用widget的expos-event信号进行图像缩放.另外,将图像添加到可滚动容器中可以解决窗口调整大小的问题.请检查下面的示例是否适合您.
import gtk
class ScaleImage:
def __init__(self):
self.temp_height = 0
self.temp_width = 0
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
image = gtk.Image()
image.set_from_file('/home/my_test_image.jpg')
self.pixbuf = image.get_pixbuf()
image.connect('expose-event', self.on_image_resize, window)
box = gtk.ScrolledWindow()
box.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
box.add(image)
window.add(box)
window.set_size_request(300, 300)
window.show_all()
def on_image_resize(self, widget, event, window):
allocation = widget.get_allocation()
if self.temp_height != allocation.height or self.temp_width != allocation.width:
self.temp_height = allocation.height
self.temp_width = allocation.width
pixbuf = self.pixbuf.scale_simple(allocation.width, allocation.height, gtk.gdk.INTERP_BILINEAR)
widget.set_from_pixbuf(pixbuf)
def close_application(self, widget, event, data=None):
gtk.main_quit()
return False
if __name__ == "__main__":
ScaleImage()
gtk.main()
Run Code Online (Sandbox Code Playgroud)
希望这有帮助,问候
| 归档时间: |
|
| 查看次数: |
7234 次 |
| 最近记录: |