【问题标题】:How to create Tree Structure of Directory with check box using Tkinter如何使用 Tkinter 创建带有复选框的目录树结构
【发布时间】:2017-12-05 09:24:14
【问题描述】:

我正在制作一个 GUI,它将文件夹或文件从源复制到目标。我需要使用复选框制作目录树结构来浏览文件。我引用了这个 How to create a tree view with checkboxes in Python,但我没有找到创建整个文件夹的逻辑。 请帮我解决这个问题。

from tkinter import tix

import os
i = 0

class View(object):

 def __init__(self, root,path):
    self.root = root
    self.path = path
    self.makeCheckList(self.path)


 def makeCheckList(self,path1):
    global i
    self.cl = tix.CheckList(self.root, browsecmd=self.selectItem)
    self.cl.pack(fill='both',expand="yes")

    self.cl.hlist.add("CL1", text=path1)
    self.cl.setstatus("CL1", "off")
    self.check(path1)

    self.cl.autosetmode()

 def selectItem(self, item):
    print (item, self.cl.getstatus(item))

 def check(self,path1):
    global i
    self.path1 = path1
    file = os.listdir(path1)
    for p in file:
        #print(p)
        full_path = path1 +"\\"+ p
        val = "CL1.Item1"
        if os.path.isdir(full_path) != True :
            self.cl.hlist.add("CL1.Item"+ str(i), text=p)
            self.cl.setstatus("CL1.Item"+str(i), "off")
            i = i + 1
    self.dir(path1)         

 def dir(self,path1):

    global i
    self.path1 = path1
    file = os.listdir(path1)
    for folder in file:
        full_path = path1 +"\\"+ folder
        if os.path.isdir(full_path) == True :    
            self.cl.hlist.add("CL1.Item"+str(i), text=folder)
            self.cl.setstatus("CL1.Item"+str(i), "off")
            i = i + 1
            #self.dir(full_path)


def main():
   root = tix.Tk()
   root.geometry("800x400")
   view = View(root,"C:\\")
   root.update()
   root.mainloop()

if __name__ == '__main__':
   main()

我的逻辑在这里不正确。我想要一个递归函数来创建整个目录

【问题讨论】:

  • 发布您正在使用的最少代码
  • 是的,等等……

标签: python user-interface tkinter


【解决方案1】:

如果你想创建一个目录内容的树,你确实需要一个递归函数。对于目录的每个元素,首先将其放入树中,然后,如果是目录,则重新应用函数:

def check(self, parent, path):
    content = os.listdir(path)
    for i, item in enumerate(content):
        full_path = os.path.join(path, item)  # item absolute path
        entry = "{}.Item{}".format(parent, i)  # item path in the tree
        self.cl.hlist.add(entry, text=item)   # add item in the tree
        self.cl.setstatus(entry, "off")
        if os.path.isdir(full_path):  
            # reapply the function if the item is a directory
            self.check(entry, full_path) 

在这个函数中,我需要同时跟踪父目录的绝对路径及其在树中的路径。

然后,要生成树,请使用makeCheckList 方法:

def makeCheckList(self):  # no need of any extra argument here
    self.cl = tix.CheckList(self.root, browsecmd=self.selectItem)
    self.cl.pack(fill='both',expand="yes")
    # add the root directory in the tree
    self.cl.hlist.add("CL1", text=self.path)
    self.cl.setstatus("CL1", "off")
    # build the tree
    self.check("CL1", self.path)  

    self.cl.autosetmode()    

如果你使用python 3.5或更高版本,你可以用os.scandir替换os.listdir来加速你的程序:

def check(self, parent, path):
    with os.scandir(path) as content:
        for i, item in enumerate(content):
            entry = "{}.Item{}".format(parent, i)
            self.cl.hlist.add(entry, text=item.name)
            self.cl.setstatus(entry, "off")
            if item.is_dir():
                self.check(entry, item.path)

【讨论】:

  • 非常感谢。你能给我任何链接,我可以在其中找到 tix 的所有命令,例如当我点击任何项目时它应该给我它的值(路径),如果我检查任何父母目录它应该检查它的所有子路径
  • @python 我不使用tix,而且我不知道任何指向其文档的链接。但是,我已经从 ttk.Treeview 实现了 CheckboxTreeview(这是 stackoverflow.com/questions/5104330/… 的第二个答案),它已经将检查传播给了孩子。
  • 我得到了检查所有的代码,但是如果我想打印复选框的名称,那么该怎么做呢?
  • 这个名字是什么意思?整个路径?
  • 是整个路径,因为我想复制选定的文件夹
猜你喜欢
  • 2018-06-17
  • 1970-01-01
  • 2019-05-10
  • 1970-01-01
  • 2011-07-03
  • 1970-01-01
  • 2021-10-03
  • 2012-08-05
  • 2020-07-17
相关资源
最近更新 更多