使用简单的Dialog在Python中选择文件

时间:2010-08-26 21:13:11

标签: python user-interface dialog filechooser

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

目前我只能在控制台中询问完整路径作为输入。

有没有办法触发一个简单的用户界面,用户可以选择文件而不是输入完整路径?

9 个答案:

答案 0 :(得分:172)

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

完成!

答案 1 :(得分:74)

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

from tkinter.filedialog import askopenfilename
filename = askopenfilename()

答案 2 :(得分:21)

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

import easygui
print easygui.fileopenbox()

安装:

pip install easygui

演示:

import easygui
easygui.egdemo()

答案 3 :(得分:5)

使用属于标准安装的tkFileDialog模块。

import tkFileDialog

print tkFileDialog.askopenfilename()

答案 4 :(得分:3)

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

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

Zenity支持多个平台,并且已经安装在我们的开发服务器上,因此它便于我们的调试/开发,而不会引入不必要的依赖。

答案 5 :(得分:1)

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

https://stackoverflow.com/a/9319832

wxPython版本通过xfce桌面在我的OpenSUSE Tumbleweed安装中的几乎所有其他应用程序中产生了与打开文件对话框相同的文件对话框,而tkinter则产生了局促且难以理解的内容,并且不熟悉侧滚动接口。

答案 6 :(得分:1)

这对我有用

参考:https://www.youtube.com/watch?v=H71ts4XxWYU

import  tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
print(file_path)

答案 7 :(得分:0)

这是一个在终端窗口中显示文件选择器的简单函数。 此方法支持选择多个文件或目录。即使在不支持 GUI 的环境中,这也有额外的好处。

from os.path import join,isdir
from pathlib import Path
from enquiries import choose,confirm

def dir_chooser(c_dir=getcwd(),selected_dirs=None,multiple=True) :
    '''
        This function shows a file chooser to select single or
        multiple directories.
    '''
    selected_dirs = selected_dirs if selected_dirs else set([])

    dirs = { item for item in listdir(c_dir) if isdir(join(c_dir, item)) }
    dirs = { item for item in dirs if join(c_dir,item) not in selected_dirs and item[0] != "." } # Remove item[0] != "." if you want to show hidde

    options = [ "Select This directory" ]
    options.extend(dirs)
    options.append("⬅")

    info = f"You have selected : \n {','.join(selected_dirs)} \n" if len(selected_dirs) > 0 else "\n"
    choise = choose(f"{info}You are in {c_dir}", options)

    if choise == options[0] :
        selected_dirs.add(c_dir)

        if multiple and confirm("Do you want to select more folders?") :
            return get_folders(Path(c_dir).parent,selected_dirs,multiple)

        return selected_dirs

    if choise == options[-1] :
        return get_folders(Path(c_dir).parent,selected_dirs,multiple)

    return get_folders(join(c_dir,choise),selected_dirs,multiple)

要安装查询器,

<块引用>

pip 安装查询

答案 8 :(得分:0)

我解决了所有相关的问题 from tkinter import * from tkinter import filedialog

只需从 pycharm IDE 迁移到 visual studio code IDE 即可解决所有问题。