[pygtk] some trouble with preview widget in GtkFileChooserDialog

Robert Park rbpark at exolucere.ca
Wed Sep 22 14:57:38 WST 2010


D'oh! show_all() was the magic I was missing. Here is the solution
I've arrived at:

	def update_preview(self, chooser):
		filename = chooser.get_preview_filename()
		
		try:
			thumb = gtk.gdk.pixbuf_new_from_file_at_size(filename, 300, 300)
			image = gtk.Image()
			image.set_from_pixbuf(thumb)
			
			label = gtk.Label("some information\nabout this file")
			label.set_justify(gtk.JUSTIFY_CENTER)
			
			vbox = gtk.VBox(False, 12)
			vbox.pack_start(image, False, False, 6)
			vbox.pack_start(label, False, False)
			vbox.show_all()
			
			chooser.set_preview_widget(vbox)
			active = True
		except (TypeError, glib.GError):
			active = False
		
		chooser.set_preview_widget_active(active)

On Wed, Sep 22, 2010 at 12:37 AM, Robert Park <rbpark at exolucere.ca> wrote:
> Hi,
>
> What I'm wanting to accomplish is to have an image preview, with a few
> rows of text beneath the image show up on the preview area of the
> FileChooserDialog.
>
> What I have so far that works:
>
>        def update_preview(self, chooser):
>                filename = chooser.get_preview_filename()
>
>                try:
>                        thumb = gtk.gdk.pixbuf_new_from_file_at_size(filename, 300, 300)
>                        image = gtk.Image()
>                        image.set_from_pixbuf(thumb)
>
>                        chooser.set_preview_widget(image)
>                        active = True
>                except:
>                        active = False
>
>                chooser.set_preview_widget_active(active)
>
> This works perfectly as you'd expect, for supported image types, it
> shows a 300px preview image, and for unsupported filetypes it shows
> nothing with no errors.
>
> However, if I try the obvious thing of embedding the image into a
> vbox, it completely stops working.
>
>        def update_preview(self, chooser):
>                filename = chooser.get_preview_filename()
>
>                try:
>                        thumb = gtk.gdk.pixbuf_new_from_file_at_size(filename, 300, 300)
>                        image = gtk.Image()
>                        image.set_from_pixbuf(thumb)
>
>                        vbox = gtk.VBox(False, 12)
>                        vbox.add(image)
>
>                        chooser.set_preview_widget(vbox)
>                        active = True
>                except (TypeError, glib.GError):
>                        active = False
>
>                chooser.set_preview_widget_active(active)
>
> Instead of showing the preview image as before, it simply shows an
> ellipses ("...") with no image, and no errors of any kind (nothing
> printed to the terminal, the program continues running fine, etc).
>
> I am completely baffled by this. Why can't I just drop in a vbox as
> the preview widget? I don't see anything at all in the documentation
> stating that gtk.Image() is the only acceptable preview widget type.
> Quite the opposite, it implies that any Gtk.Widget can be used, of
> which VBox is certainly one.
>
> Adding a few Gtk.Label()s to the vbox also didn't help. What gives?
>
> Thanks.
>
> --
> http://exolucere.ca
>



-- 
http://exolucere.ca


More information about the pygtk mailing list