python - 动态调整图像大小 pyGTK (python)

标签 python image resize gtk pygtk

我被困在我不明白的问题上。主程序运行自

if __name__ == "__main__":
    HelloWorld()
    gtk.main()

里面HelloWorld i 类有两个信号:

self.button.connect("file-set", self.load_image)
self.window.connect("check-resize", self.resize_image)

它们在这里:

def load_image(self, widget):
    self.image_loc = self.button.get_filename()
    pixbuf = gtk.gdk.pixbuf_new_from_file(self.image_loc)

    self.resize_image
    print "image loaded"

def resize_image(self, widget):
    allocation = self.scrolledwindow.get_allocation()
    win_h = float(allocation.height)
    win_w = float(allocation.width)
    wk = round(float(win_h / win_w), 6)

    if self.image_loc is not None:
        pixbuf = gtk.gdk.pixbuf_new_from_file(self.image_loc)

        image_h = float(pixbuf.get_height())
        image_w = float(pixbuf.get_width())
        ik = round(float(image_h / image_w), 6)

        if image_h <= win_h and image_w <= win_w:
            pixbuf = pixbuf.scale_simple(int(image_w), int(image_h), gtk.gdk.INTERP_BILINEAR)
        elif (image_h > win_h and image_w <= win_w) or (image_h > win_h and image_w > win_w and ik >= wk):
            pixbuf = pixbuf.scale_simple(int((win_h - 30) * (1 / ik)), int(win_h) - 30, gtk.gdk.INTERP_BILINEAR)
        elif (image_h <= win_h and image_w > win_w) or (image_h > win_h and image_w > win_w and ik < wk):
            pixbuf = pixbuf.scale_simple(int(win_w) - 30, int((win_w - 30) * ik), gtk.gdk.INTERP_BILINEAR)
        else:
            print "WTF? Incorrect image size calculation"
        self.image.set_from_pixbuf(pixbuf)

    print "window resized"

虽然加载图像正常并且调整大小也可以,但我需要 Ctrl+C每次我调整窗口大小时。为什么?据我发现,问题位于 set_from_pixbuf() 内部。方法,因为如果我删除它,我会得到“图像已加载”和“窗口大小调整”打印而无需循环。 回溯:

window resized
window resized
image loaded
window resized
[...lots of prints...]
window resized
^CTraceback (most recent call last):
  File "./photgal.py", line 229, in resize_image
    pixbuf = gtk.gdk.pixbuf_new_from_file(self.image_loc)
KeyboardInterrupt
window resized
[...lots of prints...]
window resized
window resized
^CTraceback (most recent call last):
  File "./photgal.py", line 229, in resize_image
    pixbuf = gtk.gdk.pixbuf_new_from_file(self.image_loc)
KeyboardInterrupt

更新 来自this来源:You can easily get into infinite loops doing this type of thing though.据我了解,这是我的情况,建议是 The "right way" to调整一个小部件的大小,同时调整另一个小部件的大小 is usually to write a custom container widget that sizes things the way you want. 。这个容器怎么写?

最佳答案

resize_image() 是一个无限循环。因为如果窗口收到 check-resize 信号,则会调用 resize_image(),并且重新渲染图像,并再次发出另一个 check-resize 信号....

所以我们需要一些技巧来打破这一点。

我在这里编写了一个小型演示应用程序,https://github.com/LiuLang/gtk-test/tree/master/resize-image ,这解决了这个问题。

关于python - 动态调整图像大小 pyGTK (python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17298780/

相关文章:

python - 使用Python ftplib上传文件时"' NoneType ' object has no attribute ' sendall '"

python - 在 pandas 数据框中查找范围内的值

html - 做个类(class)网站,背景图比我需要的高?

javascript - 无法限制文件数量并将其保存到数据库、图像 uploader Javascript 和 PHP

c++ - 为什么 std::vector 不保留 "double"它的容量,而调整大小呢?

python - 使用 lambda 作为属性的默认值时,Django 1.7.1 Makemigrations 失败

在 C 中将 RGB 转换为 RGBA

jquery - 使用 jquery 窗口大小动态调整元素大小

jQuery onload 屏幕大小和屏幕调整大小

python - 如何从某个索引循环遍历列表?