2 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊
CPython的源代碼中有一個(gè)示例,說明如何用目錄的內(nèi)容遞歸地填充Treeview,這基本上是它的工作方式(為了更好的可讀性,我刪除了事件綁定并將其包裝在一個(gè)類中):
import os
import tkinter as tk
import tkinter.ttk as ttk
class App(tk.Frame):
def __init__(self, master, path):
tk.Frame.__init__(self, master)
self.tree = ttk.Treeview(self)
ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
xsb = ttk.Scrollbar(self, orient='horizontal', command=self.tree.xview)
self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)
self.tree.heading('#0', text=path, anchor='w')
abspath = os.path.abspath(path)
root_node = self.tree.insert('', 'end', text=abspath, open=True)
self.process_directory(root_node, abspath)
self.tree.grid(row=0, column=0)
ysb.grid(row=0, column=1, sticky='ns')
xsb.grid(row=1, column=0, sticky='ew')
self.grid()
def process_directory(self, parent, path):
for p in os.listdir(path):
abspath = os.path.join(path, p)
isdir = os.path.isdir(abspath)
oid = self.tree.insert(parent, 'end', text=p, open=False)
if isdir:
self.process_directory(oid, abspath)
root = tk.Tk()
path_to_my_project = # ...
app = App(root, path=path_to_my_project)
app.mainloop()
更新:正如@ArtOfWarfare所提到的,可以使用該<<TreeviewOpen>>事件懶散地填充樹。為了模擬封閉的節(jié)點(diǎn),我使用了一個(gè)空的子項(xiàng),該子項(xiàng)在打開目錄時(shí)將被刪除:
import os
import tkinter as tk
import tkinter.ttk as ttk
class App(object):
def __init__(self, master, path):
self.nodes = dict()
frame = tk.Frame(master)
self.tree = ttk.Treeview(frame)
ysb = ttk.Scrollbar(frame, orient='vertical', command=self.tree.yview)
xsb = ttk.Scrollbar(frame, orient='horizontal', command=self.tree.xview)
self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)
self.tree.heading('#0', text='Project tree', anchor='w')
self.tree.grid()
ysb.grid(row=0, column=1, sticky='ns')
xsb.grid(row=1, column=0, sticky='ew')
frame.grid()
abspath = os.path.abspath(path)
self.insert_node('', abspath, abspath)
self.tree.bind('<<TreeviewOpen>>', self.open_node)
def insert_node(self, parent, text, abspath):
node = self.tree.insert(parent, 'end', text=text, open=False)
if os.path.isdir(abspath):
self.nodes[node] = abspath
self.tree.insert(node, 'end')
def open_node(self, event):
node = self.tree.focus()
abspath = self.nodes.pop(node, None)
if abspath:
self.tree.delete(self.tree.get_children(node))
for p in os.listdir(abspath):
self.insert_node(node, p, os.path.join(abspath, p))
if __name__ == '__main__':
root = tk.Tk()
app = App(root, path='.')
root.mainloop()

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
可以通過以下方式在支持Python 3.4及更高版本的第二行和第三行更改導(dǎo)入:
import tkinter as tk
import tkinter.ttk as ttk
tkinter中的小寫字母t代替了Tkinter中的大寫字母T,因?yàn)镻ython 3.4及更高版本不再識(shí)別Tkinter;消除了“無法識(shí)別的參考”錯(cuò)誤。
在較新的Python發(fā)行版中,完全合格的導(dǎo)入指令對(duì)所有點(diǎn)符號(hào)都更加嚴(yán)格,因此tkinter.ttk是ttk所必需的,從而消除了對(duì)重復(fù)的完全合格引用的需求。告誡:有人認(rèn)為導(dǎo)入tk.ttk就足夠了,但是以某種方式引起了參考錯(cuò)誤;消除過多的指針和有條件的宏處理使我選擇了上面的格式-還有其他可能,但這是最容易使用的形式。
path_to_my_project =#...會(huì)引發(fā)錯(cuò)誤,但僅是占位符(盡管仍然可以使用),可以更改為以下內(nèi)容:
path_to_my_project = "" # ...
請(qǐng)記住,如果在Windows中運(yùn)行腳本,則使用反斜杠的文字文件路徑會(huì)引發(fā)錯(cuò)誤。您必須使用前面的反斜杠(雙反斜杠)對(duì)文件路徑url中的反斜杠進(jìn)行轉(zhuǎn)義,如下所示:
path_to_my_project = "C:\\Users\\userName\\Desktop\\projDir" #Windows file paths
在文件路徑中使用反斜杠轉(zhuǎn)義反斜杠可以消除“ Unicode轉(zhuǎn)義字符”錯(cuò)誤,其中“ C:\ Users”解析為在“用戶”中轉(zhuǎn)義控制字符U,而這并不是我們最初想要的。將路徑轉(zhuǎn)換為字符串將無法工作,因?yàn)闀?huì)引發(fā)相同的錯(cuò)誤,因此:
path_to_my_project = str("C:\Users\userName\Desktop\projDir")
...無效。
上面的腳本示例針對(duì)在Windows 10 64bit上運(yùn)行的Python 3.4 64bit進(jìn)行了這些修改,沒有任何問題,并且非常干凈,可靠。
我喜歡它-輕松運(yùn)行(簡(jiǎn)單的更新),沒有錯(cuò)誤,警告或無法解釋的解決方法,bs和hacks。豎起大拇指。
添加回答
舉報(bào)