【问题标题】:Modify the tag of the selected row in python tkinter treeview修改python tkinter treeview中选中行的标签
【发布时间】:2021-01-02 20:10:15
【问题描述】:

我添加了以下代码(取自this post),但是我没有得到想要的解决方案。

def change_colour():
    trv.set(trv.selection()[0],0,tags='changed_tag')        #this causes problem
    trv.tag_configure("changed_tag",foreground="blue",background="yellow")

执行时出现此错误(来自函数的第一行):
TypeError: set() got an unexpected keyword argument 'tags'

我的目标是更改所选项目的标签(在树视图中),所以它的颜色会改变。

【问题讨论】:

  • 您链接的帖子中提供了此问题的解决方案;他们有同样的错误
  • 我知道,但我不是很明白,一个解决方案的例子对我很有帮助。
  • 我正在写一个
  • set 方法不接受 tags 选项。

标签: python python-3.x tkinter


【解决方案1】:

我自己找到了一个解决方案,适用于任何有同样问题的人:

    def high_target():
        selected_item = trv.selection()[0]
        trv.item(selected_item, tags='changed_tag')
        trv.tag_configure("changed_tag",foreground="blue",background="yellow")

【讨论】:

  • 这里不需要配置标签。通常最好在创建 Treeview 小部件时配置标签。
【解决方案2】:

我认为你的代码应该是这样的(虽然没有测试过):

def change_colour(iid):
    trv.item(iid, tags="changed_tag")
    trv.tag_configure("changed_tag", foreground="blue", background="yellow")

在使用trv.insert 创建树视图项的位置,将trv.insert 返回的值存储在变量中,并将其作为参数传递给change_colour。示例:

iid = trv.insert("", "end", values=("a", "b", "c"))
change_colour(iid)

【讨论】:

  • 基本上这会改变每个项目的标签。我不想要那个,而只有 SELECTED 项是我想要的。
  • 对不起@KleitonKurti:我误解了你的问题!
猜你喜欢
  • 2019-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-07
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 2020-09-24
相关资源
最近更新 更多