1
我在tkinter中使用python 3樹形視圖。 問題是,當我綁定右鍵單擊以獲取右鍵單擊行的行ID時,我最終得到上一個事件的實際行ID。例如,我可以右鍵單擊「項目1」,這會返回我「」,然後右鍵單擊「項目2」,並以rowID的形式返回「項目1」。Tkinter Treeview識別右鍵單擊事件返回以前右鍵單擊的行
def initTreeView(self):
self.treeView = ttk.Treeview(self.treeSectionFrame)
self.treeView.heading('#0', text='Projects')
self.treeView.grid(row=0, column=0, sticky=("N", "S", "E", "W"))
self.treeView.bind('<3>', self.rightClickMenu)
def rightClickMenu(self, event):
def hello():
print("hello!")
# create a popup menu
print(event.x, event.y)
rowID = self.treeView.identify('item', event.x, event.y)
if rowID:
menu = Menu(self.root, tearoff=0)
menu.add_command(label="Undo", command=hello)
menu.add_command(label="Redo", command=hello)
menu.post(event.x_root, event.y_root)
self.treeView.selection_set(rowID)
self.treeView.focus_set()
self.treeView.focus(rowID)
print(rowID)
else:
pass
感謝,
[編輯]
我發現一個骯髒的黑客其中包括使每一個項目相同,其ID的標籤,以便您可以再提取實際的ROWID的。這也可以使用值選項完成。
self.treeView.insert("", "end", "id-1, tags="id-1", text="Project 1")
...
rowID = self.treeView.identify('item', event.x, event.y)
rowID = self.treeView.item(rowID)["tags"] # gives you actual ID
謝謝,現在完美。如何反轉它使它工作? –
@我是Legend,因爲在事件隊列中存在某種重疊(我假設),它看起來像'tkinter'返回到主循環,因爲用戶可以與菜單交互,並且在菜單可見時阻止代碼執行。無論如何,我略微更新了我的答案。 – CommonSense