我可以使用此方法缩放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)
当我手动调用update_image时,它按预期工作.现在我希望在调整GtkImage小部件时自动进行缩放.我带来的最佳解决方案是将update_image方法绑定到窗口的configure-event GTK事件.在窗口的每次尺寸改变之后,图像确实被适当地缩放.但是我对此解决方案有两个问题:
>我只能让窗户更大.一旦图像被放大,GTK将不会让我调整窗口的大小,因为窗口不能小于其窗口小部件.我明白为什么它会这样,但我不知道如何改变这种行为.
>也许这没什么大不了的,但我更喜欢GtkImage小部件而不是顶级窗口的事件.
我很抱歉这个小问题的长期解释,我希望你能帮助我.
解决方法 我相信你可以使用小部件的 expose-event信号进行图像缩放.另外,将图像添加到可滚动容器中可以解决窗口调整大小的问题.请检查下面的示例是否适合您.import gtkclass 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,data=None): gtk.main_quit() return Falseif __name__ == "__main__": ScaleImage() gtk.main()
希望这有帮助,问候
总结以上是内存溢出为你收集整理的使用(Py)GTK调整大小时自动缩放图像全部内容,希望文章能够帮你解决使用(Py)GTK调整大小时自动缩放图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)