简体   繁体   English

鼠标悬停时如何更改行颜色? Tkinter Treeview

[英]How to change row color when mouse over? Tkinter Treeview

I have been working with the Tkinter ttk.Treeview widget lately and I have been able to change a lot the widget's style using a ttk.Style , but sadly I am unable to find a solution to this problem:我最近一直在使用 Tkinter ttk.Treeview 小部件,我已经能够使用ttk.Style更改很多小部件的样式,但遗憾的是我无法找到解决此问题的方法:

How can I change the item's colour when the cursor/mouse is over?光标/鼠标结束时如何更改项目的颜色?

Like the activebackground option of Tkinter.Button .就像 Tkinter.Button 的activebackground选项Tkinter.Button Like the VS CODE treeview: When you are navigating the explorer, the file/ folder under the cursor changes the background colour.与 VS CODE treeview 一样:当您浏览资源管理器时,cursor 下的文件/文件夹会更改背景颜色。

You can control the color of a row with a tag, so the first part of the solution is to define a tag to highlight a row:你可以用一个标签来控制一行的颜色,所以解决方案的第一部分是定义一个标签来高亮一行:

tree.tag_configure('highlight', background='lightblue')

Next, write a method that will remove that tag from all items in the tree and then add it for the item under the cursor.接下来,编写一个方法,从树中的所有项目中删除该标签,然后将其添加到 cursor 下的项目中。 The underlying tk widget has methods for adding and removing tags but those methods aren't exposed, so we'll need to directly call the underlying tk code.底层 tk 小部件具有添加和删除标签的方法,但这些方法没有公开,因此我们需要直接调用底层 tk 代码。

def highlight_row(event):
    tree = event.widget
    item = tree.identify_row(event.y)
    tree.tk.call(tree, "tag", "remove", "highlight")
    tree.tk.call(tree, "tag", "add", "highlight", item)

Finally, bind the function to the <Motion> event:最后,将 function 绑定到<Motion>事件:

tree.bind("<Motion>", highlight_row)

Here is a complete working example:这是一个完整的工作示例:

import tkinter as tk
from tkinter import ttk


def highlight_row(event):
    tree = event.widget
    item = tree.identify_row(event.y)
    tree.tk.call(tree, "tag", "remove", "highlight")
    tree.tk.call(tree, "tag", "add", "highlight", item)

root = tk.Tk()

tree = ttk.Treeview(root, style = 'W.TButton')
vsb = ttk.Scrollbar(root, command=tree.yview)
tree.configure(yscrollcommand=vsb.set)

vsb.pack(side="right", fill="y")
tree.pack(side="left", fill="both", expand=True)

tree.tag_configure('highlight', background='lightblue')
tree.bind("<Motion>", highlight_row)


for i in range(100):
    tree.insert("", "end", text=f"Item #{i+1}")
    tree.tag_bind(i, '<Motion>', highlight_row)

root.mainloop()

截屏

Here is an example to do it.这是一个例子。 You may further improve this code.您可以进一步改进此代码。

import tkinter as tk
from tkinter import ttk


def Enter_event(event):

    for x in tree.get_children():
        tree.tag_configure(tree.item(x)['tags'], background='')

    tree.tag_configure(tree.item(tree.identify_row(event.y))['tags'], background='yellow')

root = tk.Tk()
style = ttk.Style() 
  
  
style.configure('W.TButton') 

tree = ttk.Treeview(root, style = 'W.TButton')

tree.pack()
for i in range(10):
    tree.insert("", "end", text="Item %s" % i, tags=i)
    tree.tag_bind(i, '<Motion>', Enter_event)
        
root.mainloop()

Explanation:解释:

use set each cell a tag then bind each tag to motion event and call event handler.使用为每个单元格设置一个标签,然后将每个标签绑定到运动事件并调用事件处理程序。

  • tree.item(x)['tags'] returns the tag of specified iid. tree.item(x)['tags']返回指定 iid 的标签。

  • tree.get_children() returns all the children iid tree.get_children()返回所有孩子的 iid

  • tree.identify_row(event.y) returns the row at y position. tree.identify_row(event.y)返回 y position 处的行。

  • tree.tag_configure(tree.item(tree.identify_row(event.y))['tags'], background='yellow') will change the background of the specified tag. tree.tag_configure(tree.item(tree.identify_row(event.y))['tags'], background='yellow')将改变指定标签的背景。

I tried Enter and leave event but it's not being recognized.我尝试了进入和离开事件,但它没有被识别。 (Also, note setting style configure to 'Treeview' doesn't work for me. If it does for any of the readers pls inform me) (此外,将注释设置样式配置为“Treeview”对我不起作用。如果对任何读者有用,请通知我)

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

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