Sam
Sam

Reputation: 263

How to detect when a pressed mouse enters a widget in a different Frame

I have a ttk Treeview (in a Frame in a Tk)) and a tk Listbox (in a Toplevel). My intention is to do the following:

  1. press mouse button 1 in the Treeview
  2. move (the pressed) mouse over to the Listbox
  3. set the focus to the Toplevel containing the Listbox when the mouse enters

Right now I can set the focus of the Toplevel when an unpressed mouse enters. I used .bind("<Enter>").

When I use .bind("<B1-Motion>") I can detect movement of a pressed mouse. But only if I pressed the mouse within the list.

When I use.bind("<B1-Enter>")I don't get any events.

How can I detect if a somewhere else pressed mouse enters my Listbox?

Upvotes: 3

Views: 2131

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 386275

You will need to set a binding on <B1-Motion>. Then, in the callback you can use the winfo_containing method to determine which widget is under the cursor. You can use event.x_root and event.y_root as arguments to winfo_containing to find the widget:

self.bind_all("<B1-Motion>", self.on_motion)
...
def on_motion(self, event):
    widget = self.winfo_containing(event.x_root, event.y_root)
    ...

Upvotes: 8

Related Questions