Get the text of a treeview item using it's Id - Treeview Tkinter

泪湿孤枕 提交于 2019-12-08 05:46:08

问题


I would like to get the display text of the treeview item subdir3 when I double click. I know 'text' is not correct as print tree.set('subdir3') prints a dictionary of columns and values and text is not part of that, but I can't find anything about it in the limited documentation I have found.

Here's my code:

from Tkinter import *
import ttk

root = Tk()

def OnDoubleClick(event):
    print tree.set('subdir3')['text']


tree = ttk.Treeview(root)

tree["columns"]=("one","two")
tree.heading("one", text="coulmn A")
tree.heading("two", text="column B")

tree.insert("", 3, "dir3", text="Dir 3",values=("3A"," 3B"))
tree.insert("dir3", 3, 'subdir3', text="sub dir 3", values=("3A"," 3B"))

tree.bind("<Double-1>", OnDoubleClick)


tree.pack()
root.mainloop()

Desired output: sub dir 3


回答1:


You can use the identify method to get the item under the cursor, and the item method to get information about that item:

def OnDoubleClick(event):
    item = tree.identify("item", event.x, event.y)
    print "you clicked on", tree.item(item)["text"]


来源:https://stackoverflow.com/questions/31793795/get-the-text-of-a-treeview-item-using-its-id-treeview-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!