使用它的 Id 获取树视图项的文本 - Treeview Tkinter [英] Get the text of a treeview item using it's Id - Treeview Tkinter

查看:81
本文介绍了使用它的 Id 获取树视图项的文本 - Treeview Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在双击时获取树视图项 subdir3 的显示文本.我知道 'text' 不正确,因为 print tree.set('subdir3') 打印列和值的字典,文本不是其中的一部分,但我可以在我找到的有限文档中找不到任何相关信息.

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.

这是我的代码:

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()

所需的输出:子目录3

推荐答案

可以使用 identify 方法获取光标下的项目,使用 item 方法获取获取有关该项目的信息:

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"]

这篇关于使用它的 Id 获取树视图项的文本 - Treeview Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆