Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Tkinter中询问多个目录对话框_Python_Python 3.x_Tkinter_Directory - Fatal编程技术网

Python 在Tkinter中询问多个目录对话框

Python 在Tkinter中询问多个目录对话框,python,python-3.x,tkinter,directory,Python,Python 3.x,Tkinter,Directory,我正在尝试选择多个文件夹。我需要相当于askopenfilenames()的目录,但只有askdirectory()存在,它只允许选择一个文件夹 之前,我发现了一个为Matlab做这件事的自定义脚本(uigetdir)。 有没有办法用Python实现这一点 我需要一次批处理大约50个文件夹中的文件,逐个选择它们是不现实的 另外,我不是一个程序员,只是试图处理我的地球物理数据,我不能像我在其他地方看到的那样“自己编写代码”。 本以为这样的基本功能会包含在基本功能中。OP要求Tkinter提供解决方

我正在尝试选择多个文件夹。我需要相当于
askopenfilenames()
的目录,但只有
askdirectory()
存在,它只允许选择一个文件夹

之前,我发现了一个为Matlab做这件事的自定义脚本(
uigetdir
)。 有没有办法用Python实现这一点

我需要一次批处理大约50个文件夹中的文件,逐个选择它们是不现实的

另外,我不是一个程序员,只是试图处理我的地球物理数据,我不能像我在其他地方看到的那样“自己编写代码”。
本以为这样的基本功能会包含在基本功能中。

OP要求Tkinter提供解决方案,但wxPython Phoenix可能提供解决方案

####### Retrieve a list of directories with wxPython-Phoenix   - tested on python3.5
### installation instruction for wxPython-Phoenix  : https://wiki.wxpython.org/How%20to%20install%20wxPython#Installing_wxPython-Phoenix_using_pip
### modified from : https://wxpython.org/Phoenix/docs/html/wx.lib.agw.multidirdialog.html
import os
import wx
import wx.lib.agw.multidirdialog as MDD

# Our normal wxApp-derived class, as usual
app = wx.App(0)
dlg = MDD.MultiDirDialog(None, title="Custom MultiDirDialog", defaultPath=os.getcwd(),  # defaultPath="C:/Users/users/Desktop/",
                         agwStyle=MDD.DD_MULTIPLE|MDD.DD_DIR_MUST_EXIST)

if dlg.ShowModal() != wx.ID_OK:
    print("You Cancelled The Dialog!")
    dlg.Destroy()


paths = dlg.GetPaths()

#Print directories' path and files 
for path in enumerate(paths):
    print(path[1])
    directory= path[1].replace('OS (C:)','C:')
    print(directory)
    for file in os.listdir(directory):
        print(file)

dlg.Destroy()
app.MainLoop()

有了同样的问题,我制定了自己的解决方案。使用此选项,您可以一次选择一个目录,然后在完成后选择“取消”

函数返回所选目录的列表

def fun_directory_selector(request_string: str, selected_directory_list: list, search_directory):
    directory_path_string = filedialog.askdirectory(initialdir=search_directory, title=request_string)

       if len(directory_path_string) > 0:
        selected_directory_list.append(directory_path_string)
        fun_directory_selector('Select the next Directory or Cancel to end', 
                               selected_directory_list,
                               os.path.dirname(directory_path_string))

    return selected_directory_list

我对它了解不多,但快速的谷歌搜索就会发现。你做过很多(任何)研究吗?@AdamSmith这只是一个继续
askdirectory
的循环,这并不是OP想要的。@Rinzler在
tkinter.askdirectory多个目录的第一个结果后,我否决了这个问题。askdirectory多个目录
是一个带有适用代码的新闻组帖子“没有更好的方法可以做到这一点,但这里有一点困难。”投票结果是“这个问题没有显示任何研究成果。”。“我认为标准库中没有这样一个函数来
askdirectories
,但我认为它可以以某种方式实现。@Adam这个问题值得投票,以鼓励人们实现这样一个对话框,以我谦虚的观点。很容易说“不”,并给出解决办法。我收到一个错误消息,说C/C++和Windows语言环境不匹配。发生了什么事???!!!