从pygtk程序中启动默认图像查看器

2024-05-23 14:23:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在Ubuntu中编写一个PyGTK GUI应用程序来浏览一些图像,我想在默认的图像查看器应用程序中打开它,当它被双击时(比如在Nautilus中打开时)。
我该怎么做?在


Tags: 图像应用程序ubuntuguipygtknautilus
3条回答

在GNU/Linux中使用xdg-open,在Mac中使用open,在Windows中使用start。另外,使用subprocess,否则在调用外部应用程序时可能会阻塞应用程序。在

这是我的实现,希望它有帮助: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

我不知道具体使用PyGTK,但是:xdg-open会为一个文件打开默认应用程序,因此运行类似这样的程序应该可以:

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

编辑:我建议使用注释中的subprocess模块。我还不确定如何使用它,所以我只是在示例中使用os.system来显示xdg-open。在

GTK(>;=2.14)有gtk_show_uri

gtk.show_uri(screen, uri, timestamp)

用法示例:

^{pr2}$

相关

相关问题 更多 >