带图像预览的GtkFileChooserDialog

时间:2016-09-16 00:29:24

标签: python gtk

如何在composer dump-autoload

中创建图片预览

这是一个简单的ChooserDialog:

GtkFileChooserDialog

如何添加图片预览?

1 个答案:

答案 0 :(得分:1)

GtkFileChooser提供set_preview_widget功能。您可以创建GtkImage,将其设置为预览窗口小部件,并在发出update-preview信号时更新图像:

from gi.repository import GdkPixbuf

preview_image= Gtk.Image()
filechooserdialog.set_preview_widget(preview_image)

def update_preview(dialog):
    path= dialog.get_preview_filename()
    try:
        pixbuf= GdkPixbuf.Pixbuf.new_from_file(path)
    except Exception:
        dialog.set_preview_widget_active(False)
    else:
        #scale the image
        maxwidth, maxheight= 300.0, 700.0 # as floats to avoid integer division in python2
        width, height= pixbuf.get_width(), pixbuf.get_height()
        scale= min(maxwidth/width, maxheight/height)
        if scale<1:
            width, height= int(width*scale), int(height*scale)
            pixbuf= pixbuf.scale_simple(width, height, GdkPixbuf.InterpType.BILINEAR)

        preview_image.set_from_pixbuf(pixbuf)
        dialog.set_preview_widget_active(True)
filechooserdialog.connect('update-preview', update_preview)