开发者

Launch default image viewer from pygtk program

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 open开发者_开发百科ed in Nautilus).

How can I do it?


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.


In GNU/Linux use xdg-open, in Mac use open, in Windows use start. Also, use subprocess, if not you risk to block your application when you call the external app.

This is my implementation, hope it helps: http://goo.gl/xebnV

import sys
import subprocess
import webbrowser

def default_open(something_to_open):
    """
    Open given file with default user program.
    """
    # Check if URL
    if something_to_open.startswith('http') or something_to_open.endswith('.html'):
        webbrowser.open(something_to_open)
        return 0

    ret_code = 0

    if sys.platform.startswith('linux'):
        ret_code = subprocess.call(['xdg-open', something_to_open])

    elif sys.platform.startswith('darwin'):
        ret_code = subprocess.call(['open', something_to_open])

    elif sys.platform.startswith('win'):
        ret_code = subprocess.call(['start', something_to_open], shell=True)

    return ret_code


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

  • How to open a file with the standard application?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜