我是python新手,但有java經驗。很不一樣的動物。我有一種基于遍歷目錄結構創建 json/dictionary 的方法,并且我有它創建如下所示的 json 的方法。我正在嘗試另一種方法來基于它填充樹視圖。我在 stackoverflow 上看到了幾個例子,并試圖遵循它們。下面是我想出的,但它總是在瀏覽第一個目錄后出錯,就像它忘記了它的位置一樣。回傳以下錯誤:
Traceback (most recent call last):
File "temp.py", line 147, in <module>
load_treeview_from_project_dictionary(myData, tree_view)
File "temp.py", line 134, in load_treeview_from_project_dictionary
load_treeview_from_project_dictionary(c, tree_view, int(c['index']))
File "temp.py", line 136, in load_treeview_from_project_dictionary
tree_view.insert(parent_id, "end", text=c['name'], values=("", ""))
File "C:\Users\...\AppData\Local\Programs\Python\Python39\lib\tkinter\ttk.py", line 1364, in insert
res = self.tk.call(self._w, "insert", parent, index, *opts)
_tkinter.TclError: Item 1 not found
我作業了幾個小時試圖重構并解決這個問題,但沒有成功。我現在正在聯系這個偉大的社區,指出我的方式錯誤!請幫忙。
import json
import tkinter
from tkinter import ttk
from tkinter.ttk import Label
myData = {
"name": "MyRoot",
"type": "directory",
"index": 0,
"children": [
{
"name": "SubDir1",
"type": "directory",
"index": 1,
"children": [
{
"name": "apic.png",
"type": "file"
}
]
},
{
"name": "SubDir2",
"type": "directory",
"index": 2,
"children": [
{
"name": "somefile.txt",
"type": "file"
},
{
"name": "anotherfile.txt",
"type": "file"
}
]
},
{
"name": "SubDir3",
"type": "directory",
"index": 3,
"children": []
},
{
"name": "SubDir4",
"type": "directory",
"index": 4,
"children": [
{
"name": "asomefile.txt",
"type": "file"
},
{
"name": "bsomefile.txt",
"type": "file"
},
{
"name": "csomefile.txt",
"type": "file"
},
{
"name": "dsomefile.txt",
"type": "file"
},
{
"name": "esomefile.txt",
"type": "file"
}
]
},
{
"name": "SubDir5",
"type": "directory",
"index": 5,
"children": [
{
"name": "NesterDir1",
"type": "directory",
"index": 6,
"children": []
}
]
},
{
"name": "SubDir6",
"type": "directory",
"index": 7,
"children": []
},
{
"name": "SubDir7",
"type": "directory",
"index": 8,
"children": []
},
{
"name": "SubDir8",
"type": "directory",
"index": 9,
"children": []
},
{
"name": "SubDir9",
"type": "directory",
"index": 10,
"children": []
},
{
"name": "SubDir10",
"type": "directory",
"index": 11,
"children": []
},
{
"name": "SubDir11",
"type": "directory",
"index": 12,
"children": []
}
]
}
def load_treeview_from_project_dictionary(data, my_tree_view, parent_id=None):
print("this:" data['name'] " called function!")
if parent_id is None:
my_tree_view.insert("", "0", text=data['name'], values=("", "")) # applies to first iteration only
if data['children'] is not None:
for c in data['children']:
print("child: " c['name'])
if c['type'] == "directory":
my_tree_view.insert('', int(c['index']), text=c['name'], values=("", ""))
load_treeview_from_project_dictionary(c, my_tree_view, int(c['index']))
else:
my_tree_view.insert(parent_id, "end", text=c['name'], values=("", ""))
load_treeview_from_project_dictionary(c, my_tree_view, parent_id)
root = tkinter.Tk()
main_label = Label(root, text="Directory Tree")
tree_view = ttk.Treeview(root, height=23)
tree_view.heading("#0", text="Directory Structure")
load_treeview_from_project_dictionary(myData, tree_view)
main_label.pack()
tree_view.pack()
root.mainloop()
提前致謝!
uj5u.com熱心網友回復:
所以。在查看了 DL 發布的教程鏈接,然后一遍又一遍地梳理我的代碼和除錯后,我得出的結論是遞回太多了。觀察流程我發現該方法總是在第一個檔案添加到樹后停止。在檔案插入后進行遞回呼叫修復了大部分問題。我仔細檢查了插入程序,發現我可以將 json 中的索參考于樹視圖的 iid。然后我決定使用 treeview.move 在插入條目時將條目放置在我想要的位置會更有效。下面是我想出的,效果很好。我在這里為遇到相同問題的其他人發布此資訊。在代碼之后,有一個生成的樹視圖的螢屏截圖(或由于我的等級而指向它的鏈接——我稍后會嘗試修復它)
import json
import tkinter
from tkinter import ttk
from tkinter.ttk import Label
myData = {
"name": "MyRoot",
"type": "directory",
"index": 0,
"children": [
{
"name": "SubDir1",
"type": "directory",
"index": 1,
"children": [
{
"name": "apic.png",
"type": "file"
}
]
},
{
"name": "SubDir2",
"type": "directory",
"index": 2,
"children": [
{
"name": "somefile.txt",
"type": "file"
},
{
"name": "anotherfile.txt",
"type": "file"
}
]
},
{
"name": "SubDir3",
"type": "directory",
"index": 3,
"children": []
},
{
"name": "SubDir4",
"type": "directory",
"index": 4,
"children": [
{
"name": "asomefile.txt",
"type": "file"
},
{
"name": "bsomefile.txt",
"type": "file"
},
{
"name": "csomefile.txt",
"type": "file"
},
{
"name": "dsomefile.txt",
"type": "file"
},
{
"name": "esomefile.txt",
"type": "file"
}
]
},
{
"name": "SubDir5",
"type": "directory",
"index": 5,
"children": [
{
"name": "NestedDir1",
"type": "directory",
"index": 6,
"children": [
{
"name": "yetAnotherfile.txt",
"type": "file"
}
]
}
]
},
{
"name": "SubDir6",
"type": "directory",
"index": 7,
"children": []
},
{
"name": "SubDir7",
"type": "directory",
"index": 8,
"children": []
},
{
"name": "SubDir8",
"type": "directory",
"index": 9,
"children": []
},
{
"name": "SubDir9",
"type": "directory",
"index": 10,
"children": []
},
{
"name": "SubDir10",
"type": "directory",
"index": 11,
"children": []
},
{
"name": "SubDir11",
"type": "directory",
"index": 12,
"children": []
}
]
}
def load_treeview_from_project_dictionary(data, my_tree_view, parent_id=None):
print('this:' data['name'] ' called function!')
if parent_id is None:
my_tree_view.insert('', '0', text=data['name'], iid=0) # applies to first iteration only
for c in data['children']:
print('child: ' c['name'])
if c['type'] == 'directory':
my_tree_view.insert('', 'end', text=c['name'], iid=c['index'])
my_tree_view.move(c['index'], data['index'], 'end')
load_treeview_from_project_dictionary(c, my_tree_view, data['index'])
else:
file_index = my_tree_view.insert('', 'end', text=c['name'])
my_tree_view.move(file_index, data['index'], 'end')
root = tkinter.Tk()
main_label = Label(root, text='Directory Tree')
tree_view = ttk.Treeview(root, height=23)
tree_view.heading('#0', text='Directory Structure')
load_treeview_from_project_dictionary(myData, tree_view)
main_label.pack()
tree_view.pack()
root.mainloop()
樹狀圖截圖
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/444592.html
標籤:json python-3.x 递归 树视图
