本站消息

站长简介/公众号


站长简介:高级工程师,爱好交友,无偿辅导python和前端,技术交流,面试指导,找工作指导,瞎聊都可加我微信i88811i哈,欢迎欢迎!也欢迎加入程序员交流群,专属程序员的圈子,加我微信拉你进群.欢迎关注我的微信公众号:程序员总部,程序员的家,探索程序员的人生之路!分享IT最新技术,关注行业最新动向,让你永不落伍。了解同行们的工资,生活工作中的酸甜苦辣,谋求程序员的最终出路!

  价值13000svip视频教程,python大神匠心打造,零基础python开发工程师视频教程全套,基础+进阶+项目实战,包含课件和源码

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

在 tkinter 的分层树视图中递归排列所有文件夹和文件

发布于2022-01-18 09:42     阅读(815)     评论(0)     点赞(23)     收藏(5)



我正在尝试在tkinter. 我刚刚听说了一个分层的Treeview. 因此,我正在尝试实施它。

在这里,我尝试使用递归,因为对于for循环,人们无法知道特定文件夹中的子目录和文件的数量。

这是我的递归函数。

def new_folder(path,arrs):

    for arr in arrs:
        if os.path.isdir(path+"\\"+arr):
            try:
                a1=os.listdir(path+"\\"+arr)
                new_folder(path+"\\"+arr,a1)

            except PermissionError:
                pass
        else:
            print(path+"\\"+arr)

path="C:\\Users\\91996\\Documents"
arrss=os.listdir(path)
new_folder(path,arrss)

每当我运行这段代码时,它都会给我这个错误:

Traceback (most recent call last):
  File "c:/Users/91996/Documents/Visual studio code/focd.py", line 47, in <module>
    new_folder(path,arrss,'item'+str(2))
  File "c:/Users/91996/Documents/Visual studio code/focd.py", line 37, in new_folder
    new_folder(path+"\\"+arr,a1,values[:-1]+str(3))
  File "c:/Users/91996/Documents/Visual studio code/focd.py", line 44, in new_folder
    treeview.move(values,'item1','end')
  File "C:\Program Files\Python3.8.6\lib\tkinter\ttk.py", line 1388, in move
    self.tk.call(self._w, "move", item, parent, index)
_tkinter.TclError: Item item3 not found

这是我的代码。

from tkinter import *
import os
# Importing ttk from tkinter
from tkinter import ttk

# Creating app window
app = Tk()

# Defining title of the app
app.title("GUI Application of Python")

# Defining label of the app and calling a geometry
# management method i.e, pack in order to organize
# widgets in form of blocks before locating them
# in the parent widget
ttk.Label(app, text ="Treeview(hierarchical)").pack()

# Creating treeview window
treeview = ttk.Treeview(app)

# Calling pack method on the treeview
treeview.pack()

# Inserting items to the treeview
# Inserting parent
treeview.insert('', '0', 'item1',
                text ='Documents')

# Inserting child
i=3
def new_folder(path,arrs,values):
    global i
    for arr in arrs:
        if os.path.isdir(path+"\\"+arr):
            try:
                a1=os.listdir(path+"\\"+arr)
                new_folder(path+"\\"+arr,a1,values[:-1]+str(i))
                i+=1
            except PermissionError:
                pass
        else:
            treeview.insert(values,'end',text=arr)
            print(path+"\\"+arr)
    treeview.move(values,'item1','end')
path="C:\\Users\\91996\\Documents"
arrss=os.listdir(path)
new_folder(path,arrss,'item'+str(i))
app.mainloop()

目前,这是Treeview我使用for循环制作的。但是,我觉得这不好,因为它不会获取文件和文件夹。我必须进行 8 个嵌套for循环才能将所有文件和文件夹放到第 8 个子文件夹中。但是递归可以解决所有问题。唯一的问题在于用ttk.Treeview.

在此处输入图像描述


解决方案


你开始:


i=3
...
new_folder(path,arrss,'item'+str(i))

如果您将这些行添加到您的函数中,您将看到item3它还不存在。


def new_folder(path,arrs,values):
    print('item1 ->', treeview.exists('item1'), treeview.item('item1'))
    print(values, '->', treeview.exists(values))
    global i
    for arr in arrs:
        print(f"action: treeview.insert(values,'end',text=arr)")
        print(f"insert arguments: parent='{values}', index='end', text='{arr}'")

输出:


item1 -> True {'text': 'Documents', 'image': '', 'values': '', 'open': 0, 'tags': ''}
item3 -> False
action: treeview.insert(values,'end',text=arr)
insert arguments: parent='item3', index='end', text='file.ext'
Traceback (most recent call last):
...
    res = self.tk.call(self._w, "insert", parent, index, *opts)
_tkinter.TclError: Item item3 not found

您需要在循环中获取所有找到的文件夹的 iid 并将它们用作当前父级。


from tkinter import *
import os
from tkinter import ttk


app = Tk()
app.title("GUI Application of Python")

ttk.Label(app, text="Treeview(hierarchical)").pack()

treeview = ttk.Treeview(app, show='tree')
treeview.pack(fill='both', expand=True)


def new_folder(parent_path, directory_entries,
               parent_iid, f_image, d_image):
    """Creates a graphical representation of the structure
    of subdirectories and files in the specified parent_path.

    Recursive tree building for large directories can take some time.

    :param parent_path: directory path, string
    :param directory_entries: List[str]
           a list containing the names of the entries in the parent_path
           obtained using os.listdir()
    :param parent_iid: unique identifier for a treeview item, string
    :param f_image: file icon, tkinter.PhotoImage
    :param d_image: directory icon, tkinter.PhotoImage
    :return: None
    """
    for name in directory_entries:
        item_path = parent_path+os.sep+name
        # optional: file does not exist or broken symbolic link
        #if not os.path.exists(item_path):
            #print("Skipped:", item_path)
            #continue
        if os.path.isdir(item_path):
            # set subdirectory node
            subdir_iid = treeview.insert(parent=parent_iid,
                                         index='end',
                                         text=name,
                                         image=d_image)
            try:
                # pass the iid of the subdirectory as parent iid
                # all files/folders found in this subdirectory
                # will be attached to this subdirectory node
                subdir_entries = os.listdir(item_path)
                new_folder(parent_path=item_path,
                           directory_entries=subdir_entries,
                           parent_iid=subdir_iid,
                           f_image=f_image,
                           d_image=d_image)
            except PermissionError:
                pass
        else:
            treeview.insert(parent=parent_iid,
                            index='end',
                            text=name,
                            image=f_image)
        print(item_path)


# png 16x16 -> base64 strings for tkinter.PhotoImage
file_img = """
iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABGdB
TUEAALGPC/xhBQAAAAlwSFlzAAAScwAAEnMBjCK5BwAAAalJREFU
OE99kUtLAmEUhg38Af6a6he06SJIF8FFREVtqkVEkBB2IcJIRDAk
80KrNq3CWihuuuh4GVIwM9BSa0bHUXGc8X7p1Bc6jtLLxzAwz3Pm
nPMNcaWySCR6CYVoOgMvzWaz3W63Wq1GowFPsVg8PDIqkUjg019A
gEOSJHAowEHAhDidzmAwGI3FEPZTXSDwafiJ3W5nWRbH8TSVGSAI
aBDi8TiGYT6fz2QyCwU+Dc0ADanX67XfWK3W/4QOjYRqtWqxWIQ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Voy40SuC6p0lgyo
+PS4e/aM8835GHALDWvY2LVybAxcVs881XtAUEyjC8SEGPx7bfu2
4/mgzfwiEgSz4dcJixRxjq5YVtEM1r6oHiDGuP9RwHS1TOaP/tCj
/d+VL1STn8NNZQAAAABJRU5ErkJggg==
"""
file_image = PhotoImage(data=file_img)
dir_image = PhotoImage(data=dir_img)

# adds a parent item to the tree
# and returns the item's iid value (generated automatically)
parent_iid = treeview.insert(parent='',
                             index='0',
                             text='Documents',
                             open=True,
                             image=dir_image)

start_path = os.path.expanduser(r"~\Documents")
start_dir_entries = os.listdir(start_path)

# inserting items to the treeview
new_folder(parent_path=start_path,
           directory_entries=start_dir_entries,
           parent_iid=parent_iid,
           f_image=file_image,
           d_image=dir_image)

app.mainloop()

ttk.Treeview Windows 上的示例目录树

站长简介:高级工程师,爱好交友,无偿辅导python和前端,技术交流,面试指导,找工作指导,瞎聊都可加我微信i88811i哈,欢迎欢迎!也欢迎加入程序员交流群,专属程序员的圈子,加我微信拉你进群
欢迎关注我的公众号:程序员总部,关注公众号回复python,免费领取 全套python视频教程,关注公众号回复充值+你的账号,免费为您充值1000积分







所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/1256402/7f0355304621031d990b/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

23 0
收藏该文
已收藏

评论内容:(最多支持255个字符)