使用简单的对话框在 Python 中选择文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3579568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Choosing a file in Python with simple Dialog
提问by Mustafa Zengin
I would like to get file path as input in my Python console application.
我想在我的 Python 控制台应用程序中获取文件路径作为输入。
Currently I can only ask for full path as an input in the console.
目前我只能要求完整路径作为控制台中的输入。
Is there a way to trigger a simple user interface where users can select file instead of typing the full path?
有没有办法触发一个简单的用户界面,用户可以在其中选择文件而不是输入完整路径?
采纳答案by Etaoin
How about using tkinter?
使用 tkinter 怎么样?
from Tkinter import Tk
from tkinter.filedialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
Done!
完毕!
回答by Gary Kerr
In Python 2 use the tkFileDialogmodule.
在 Python 2 中使用tkFileDialog模块。
import tkFileDialog
tkFileDialog.askopenfilename()
In Python 3 use the tkinter.filedialogmodule.
在 Python 3 中使用该tkinter.filedialog模块。
import tkinter.filedialog
tkinter.filedialog.askopenfilename()
回答by jfs
回答by Stefano Palazzo
Python 3.x version of Etaoin's answer for completeness:
Etaoin 的完整答案的 Python 3.x 版本:
from tkinter.filedialog import askopenfilename
filename = askopenfilename()
回答by kylejmcintyre
Another option to consider is Zenity: http://freecode.com/projects/zenity.
另一个需要考虑的选择是 Zenity:http://freecode.com/projects/zenity 。
I had a situation where I was developing a Python server application (no GUI component) and hence didn't want to introduce a dependency on any python GUI toolkits, but I wanted some of my debug scripts to be parameterized by input files and wanted to visually prompt the user for a file if they didn't specify one on the command line. Zenity was a perfect fit. To achieve this, invoke "zenity --file-selection" using the subprocess module and capture the stdout. Of course this solution isn't Python-specific.
我有一种情况,我正在开发一个 Python 服务器应用程序(没有 GUI 组件),因此不想引入对任何 Python GUI 工具包的依赖,但我希望我的一些调试脚本由输入文件参数化,并希望如果用户未在命令行中指定文件,则直观地提示用户输入文件。Zenity 是一个完美的选择。为此,请使用 subprocess 模块调用“zenity --file-selection”并捕获标准输出。当然,此解决方案不是特定于 Python 的。
Zenity supports multiple platforms and happened to already be installed on our dev servers so it facilitated our debugging/development without introducing an unwanted dependency.
Zenity 支持多个平台,并且恰好已经安装在我们的开发服务器上,因此它促进了我们的调试/开发,而不会引入不需要的依赖项。
回答by Glen Whitney
I obtained much better results with wxPython than tkinter, as suggested in this answer to a later duplicate question:
我使用 wxPython 获得了比 tkinter 更好的结果,正如对后来重复问题的回答中所建议的:
https://stackoverflow.com/a/9319832
https://stackoverflow.com/a/9319832
The wxPython version produced the file dialog that looked the same as the open file dialog from just about any other application on my OpenSUSE Tumbleweed installation with the xfce desktop, whereas tkinter produced something cramped and hard to read with an unfamiliar side-scrolling interface.
wxPython 版本生成的文件对话框看起来与我的 OpenSUSE Tumbleweed 安装上的几乎任何其他应用程序的打开文件对话框相同,并且带有 xfce 桌面,而 tkinter 生成了一些狭窄且难以阅读的东西,并且具有不熟悉的横向滚动界面。

