在tkinter treevi中有效地标记一行

2024-10-03 13:24:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图标记treeview列中的一行,当它被选中时。在

BadgesView.tag_configure("BadgeOfTheWeek", background="yellow")

在标记此行之前或之后,我希望其他行的所有标记都被程序删除。在

^{pr2}$

我不确定这是否可行,因为我很难确定哪一行是选定的行,以及从哪一行标记。在

感谢任何帮助。在


Tags: 标记程序configuretagbackgroundtreeviewyellowpr2
1条回答
网友
1楼 · 发布于 2024-10-03 13:24:06

Question: ... the other rows to have all their tags removed


Tkinter 8.5 reference - 45. ttk.Treeview
First, some definitions:
item
One of the entities being displayed in the widget. For a file browser, an item might be either a directory or a file. Each item is associated with a textual label, and may also be associated with an image.
iid
Every item in the tree has a unique identifier string called the iid. You can supply the iid values yourself, or you can let ttk generate them.


Treeviewitems before:

item:{'text': 'Text_1', 'values': '', 'tags': ['ALL'], 'image': '', 'open': 0}
item:{'text': 'Text_2', 'values': '', 'tags': ['BadgeOfTheWeek', 'ALL'], 'image': '', 'open': 0}
item:{'text': 'Text_3', 'values': '', 'tags': ['ALL'], 'image': '', 'open': 0}
  • 循环Treeview以获取项的iid

    for iid in self.tree.get_children():
    
  • 条件:'BadgeOfTheWeek'not in电流item(iid)tags。在

        if not 'BadgeOfTheWeek' in self.tree.item(iid)['tags']:
    
  • 若要从此item(iid)中删除所有tags,请将tags=设置为空list或{}

            self.tree.item(iid, tags=[])
    

Treeviewitems after:

item:{'text': 'Text_1', 'values': '', 'tags': '', 'image': '', 'open': 0}
item:{'text': 'Text_2', 'values': '', 'tags': ['BadgeOfTheWeek', 'ALL'], 'image': '', 'open': 0}
item:{'text': 'Text_3', 'values': '', 'tags': '', 'image': '', 'open': 0}

使用Python:3.5-TkVersion:8.6测试

相关问题 更多 >