In tkinter with Python 3.7, the default behavior for an event binding is that an "<Enter>" event will not be triggered after the mouse has been clicked down before it has been released. I was intending to implement a scrollable table such that it detects "<Button-1>" (Mouse left-click down) and "<ButtonRelease-1>" (Mouse left-click up) events as well as having each table-row's widgets "<Enter>" event bound to detect when the mouse pointer enters a different table row. In this way I could scroll my table by clicking an row and dragging through a table. My assumption was that "<Enter>" events would be triggered even while the mouse button is held down, which was incorrect. So, my entire scrolling implementation hit a brick wall. I need these events to be triggered while the mouse is down or it just won't work. I'm doing something like:
from tkinter import *
class App:
def __init__(self):
self.root = Tk()
# The name kwarg is used to infer the index of the row in the event handlers.
self.labels = [Label(text=f"Label #{i}", name=f"row-{i}") for i in range(5)]
for row, label in enumerate(self.labels):
label.bind("<Button-1>", self.mouse_down)
label.bind("<ButtonRelease-1>", self.mouse_up)
label.bind("<Enter>", self.mouse_enter)
label.grid(row=row, column=0)
mainloop()
def mouse_up(self, event):
idx = self.index_from_event(event)
# Do some with the row with the passed index
def mouse_down(self, event):
idx = self.index_from_event(event)
# Do some with the row with the passed index
def mouse_enter(self, event):
# I would like for this to be triggered even when the mouse is pressed down.
# However, by default tkinter doesn't allow this.
pass
def index_from_event(self, event):
# Get the index of the row from the labels name string.
return str(event.widget).split('-')[-1]
Any way to enable mouse enter events while the mouse button 1 is held down in tkinter? All the docs on effbot (http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm) say about the enter event is:
<Enter>
The mouse pointer entered the widget (this event doesn’t mean that the user pressed the Enter key!).