2015-04-16 190 views
1

所以我试图做一个GTK应用程序使用python,我遇到了这个问题后,我把一个图像放在一个窗口,我可以增加窗口的大小,但不会减少它。鉴于这个特定窗口的目的是显示可调整大小的图像,这相当麻烦。pyGtk:图像防止窗口大小调整

我已经提取如果你没有管,调整窗口大小罚款这表明下方

#!/usr/bin/env python 
from gi.repository import Gtk, GdkPixbuf 
import sys 

class ImageWindow(Gtk.Window): 
    def __init__(self, image_data): 
     Gtk.Window.__init__(self, title="image test") 
     if image_data and len(image_data) > 0: 
      self.loader = GdkPixbuf.PixbufLoader() 
      self.loader.write(image_data) 
      self.pixbuf = self.loader.get_pixbuf() 
      self.image = Gtk.Image.new_from_pixbuf(self.pixbuf) 
     else: 
      self.image = Gtk.Image.new() 
     self.add(self.image) 
     self.connect('delete-event', Gtk.main_quit) 

win = ImageWindow(sys.stdin.read()) 
win.show_all() 
Gtk.main() 

这种行为相关的代码。图像中的管道,窗体点击图像的大小,可以调整大一些,但不能调整较小。

回答

3

所以这里是一个缩放图像的例子。我们的想法是,你从装载机和规模把图像中的Gtk.ScrolledWindow(),并尽快调整窗口大小:

#!/usr/bin/env python 
from gi.repository import Gtk, GdkPixbuf, GLib 
import sys 

class ImageWindow(Gtk.Window): 
    def __init__(self, image_data): 
     Gtk.Window.__init__(self, title="image test") 
     self.connect('delete-event', Gtk.main_quit) 

     self.image = Gtk.Image() 
     scrolled_window = Gtk.ScrolledWindow() 
     scrolled_window.add(self.image) 
     self.add(scrolled_window) 

     if len(image_data) == 0: 
      return 

     self.loader = GdkPixbuf.PixbufLoader() 
     self.loader.write(image_data) 
     self.loader.close() 
     self.pixbuf = self.loader.get_pixbuf() 
     self.image.set_from_pixbuf(self.pixbuf) 

     width = self.pixbuf.get_width() 
     height = self.pixbuf.get_height() 
     self.dimension = float(width)/height 
     self.set_default_size(width, height) 
     self.connect('check-resize', self.on_resize) 

    def on_resize(self, window): 
     width, height = self.get_size() 
     if float(width)/height > self.dimension: 
      self.pixbuf = self.pixbuf.scale_simple(
       self.dimension * height, 
       height, 
       GdkPixbuf.InterpType.NEAREST 
      ) 
     else: 
      self.pixbuf = self.pixbuf.scale_simple(
       width, 
       width/self.dimension, 
       GdkPixbuf.InterpType.NEAREST 
      ) 
     GLib.idle_add(self.image.set_from_pixbuf, self.pixbuf) 

win = ImageWindow(sys.stdin.read()) 
win.show_all() 
Gtk.main() 

作为替代调整图像大小,你可以加载的pixbuf再次它之后。这看起来更好,如果你让你的图像变小,然后再大,但需要更多的处理:

def on_resize(self, window): 
    width, height = self.get_size() 
    self.pixbuf = self.loader.get_pixbuf() 
    if float(width)/height > self.dimension: 
     self.pixbuf = self.pixbuf.scale_simple(
      self.dimension * height, 
      height, 
      GdkPixbuf.InterpType.BILINEAR 
     ) 
    else: 
     self.pixbuf = self.pixbuf.scale_simple(
      width, 
      width/self.dimension, 
      GdkPixbuf.InterpType.BILINEAR 
     ) 
    GLib.idle_add(self.image.set_from_pixbuf, self.pixbuf) 
+0

但是......'AttributeError的:“gtk.Window”对象有没有属性“dimension'' :( – yPhil

+0

'dimension'不是'Gtk.Window'的属性,它是派生类的一个属性,用于简单地存储图片的维度,或者,当连接到'check-resize'时,可以将其作为用户数据传递:'' self.connect('check-resize',self.on_resize,dimension)'。 – elya5

相关问题