【发布时间】: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