Python 从pygtk程序启动默认图像查看器
我正在Ubuntu中编写一个PyGTK GUI应用程序来浏览一些图像,我想在双击图像时在默认的图像查看器应用程序中打开一个图像(就像在Nautilus中打开图像一样)。Python 从pygtk程序启动默认图像查看器,python,linux,pygtk,Python,Linux,Pygtk,我正在Ubuntu中编写一个PyGTK GUI应用程序来浏览一些图像,我想在双击图像时在默认的图像查看器应用程序中打开一个图像(就像在Nautilus中打开图像一样)。 我该怎么做呢?我不知道具体如何使用PyGTK,但是:xdg open会打开文件的默认应用程序,所以运行类似这样的程序应该可以: import os os.system('xdg-open ./img.jpg') 编辑:我建议使用注释中的子流程模块。我还不确定如何使用它,所以我只是在示例中使用了os.system来显示xdg o
我该怎么做呢?我不知道具体如何使用PyGTK,但是:
xdg open
会打开文件的默认应用程序,所以运行类似这样的程序应该可以:
import os
os.system('xdg-open ./img.jpg')
编辑:我建议使用注释中的子流程
模块。我还不确定如何使用它,所以我只是在示例中使用了os.system
来显示xdg open
GTK(>=2.14)具有:
用法示例:
gtk.show_uri(None, "file:///etc/passwd", gtk.gdk.CURRENT_TIME)
相关的
xdg open
,在Mac中使用open
,在Windows中使用start
。此外,如果没有,请使用子流程
,否则在调用外部应用程序时可能会阻止应用程序
这是我的实现,希望它有助于:
系统的无用使用
。这应该是子流程。检查调用([“xdg open”,filename])
。子流程
模块旨在替换较旧的功能,如操作系统
:
gtk.show_uri(None, "file:///etc/passwd", gtk.gdk.CURRENT_TIME)
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