tkinter 中的文件夹导航树

Folder Navigation Tree in tkinter

我想制作一个带有嵌入式文件夹导航树的图形用户界面,就像显示的那样 here。 tkinter 是否提供与此直接类似的任何小部件,还是我必须从头开始构建它?如果是后者,我是否使用 treeview 小部件?在这种情况下如何添加图标支持?

您可以在使用 insert() use image perimeter

时在树视图中添加图标

你应该阅读这篇文章https://tkdocs.com/tutorial/tree.html

你可以试试这个:-

import os
from tkinter import*
from tkinter import ttk
def process_directory(parent,path):
    for i in os.listdir(path):
        abspath = os.path.join(path,i)
        isdir = os.path.isdir(abspath)
        if isdir:
            elements = tree.insert(parent,END,text=i,open=False,values=(abspath,),image=image_)
            process_directory(elements,abspath)
        else:
            if i.endswith(".py"):
                elements = tree.insert(parent,END,text=i,open=False,values=(abspath,),image=image_1)
            
            
            
root = Tk()
root.title("File Display")
image_ = PhotoImage(file="folder.png") # put here file name of folder image that you have
image_1 = PhotoImage(file="python.png")# put here file name of python image that you have


folder = "."

tree = ttk.Treeview(root,show="tree")
abspath = os.path.abspath(folder)
parent_folder = tree.insert("",END,text=abspath,open=True)
process_directory(parent_folder,abspath)
tree.pack()







root.mainloop()

可以看到输出:-