用tkinter选择多个目录

问题描述 投票:0回答:3
最快的方法是获得“ AskDirectories”的可行解决方案?

不幸的是,TKINTER并未本地支持这一点。 TkfileBrowser是一个不错的选择。基于卢克(Luke)的答案使用TKFileBrowser的代码如下:

import tkfilebrowser from tkinter import * root = Tk() root.geometry('200x200') root.grid_rowconfigure(0, weight=1) root.grid_columnconfigure(0, weight=1) dirs = [] def get_directories(): dirs.append(tkfilebrowser.askopendirnames()) return dirs b1 = Button(root, text='select directories...', command=get_directories) b1.pack() root.mainloop()

python tkinter
3个回答
6
投票
在纯TKINTER中执行此操作的唯一方法(除了手动构建目录选择器小部件外)是在单独的对话框中向每个DIR请求用户。您可以保存先前使用的位置,因此用户不需要每次使用以下代码:

from tkinter import filedialog dirselect = filedialog.Directory() dirs = [] while True: d = dirselect.show() if not d: break dirs.append(d)
其他解决方案是使用
tkinter.tix

2
投票
tkinter.tix.DirList

小部件。看起来如下(有点旧的IMG):



有关更多信息,请参见
Tkinter.tix

TKtixdocsimg

您应该能够使用

tkFileDialog.askdirectory。看一下文档HEREY:)


1
投票

也许是这样的?

from Tkinter import * import tkFileDialog root = Tk() root.geometry('200x200') root.grid_rowconfigure(0, weight = 1) root.grid_columnconfigure(0, weight = 1) dirs = [] def get_directories(): dirs.append(tkFileDialog.askdirectory()) return dirs b1 = Button(root, text='select directories...', command = get_directories) b1.pack() root.mainloop() 任何想法?

我本人一直在寻找这样的事情。但是,我不仅需要返回目录,还需要目录和(正常)文件的混合。上面的建议都没有给我。此外,TIX解决方案在测试时会发出“弃用”警告,并且根据文档仅返回单目录名称。 安装TKFileBrowser似乎在测试中进行了太多的努力,因为根据文档,我也不会混合文件和目录。 所以,我写了一个小的(相对)功能,仅使用任何最近(3.x)Python构建环境中可用的模块。下面是一个原型,不是很漂亮,但是它可以解决问题。您会注意到,当尝试从Windows下的root Directory上升时,我使用了标准的TKFileDialog函数返回单个目录名称,以访问其他驱动器。

import os import platform from tkinter import * from tkinter import ttk, filedialog as fd def askopennames(initialDir='.'): if not initialDir: return ('',[]) curdir = [initialDir] returnList = [] top = Toplevel() top.columnconfigure(0, weight=1) pbtn = Button(top, text="print selection") def build_tree(dir): updir = os.path.join(os.path.abspath(dir), '..') def go_updir(): p = os.path.abspath(updir) dnp = os.path.dirname(updir) retdir = fd.askdirectory(initialdir=dnp) \ if os.path.samefile(p, dnp) and \ platform.system() == 'Windows' else p curdir[0] = retdir if retdir else dnp build_tree(curdir[0]) tree = ttk.Treeview(top, height=17) tree.columnconfigure(0, weight=1) tree.heading('#0', text=updir, anchor='w', command=go_updir) for c in os.listdir(dir): iid = tree.insert('', 'end', text=c, open=False) if os.path.isdir(os.path.join(dir,c)): tree.insert(iid, "end") tree.grid(row=0, sticky='ew') pbtn.grid(row=1, sticky='nsew') def get_selection(): returnList.extend([tree.item(s)['text'] \ for s in tree.selection() if tree.item(s)['text']]) top.destroy() pbtn['command'] = get_selection def rebuild_tree(e): folder = tree.item(tree.focus())['text'] curdir[0] = os.path.join(dir, folder) tree.destroy() build_tree(curdir[0]) tree.bind('<<TreeviewOpen>>', rebuild_tree) build_tree(curdir[0]) top.wait_window() return (curdir[0], returnList) root = Tk() def getDirPath(): print(askopennames('.')) Button(root, text="ok", font=("Helvetica", "13"), command=getDirPath).grid(ipadx=100) root.mainloop()

nice件代码。我怎么能在选择中只看到目录

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.