问题描述
限时送ChatGPT账号..我想弄清楚如何走一棵树,然后在一个用户可以浏览的窗口中显示输出,就像我电脑左侧的那样.最终我计划拥有一个完全可浏览的窗口,就像你在右手边一样.这是我到目前为止所拥有的,我猜它是伪代码和实际代码的混合.这是在使用 python 的 Linux 机器上使用的.我不是在寻找任何代码,而是主要帮助我如何使用 tkinter 完成此任务.也许只是我一个人,但我找不到多少帮助我解决问题的帮助 - 大多数只是告诉我如何显示目录等.任何帮助将不胜感激.
I am trying to figure out how to walk a tree and then display the output in a window that a user can navigate through much like that on the left hand side of my computer. Eventually I plan to have a fully browsable window just like you have on the right hand side. Here is what I have so far, I guess it is a mix of pseudo and actual code. This is for use on a Linux machine using python. I'm not looking for any code but mainly help as to how I can accomplish this with tkinter. Perhaps it is just me but I cannot find much help that helps me solve my problem - most just tell me how to display the directories etc. Any help would be greatly appreciated.
我希望这个窗口看起来像这样
I want this window to look like this
My Documents <--------starting directory
My pictures<------subdirectory
picture1.jpg<-inside of subdirectoy
picture2.jpg
1234.exe<---------random file inside of my documents
我还想在目录或子目录旁边放一个小文件夹图片.
I want to have a small folder picture next to a directory or a subdirectory also.
start at root
create window with tk
for dirname,subdirList,filelist in os.walk(root)
create new item(dirname)
for i in subdirList: #not sure what I would have to do to only
have subdirs showing once the directory was
clicked once
append i to item 1
for fname in fileList:
append fname to item 1
else:
item +=1
推荐答案
您可以使用小部件 ttk.Treeview
来完成,这里有一个演示 dirbrowser.py
这样做.所以我在这里所能做的就是给出它的一个剥离版本并解释它是如何工作的.首先,这是简短版本:
You can do it using the widget ttk.Treeview
, there is a demo dirbrowser.py
that does that. So all I can do here is give a stripped version of it and explain how it works. First, here is the short version:
import os
import sys
import Tkinter
import ttk
def fill_tree(treeview, node):
if treeview.set(node, "type") != 'directory':
return
path = treeview.set(node, "fullpath")
# Delete the possibly 'dummy' node present.
treeview.delete(*treeview.get_children(node))
parent = treeview.parent(node)
for p in os.listdir(path):
p = os.path.join(path, p)
ptype = None
if os.path.isdir(p):
ptype = 'directory'
fname = os.path.split(p)[1]
oid = treeview.insert(node, 'end', text=fname, values=[p, ptype])
if ptype == 'directory':
treeview.insert(oid, 0, text='dummy')
def update_tree(event):
treeview = event.widget
fill_tree(treeview, treeview.focus())
def create_root(treeview, startpath):
dfpath = os.path.abspath(startpath)
node = treeview.insert('', 'end', text=dfpath,
values=[dfpath, "directory"], open=True)
fill_tree(treeview, node)
root = Tkinter.Tk()
treeview = ttk.Treeview(columns=("fullpath", "type"), displaycolumns='')
treeview.pack(fill='both', expand=True)
create_root(treeview, sys.argv[1])
treeview.bind('<<TreeviewOpen>>', update_tree)
root.mainloop()
首先列出sys.argv[1]
给出的路径中存在的文件和目录.你不想在这里使用 os.walk
因为你只显示给定路径中直接可用的内容,而没有进入更深层次.然后代码继续显示这些内容,并为目录创建一个虚拟子项,因此这个 Treeview
条目将显示为可以进一步扩展的内容.然后,您可能会注意到,有一个绑定到虚拟事件 <<>
每当用户单击 Treeview
中的项目时就会触发可以进一步扩展(在这种情况下,代表目录的条目).当事件被触发时,代码最终会删除之前创建的虚拟节点,现在使用指定目录中存在的内容填充节点.其余代码由有关在 Treeview
中存储附加信息以使一切正常工作的详细信息组成.
It starts by listing the files and directories present in the path given by sys.argv[1]
. You don't want to use os.walk
here as you show only the contents directly available in the given path, without going into deeper levels. The code then proceeds to show such contents, and for directories it creates a dummy children so this Treeview
entry will be displayed as something that can be further expanded. Then, as you may notice, there is a binding to the virtual event <<TreeviewOpen>>
which is fired whenever the user clicks an item in the Treeview
that can be further expanded (in this case, the entries that represent directories). When the event is fired, the code ends up removing the dummy node that was created earlier and now populates the node with the contents present in the specified directory. The rest of the code is composed of details about storing additional info in the Treeview
to make everything work.
这篇关于python gui树走的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论