2024-05-23 14:23:35 发布
网友
我正在Ubuntu中编写一个PyGTK GUI应用程序来浏览一些图像,我想在默认的图像查看器应用程序中打开它,当它被双击时(比如在Nautilus中打开时)。 我该怎么做?在
在GNU/Linux中使用xdg-open,在Mac中使用open,在Windows中使用start。另外,使用subprocess,否则在调用外部应用程序时可能会阻塞应用程序。在
xdg-open
open
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。在
os.system
GTK(>;=2.14)有gtk_show_uri:
gtk.show_uri(screen, uri, timestamp)
用法示例:
相关
在GNU/Linux中使用
xdg-open
,在Mac中使用open
,在Windows中使用start
。另外,使用subprocess
,否则在调用外部应用程序时可能会阻塞应用程序。在这是我的实现,希望它有帮助:http://goo.gl/xebnV
我不知道具体使用PyGTK,但是:
xdg-open
会为一个文件打开默认应用程序,因此运行类似这样的程序应该可以:编辑:我建议使用注释中的
subprocess
模块。我还不确定如何使用它,所以我只是在示例中使用os.system
来显示xdg-open
。在GTK(>;=2.14)有gtk_show_uri:
用法示例:
^{pr2}$相关
相关问题 更多 >
编程相关推荐