[英]tkinter ttk iterating through treeview
我使用 tkinter ttk GUI 來顯示服務器中文件的數據。 信息存儲在 ttk 樹視圖中並顯示為表格。 目標是讓用戶能夠過濾這些行,以便在用戶完成過濾后只能對樹視圖中可見的那些行執行功能。
問題是,我找不到遍歷樹視圖的方法。 我需要能夠做這樣的事情:
def filterTreeview(treeviewToFilter, tvColumn, stringVariable):
for tvRow in treeviewToFilter:
if tvRow.getValue(tvColumn) != stringVariable:
tvRow.detach()
我怎樣才能做到這一點?
作為次要問題,有人知道更好的方法嗎? 有什么理由使用樹視圖而不是簡單的數組嗎? 如何對數據數組進行過濾,然后從頭開始重新創建樹視圖表?
我花了很多時間閱讀教程以尋找信息,但到目前為止我還沒有成功理解在樹視圖中使用數據的方式:
python ttk 樹視圖排序數字http://www.tkdocs.com/tutorial/tree.html
https://fossies.org/dox/Python-3.5.2/classtkinter_1_1ttk_1_1Treeview.html
要遍歷樹視圖的各個條目,請獲取樹視圖項“id”的列表,並使用它在“for”循環中進行迭代:
#Column integer to match the column which was clicked in the table
col=int(treeview.identify_column(event.x).replace('#',''))-1
#Create list of 'id's
listOfEntriesInTreeView=treeview.get_children()
for each in listOfEntriesInTreeView:
print(treeview.item(each)['values'][col]) #e.g. prints data in clicked cell
treeview.detach(each) #e.g. detaches entry from treeview
這滿足了我的需求,但如果有更好的方法,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.