2015-02-09 49 views
4

我想選擇多個文件夾。我需要相當於askopenfilenames()的目錄,但只有askdirectory()存在,它只允許選擇一個文件夾。在Tkinter中詢問多個目錄對話框

以前我發現一個自定義腳本,它爲Matlab做了這個(uigetdir)。 在Python中這樣做的任何方式?

我需要批量處理大約50個文件夾中的文件,一個一個選擇它們是不現實的。另外,我不是程序員,只是試圖處理我的地球物理數據,不能像我在其他地方看到的那樣「自己編寫代碼」。想到這樣一個基本的東西會包含在基本功能中。

+0

我不知道了一大堆關於它的,但快速谷歌搜索發現[此鏈接](https://mail.python.org/pipermail/tkinter-discuss/2011-January/002735.html)。你做了很多(任何)研究嗎? – 2015-02-09 20:53:01

+0

@AdamSmith這只是一個循環,繼續到'askdirectory',這不完全是OP想要的。 – nbro 2015-02-09 20:53:59

+0

@Rinzler我在tkinter.ask目錄下的多個目錄的第一個結果是用適當的代碼發佈的新聞組發帖之後低估了這個問題:「沒有更好的方法可以做到這一點,但是這裏有一點竅門。」 downvote是爲「這個問題沒有顯示任何研究努力。」 – 2015-02-09 20:55:53

回答

0

的OP要求與Tkinter的一個解決方案,它是不可用,但是一個解決方案是可能的wxPython鳳凰

####### 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() 
+0

我得到一個錯誤,說'C/C++之間不匹配和Windows語言環境「。發生什麼事???!!! – 2018-02-15 16:29:42

+0

您是否嘗試過https://stackoverflow.com/a/24763770/3154274 – Enora 2018-02-16 16:24:55