當查詢(Python)ttk.Treeview
item
的open
選項時,我遇到不利行爲。一個節點(item
)的知名度可以通過做類似設置:檢索ttk.Treeview項目的「打開」選項作爲布爾值
tree.item(someItemID, open=True) # or
tree.item(someItemID, open=False)
而且我的假設是open
選項可以查詢得到一個布爾真/假。但是,這似乎並非如此。考慮這個腳本:
from Tkinter import *
from ttk import Treeview
def check_state():
for row in tree.get_children():
opened = tree.item(row, option='open')
print row, 'opened:', opened, '(type: %s)' % str(type(opened)), 'Got:',
if not opened:
print 'False (bool)'
elif opened == 'true':
print 'equal to string "true"'
elif opened == 'false':
print 'equal to string "false"'
elif opened:
print 'True (bool)'
else:
print 'something entirely different(!)'
print
win = Frame()
tree = Treeview(win)
win.pack()
tree.pack()
Button(win, text='View state', command=check_state).pack()
level1 = ['C:\\dir1', 'C:\\dir2', 'C:\\dir3']
level2 = ['one.txt', 'two.txt', 'three.txt']
for L in level1:
iid = tree.insert('', END, text=L)
for M in level2:
tree.insert(iid, END, text=M)
win.mainloop()
運行時,它顯示一個小的Treeview控件,填充了僞目錄和文件名。 之前打開或關閉任何頂級節點,按下按鈕轉儲open
選項狀態到標準輸出。應該是這樣的:
I001 opened: 0 (type: <type 'int'>) Got: False (bool)
I005 opened: 0 (type: <type 'int'>) Got: False (bool)
I009 opened: 0 (type: <type 'int'>) Got: False (bool)
現在打開其中一個節點上,再次按下按鈕。現在它傾倒:
I001 opened: 0 (type: <type 'int'>) Got: False (bool)
I005 opened: 0 (type: <type 'int'>) Got: False (bool)
I009 opened: true (type: <type '_tkinter.Tcl_Obj'>) Got: True (bool)
最後,關閉所有節點並再次按下按鈕。它轉儲:
I001 opened: 0 (type: <type 'int'>) Got: False (bool)
I005 opened: 0 (type: <type 'int'>) Got: False (bool)
I009 opened: false (type: <type '_tkinter.Tcl_Obj'>) Got: True (bool)
脫穎而出,以我的事情:
- 不一致:當初始化爲
int
S,後來分配的值是_tkinter
對象 - 布爾比較失敗:儘管
_tkinter
對象渲染作爲字符串「真」或「假」,他們不計算
_tkinter
對象)
任何人都知道給出了什麼?我如何可靠地確定Treeview項目的打開/關閉狀態?