[英]Get the text of a treeview item using it's Id - Treeview Tkinter
I would like to get the display text of the treeview item subdir3
when I double click. 当我双击时,我想获取树视图项目subdir3
的显示文本。 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. 我知道'text'
是不正确的,因为print tree.set('subdir3')
打印列和值的字典,而text不是其中的一部分,但是我在有限的文档中找不到关于它的任何内容。
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
所需的输出: sub dir 3
You can use the identify
method to get the item under the cursor, and the item
method to get information about that item: 您可以使用identify
方法获取光标下方的项目,并使用item
方法获取有关该项目的信息:
def OnDoubleClick(event):
item = tree.identify("item", event.x, event.y)
print "you clicked on", tree.item(item)["text"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.