【发布时间】:2015-10-15 22:47:56
【问题描述】:
在我使用过的几乎所有桌面应用程序中,都可以使用看起来像 Finder 的标准文件导航器来选择打开或保存文件的路径。例如,如果我去保存这个网页, this menu comes up,我可以选择保存位置,创建新文件夹等。是否有系统调用从我自己的程序中启动此菜单,并大概返回所选文件或文件夹的路径?
【问题讨论】:
在我使用过的几乎所有桌面应用程序中,都可以使用看起来像 Finder 的标准文件导航器来选择打开或保存文件的路径。例如,如果我去保存这个网页, this menu comes up,我可以选择保存位置,创建新文件夹等。是否有系统调用从我自己的程序中启动此菜单,并大概返回所选文件或文件夹的路径?
【问题讨论】:
【讨论】:
如果其他人看到这一点,Johannes Weiß 的回答是正确的:NSOpenPanel 正是我想要的。在 Python 中,它的代码如下所示:
from AppKit import NSOpenPanel
# the following import is only used to prevent multiselect and directory select
from objc import NO
panel = NSOpenPanel.openPanel()
# set the title (not required)
panel.setTitle("open file")
# prevent multiselect and directory select (not required)
panel.setAllowsMultipleSelection_(NO)
panel.setCanChooseDirectories_(NO)
# open the panel
panel.runModal()
# get the path
path = panel.filenames()[0]
可以找到更长的示例here。
【讨论】: