views:

51

answers:

2

Hi there!

I'm writing a PyGTK GUI application in Ubuntu to browse some images, and I'd like to open an image in the default image viewer application when it is double-clicked (like when it is opened in Nautilus).
How can I do it?

+2  A: 

I don't know specifically using PyGTK but: xdg-open opens the default app for a file so running something like this should work:

import os
os.system('xdg-open ./img.jpg')

EDIT: I'd suggest using the subprocess module as in the comments. I'm not sure exactly how to use it yet so I just used os.system in the example to show xdg-open.

vlad003
Works great, thanks!
mooware
Useless use of `system`. This should be `subprocess.check_call(["xdg-open", filename])`.
Philipp
Hi Philipp, what's wrong with using 'system'?
mooware
The `subprocess` module is aimed at replacing older functions like `os.system`: http://docs.python.org/library/subprocess.html
vlad003
+1  A: 

GTK (>= 2.14) has gtk_show_uri:

gtk.show_uri(screen, uri, timestamp)

Example usage:

gtk.show_uri(None, "file:///etc/passwd", gtk.gdk.CURRENT_TIME)

Related

Bruce van der Kooij