使用tkinter Treeview小部件显示目录内容

目前,我正在开发一个程序,该程序具有自己的项目文件,并且内部有子文件之类的东西,我想知道如何使用treeview小部件显示项目文件中的所有子文件,有什么想法吗?



手掌心
浏览 439回答 2
2回答

蛊毒传说

CPython的源代码中有一个示例,说明如何用目录的内容递归地填充Treeview,这基本上是它的工作方式(为了更好的可读性,我删除了事件绑定并将其包装在一个类中):import osimport tkinter as tkimport tkinter.ttk as ttkclass App(tk.Frame):&nbsp; &nbsp; def __init__(self, master, path):&nbsp; &nbsp; &nbsp; &nbsp; tk.Frame.__init__(self, master)&nbsp; &nbsp; &nbsp; &nbsp; self.tree = ttk.Treeview(self)&nbsp; &nbsp; &nbsp; &nbsp; ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)&nbsp; &nbsp; &nbsp; &nbsp; xsb = ttk.Scrollbar(self, orient='horizontal', command=self.tree.xview)&nbsp; &nbsp; &nbsp; &nbsp; self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)&nbsp; &nbsp; &nbsp; &nbsp; self.tree.heading('#0', text=path, anchor='w')&nbsp; &nbsp; &nbsp; &nbsp; abspath = os.path.abspath(path)&nbsp; &nbsp; &nbsp; &nbsp; root_node = self.tree.insert('', 'end', text=abspath, open=True)&nbsp; &nbsp; &nbsp; &nbsp; self.process_directory(root_node, abspath)&nbsp; &nbsp; &nbsp; &nbsp; self.tree.grid(row=0, column=0)&nbsp; &nbsp; &nbsp; &nbsp; ysb.grid(row=0, column=1, sticky='ns')&nbsp; &nbsp; &nbsp; &nbsp; xsb.grid(row=1, column=0, sticky='ew')&nbsp; &nbsp; &nbsp; &nbsp; self.grid()&nbsp; &nbsp; def process_directory(self, parent, path):&nbsp; &nbsp; &nbsp; &nbsp; for p in os.listdir(path):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; abspath = os.path.join(path, p)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isdir = os.path.isdir(abspath)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oid = self.tree.insert(parent, 'end', text=p, open=False)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if isdir:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.process_directory(oid, abspath)root = tk.Tk()path_to_my_project = # ...app = App(root, path=path_to_my_project)app.mainloop()更新:正如@ArtOfWarfare所提到的,可以使用该<<TreeviewOpen>>事件懒散地填充树。为了模拟封闭的节点,我使用了一个空的子项,该子项在打开目录时将被删除:import osimport tkinter as tkimport tkinter.ttk as ttkclass App(object):&nbsp; &nbsp; def __init__(self, master, path):&nbsp; &nbsp; &nbsp; &nbsp; self.nodes = dict()&nbsp; &nbsp; &nbsp; &nbsp; frame = tk.Frame(master)&nbsp; &nbsp; &nbsp; &nbsp; self.tree = ttk.Treeview(frame)&nbsp; &nbsp; &nbsp; &nbsp; ysb = ttk.Scrollbar(frame, orient='vertical', command=self.tree.yview)&nbsp; &nbsp; &nbsp; &nbsp; xsb = ttk.Scrollbar(frame, orient='horizontal', command=self.tree.xview)&nbsp; &nbsp; &nbsp; &nbsp; self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)&nbsp; &nbsp; &nbsp; &nbsp; self.tree.heading('#0', text='Project tree', anchor='w')&nbsp; &nbsp; &nbsp; &nbsp; self.tree.grid()&nbsp; &nbsp; &nbsp; &nbsp; ysb.grid(row=0, column=1, sticky='ns')&nbsp; &nbsp; &nbsp; &nbsp; xsb.grid(row=1, column=0, sticky='ew')&nbsp; &nbsp; &nbsp; &nbsp; frame.grid()&nbsp; &nbsp; &nbsp; &nbsp; abspath = os.path.abspath(path)&nbsp; &nbsp; &nbsp; &nbsp; self.insert_node('', abspath, abspath)&nbsp; &nbsp; &nbsp; &nbsp; self.tree.bind('<<TreeviewOpen>>', self.open_node)&nbsp; &nbsp; def insert_node(self, parent, text, abspath):&nbsp; &nbsp; &nbsp; &nbsp; node = self.tree.insert(parent, 'end', text=text, open=False)&nbsp; &nbsp; &nbsp; &nbsp; if os.path.isdir(abspath):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.nodes[node] = abspath&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tree.insert(node, 'end')&nbsp; &nbsp; def open_node(self, event):&nbsp; &nbsp; &nbsp; &nbsp; node = self.tree.focus()&nbsp; &nbsp; &nbsp; &nbsp; abspath = self.nodes.pop(node, None)&nbsp; &nbsp; &nbsp; &nbsp; if abspath:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tree.delete(self.tree.get_children(node))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for p in os.listdir(abspath):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.insert_node(node, p, os.path.join(abspath, p))if __name__ == '__main__':&nbsp; &nbsp; root = tk.Tk()&nbsp; &nbsp; app = App(root, path='.')&nbsp; &nbsp; root.mainloop()

守候你守候我

可以通过以下方式在支持Python 3.4及更高版本的第二行和第三行更改导入:import tkinter as tkimport tkinter.ttk as ttktkinter中的小写字母t代替了Tkinter中的大写字母T,因为Python 3.4及更高版本不再识别Tkinter;消除了“无法识别的参考”错误。在较新的Python发行版中,完全合格的导入指令对所有点符号都更加严格,因此tkinter.ttk是ttk所必需的,从而消除了对重复的完全合格引用的需求。告诫:有人认为导入tk.ttk就足够了,但是以某种方式引起了参考错误;消除过多的指针和有条件的宏处理使我选择了上面的格式-还有其他可能,但这是最容易使用的形式。path_to_my_project =#...会引发错误,但仅是占位符(尽管仍然可以使用),可以更改为以下内容:path_to_my_project = "" # ...请记住,如果在Windows中运行脚本,则使用反斜杠的文字文件路径会引发错误。您必须使用前面的反斜杠(双反斜杠)对文件路径url中的反斜杠进行转义,如下所示:path_to_my_project = "C:\\Users\\userName\\Desktop\\projDir" #Windows file paths在文件路径中使用反斜杠转义反斜杠可以消除“ Unicode转义字符”错误,其中“ C:\ Users”解析为在“用户”中转义控制字符U,而这并不是我们最初想要的。将路径转换为字符串将无法工作,因为会引发相同的错误,因此:path_to_my_project = str("C:\Users\userName\Desktop\projDir")...无效。上面的脚本示例针对在Windows 10 64bit上运行的Python 3.4 64bit进行了这些修改,没有任何问题,并且非常干净,可靠。我喜欢它-轻松运行(简单的更新),没有错误,警告或无法解释的解决方法,bs和hacks。竖起大拇指。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python