简体   繁体   English

Python Tkinter TTK Treeview 查找所选项目的 ID 或行

[英]Python Tkinter TTK Treeview Find Selected Items' ID Or Row

I am creating a program which uses a TTK Treeview as an item hierarchy.我正在创建一个使用 TTK Treeview 作为项目层次结构的程序。 So far, the user is able to insert their own values into the tree, but I need to have a check running for when the user clicks on an item in the tree.到目前为止,用户能够将他们自己的值插入到树中,但是我需要在用户单击树中的项目时运行检查。 I need the check to find the selected items ID and return it.我需要检查以找到选定的项目 ID 并将其返回。

The check I have running relies on the <<TreeviewSelect>> bind.我运行的检查依赖于<<TreeviewSelect>>绑定。 Currently, it finds the selected item and stores some information, but I need for it to also find the row the item is in or it's ID.目前,它会找到所选项目并存储一些信息,但我还需要它找到项目所在的行或其 ID。

I tried implementing my own tag using a counter and the tag attribute of Treeview items, but it would return the number as a NoneType .我尝试使用计数器和 Treeview 项目的 tag 属性来实现我自己的标签,但它会将数字作为NoneType返回。 I also tried using the value attribute, but it returned the same problem.我也尝试使用 value 属性,但它返回了同样的问题。

For information, I have read through many sites and a few questions on here, but none (that I have found) answer what I need.有关信息,我已经阅读了许多网站和这里的一些问题,但没有一个(我找到的)回答我的需要。

Any help would be greatly appreciated, thank you for taking the time to read.任何帮助将不胜感激,感谢您花时间阅读。

您可以调用selection方法来获取所有选定项目的列表:

selected_items = self.tree.selection()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM