Python Forum
bind hover on tkinter.ttk.Treeview
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bind hover on tkinter.ttk.Treeview
I have been trying to find an example that shows how to bind mouse over Treeview cell without much success.
I thought that '<Enter>' could be used, but that is on the widget level, not the cell level
Perhaps I am not working my search correctly, but I can't find a single example or discussion on the subject
I can't remember all that I have tried, but the one I thought would work was 'hover + tkinter.ttk.Treeview'

This looked promising (and still may be), but the entry for Treeview, has no mention of active color. However,
there is mention in spinbox, and other widgets, and I haven't tried that, so I will do that next.

In the mean time, if anyone has an example, I'd love to see it.

Thanks Larry
import tkinter as tk
import tkinter.ttk as ttk

class TreeBuilder():

    def __init__(self, parent):
        self.parent = parent

    def build(self):

        self.tree = ttk.Treeview(self.parent)

        self.tree.column("one", width=100 )
        self.tree.column("two", width=100)
        self.tree.heading("one", text="coulmn A")
        self.tree.heading("two", text="column B")

        self.tree.insert("" , 0,    text="Line 1", values=("1A","1b"))

        id2 = self.tree.insert("", 1, "dir2", text="Dir 2")
        self.tree.insert(id2, "end", "dir 2", text="sub dir 2", values=("2A","2B"))

        # or alternatively:
        self.tree.insert("", 3, "dir3", text="Dir 3")
        self.tree.insert("dir3", 3, text=" sub dir 3",values=("3A"," 3B"))

        # Here begins the key step
        self.tree.tag_configure('focus', background='yellow')
        self.tree.bind("<Motion>", self.mycallback)

        self.last_focus = None

        # ................................................

    def mycallback(self, event):

        _iid = self.tree.identify_row(event.y)

        if _iid != self.last_focus:
            if self.last_focus:
                self.tree.item(self.last_focus, tags=[])
            self.tree.item(_iid, tags=['focus'])
            self.last_focus = _iid

if __name__=='__main__':

    root = tk.Tk()
    tree = TreeBuilder(root)
Now if I could ever remember what I was writing in January.
I'll will use this!
Sorry. Perhaps too late, but I found your question 2 days ago, when I was looking for a solution for the same problem.
Don't be sorry, need to keep on learning. This is a common enough thing that I will be
using it again soon.

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to insert data json to treeview tkinter? Shakanrose 8 1,000 Jan-19-2023, 03:58 PM
Last Post: Shakanrose
  [Tkinter] Different rows colours in treeview tkinter Krissstian 1 459 Nov-20-2022, 09:59 PM
Last Post: woooee
  [PyQt] Hover over highlighted text and open popup window DrakeSoft 2 420 Oct-29-2022, 04:30 PM
Last Post: DrakeSoft
  [Tkinter] bind menator01 1 885 Apr-15-2022, 08:47 PM
Last Post: menator01
  [Tkinter] About Tkinter Treeview.selection_get() usage. water 3 4,870 Feb-12-2022, 02:19 PM
Last Post: water
  [Tkinter] [split] Is there a way to embed a treeview as a row inside another treeview? CyKlop 5 2,212 Oct-20-2021, 12:14 AM
Last Post: CyKlop
  tkinter - update/refresh treeview snakes 4 14,130 May-13-2021, 07:10 AM
Last Post: snakes
  [Tkinter] Glow text of clickable object on hover with transition andy 6 3,915 May-11-2021, 07:39 AM
Last Post: andy
  [Tkinter] bind lambda keypress counter knoxvilles_joker 15 5,698 Apr-19-2021, 01:56 AM
Last Post: knoxvilles_joker
  [Tkinter] acceleration of data output in treeview tkinter Vladimir1984 4 2,980 Nov-21-2020, 03:43 PM
Last Post: Vladimir1984

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020