标签归档:filechooser

使用简单对话框在Python中选择文件

问题:使用简单对话框在Python中选择文件

我想在我的Python控制台应用程序中获取文件路径作为输入。

目前,我只能要求完整路径作为控制台中的输入。

有没有一种触发简单用户界面的方法,用户可以在其中选择文件而不是键入完整路径?

I would like to get file path as input in my Python console application.

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?


回答 0

使用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)

做完了!

How about using tkinter?

from Tkinter import Tk     # from tkinter import Tk for Python 3.x
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!


回答 1

Etaoin答案的Python 3.x版本的完整性:

from tkinter.filedialog import askopenfilename
filename = askopenfilename()

Python 3.x version of Etaoin’s answer for completeness:

from tkinter.filedialog import askopenfilename
filename = askopenfilename()

回答 2

使用EasyGui(由pydocepydoc生成的0.96版文档):

import easygui
print(easygui.fileopenbox())

安装:

pip install easygui

演示:

import easygui
easygui.egdemo()

With EasyGui:

import easygui
print(easygui.fileopenbox())

To install:

pip install easygui

Demo:

import easygui
easygui.egdemo()

回答 3

在Python 2中,使用tkFileDialog模块。

import tkFileDialog

tkFileDialog.askopenfilename()

在Python 3中,使用tkinter.filedialog模块。

import tkinter.filedialog

tkinter.filedialog.askopenfilename()

In Python 2 use the tkFileDialog module.

import tkFileDialog

tkFileDialog.askopenfilename()

In Python 3 use the tkinter.filedialog module.

import tkinter.filedialog

tkinter.filedialog.askopenfilename()

回答 4

另一个要考虑的选项是Zenity:http://freecode.com/projects/zenity

我遇到的情况是我正在开发Python服务器应用程序(没有GUI组件),因此不想引入对任何python GUI工具包的依赖关系,但是我希望我的一些调试脚本可以通过输入文件进行参数化,并且想要如果用户未在命令行上指定文件,则以可视方式提示用户输入文件。Zenity非常适合。为此,请使用子流程模块调用“ zenity –file-selection”并捕获标准输出。当然,此解决方案不是特定于Python的。

Zenity支持多种平台,并且恰好已经安装在我们的开发服务器上,因此它方便了我们的调试/开发,而没有引入不必要的依赖性。

Another option to consider is 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.

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.


回答 5

我用wxPython获得的结果比tkinter更好,这是对以后重复的问题的回答:

https://stackoverflow.com/a/9319832

wxPython版本通过xfce桌面在我的OpenSUSE Tumbleweed安装中几乎通过任何其他应用程序生成的文件对话框看上去都与打开文件对话框相同,而tkinter却产生了局促且难以理解的内容,并且使用了不熟悉的侧滚动界面。

I obtained much better results with wxPython than tkinter, as suggested in this answer to a later duplicate question:

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.