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?
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?
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()