Launch default image viewer from pygtk program

可紊 提交于 2019-12-04 01:59:57

问题


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?


回答1:


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.




回答2:


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



回答3:


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?


来源:https://stackoverflow.com/questions/3431135/launch-default-image-viewer-from-pygtk-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!