【问题标题】:pyGtk: Image prevents window resizepyGtk:图像防止窗口调整大小
【发布时间】:2015-04-16 05:19:30
【问题描述】:

所以我正在尝试使用 python 制作一个 GTK 应用程序,我遇到了这个问题,在我将图像放在窗口上之后,我可以增加窗口的大小,但不能减小它。鉴于此特定窗口的目的是显示可调整大小的图像,这相当麻烦。

我在下面提取了演示此行为的相关代码

#!/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()

如果您不输入任何内容,则窗口会很好地调整大小。用管道插入图片,表单点击图片大小,可以变大,但不能变小。

【问题讨论】:

    标签: python image user-interface gtk pygtk


    【解决方案1】:

    所以这里是一个缩放图像的例子。这个想法是你将图像放在 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)
    

    【讨论】:

    • 但是... AttributeError: 'gtk.Window' object has no attribute 'dimension' :(
    • dimension 不是Gtk.Window 的属性。简单存储图片的尺寸是派生类的一个属性。或者,可以在连接到 check-resize 时将其作为用户数据传递,如下所示:self.connect('check-resize', self.on_resize, dimension)
    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    相关资源
    最近更新 更多