-1

I want use python3 (tkinter) to finish this function: When the mouse stop at some place, one message will be shown. When the mouse move away, the message will disappear.

Is there any information for this function?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Tony Wu
  • 3
  • 2
  • Well, you can bind to the `""` event to learn about motion, but you'll have to use a timer to detect when mouse motion stops. That's not considered an "event". – Tim Roberts Sep 22 '21 at 03:35
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 29 '21 at 21:02

1 Answers1

1

This effect can be achieved by combining bind and after.

The motion of cursor is tracked with bind which removes label

after checks every 50 milliseconds for non-movement of cursor and uses place manager to display label.

I've updated answer since bind event.x and y caused an occasional bug in the positioning of the message. (displaying it at top left corner)

Tracked it down to event.x event.y, so replaced it with

x = root.winfo_pointerx() - root.winfo_rootx()

y = root.winfo_pointery() - root.winfo_rooty()

This update solves the problem, now it functions as intended.

import tkinter as tk

root = tk.Tk()
label = tk.Label(root, text = "Message")
x = y = xx = yy = 0

def display(event):
    global x, y
    x = root.winfo_pointerx() - root.winfo_rootx()
    y = root.winfo_pointery() - root.winfo_rooty()
    label.place_forget()

def check(xx, yy):
    global x, y
    if x == xx and y == yy:
        label.place(x = x, y = y, anchor = "s")
    else:
        xx, yy = x, y
    root.after(50, check, xx, yy)

root.bind("<Motion>", display)
root.after(10, check, xx, yy)
root.mainloop()
Derek
  • 1,916
  • 2
  • 5
  • 15