树到(文件资源管理器)
Tree tk (file explorer)
我想创建一个文件资源管理器,但我在使用 id 时遇到了一些问题 = tree.insert()
def SUBS(path):
PO = ""
parent = tree.parent(XOM)
while os.path.exists(path+"/"+PO):
X1 = 0
List = os.listdir(path+"/"+PO)
for element in List:
X1 += 1
if os.path.isfile(path+"/"+element):
tree.insert(XOM,str(X1),text=element)
if os.path.isdir(path+"/"+element):
id = tree.insert(XOM,str(X1),text=element)
PO = element
break
X = 0
Path = "/"
WinT = Tk()
tree = ttk.Treeview(WinT ,height=15)
tree.pack(expand=YES,fill=BOTH)
tree.heading("#0" ,text="Directory")
PATH = os.listdir(Path)
for element in PATH:
X += 1
PaTh = Path+"/"+element
if "//" in PaTh:
PATH = PaTh.replace('//','/')
if "//" not in PaTh:
PATH = PaTh
if "." in element :
tree.insert('',str(X),text=element)
if "." not in element:
XOM = tree.insert('',str(X),text=element)
SUBS(PATH)
所以问题出在函数 SUBS() 问题实际上是因为 m 在开头使用 id = 它应该只通过函数 SUBS() 使用,否则 id 已经存在并且不能工作..我想在 SUBS() 中使用 id = 而不是 X = 但我不能 ..
你要的是递归函数,就是调用自己的函数。
def SUBS(path, parent):
for p in os.listdir(path):
abspath = os.path.join(path, p)
parent_element = tree.insert(parent, 'end', text=p, open=True)
if os.path.isdir(abspath):
SUBS(abspath, parent_element)
path = "C:/"
WinT = Tk()
tree = ttk.Treeview(WinT ,height=15)
tree.pack(expand=YES,fill=BOTH)
tree.heading("#0" ,text="Directory")
root = tree.insert('', 'end', text=path, open=True)
SUBS(path, root)
WinT.mainloop()
它获取目录中的所有元素并将其添加到树形小部件中。如果它是另一个目录而不是函数调用自身,直到它们没有更多的目录。
我想创建一个文件资源管理器,但我在使用 id 时遇到了一些问题 = tree.insert()
def SUBS(path):
PO = ""
parent = tree.parent(XOM)
while os.path.exists(path+"/"+PO):
X1 = 0
List = os.listdir(path+"/"+PO)
for element in List:
X1 += 1
if os.path.isfile(path+"/"+element):
tree.insert(XOM,str(X1),text=element)
if os.path.isdir(path+"/"+element):
id = tree.insert(XOM,str(X1),text=element)
PO = element
break
X = 0
Path = "/"
WinT = Tk()
tree = ttk.Treeview(WinT ,height=15)
tree.pack(expand=YES,fill=BOTH)
tree.heading("#0" ,text="Directory")
PATH = os.listdir(Path)
for element in PATH:
X += 1
PaTh = Path+"/"+element
if "//" in PaTh:
PATH = PaTh.replace('//','/')
if "//" not in PaTh:
PATH = PaTh
if "." in element :
tree.insert('',str(X),text=element)
if "." not in element:
XOM = tree.insert('',str(X),text=element)
SUBS(PATH)
所以问题出在函数 SUBS() 问题实际上是因为 m 在开头使用 id = 它应该只通过函数 SUBS() 使用,否则 id 已经存在并且不能工作..我想在 SUBS() 中使用 id = 而不是 X = 但我不能 ..
你要的是递归函数,就是调用自己的函数。
def SUBS(path, parent):
for p in os.listdir(path):
abspath = os.path.join(path, p)
parent_element = tree.insert(parent, 'end', text=p, open=True)
if os.path.isdir(abspath):
SUBS(abspath, parent_element)
path = "C:/"
WinT = Tk()
tree = ttk.Treeview(WinT ,height=15)
tree.pack(expand=YES,fill=BOTH)
tree.heading("#0" ,text="Directory")
root = tree.insert('', 'end', text=path, open=True)
SUBS(path, root)
WinT.mainloop()
它获取目录中的所有元素并将其添加到树形小部件中。如果它是另一个目录而不是函数调用自身,直到它们没有更多的目录。