I'm trying to change the cursor in my Tkinter app when the mouse moves. As a simplified example:
import tkinter as tk
root = tk.Tk()
root.geometry("500x200")
frame2 = tk.Frame(root)
frame = tk.Canvas(frame2)
def on_mouse_motion(event):
if event.x > 200:
frame2.config(cursor="xterm")
else:
frame2.config(cursor="crosshair")
frame.bind("<Motion>", on_mouse_motion)
frame.pack(expand=True, fill="both")
frame2.pack(expand=True, fill="both")
root.mainloop()
However, the cursor updates the first one or two times and then stops changing.
I've noticed that it works if I change the cursor on the Canvas and not the container, but because of limitations with the widget I'm working with (which is a non-standard widget but also faces the same problem), that's not possible. I've tried using update
and update_idletasks
, but with no avail. The only thing that seems to work is if I put print()
in the callback, which is kind of wierd, and not a fantastic solution.
I was wondering if anyone has encountered this issue in the past and knows how to work around/fix it?
I'm trying to change the cursor in my Tkinter app when the mouse moves. As a simplified example:
import tkinter as tk
root = tk.Tk()
root.geometry("500x200")
frame2 = tk.Frame(root)
frame = tk.Canvas(frame2)
def on_mouse_motion(event):
if event.x > 200:
frame2.config(cursor="xterm")
else:
frame2.config(cursor="crosshair")
frame.bind("<Motion>", on_mouse_motion)
frame.pack(expand=True, fill="both")
frame2.pack(expand=True, fill="both")
root.mainloop()
However, the cursor updates the first one or two times and then stops changing.
I've noticed that it works if I change the cursor on the Canvas and not the container, but because of limitations with the widget I'm working with (which is a non-standard widget but also faces the same problem), that's not possible. I've tried using update
and update_idletasks
, but with no avail. The only thing that seems to work is if I put print()
in the callback, which is kind of wierd, and not a fantastic solution.
I was wondering if anyone has encountered this issue in the past and knows how to work around/fix it?
Share Improve this question edited yesterday Andereoo asked yesterday AndereooAndereoo 9581 gold badge9 silver badges28 bronze badges 5 |1 Answer
Reset to default 0I couldn't find any issue per se, am not really sure what the issue is , added a couple some 'feedback' to your code , hopefully that will help you ... if not ... you need to better explain what you think the issue is ...
import tkinter as tk
root = tk.Tk()
root.title("Event-Driven Messages in Frame")
root.geometry("500x200")
frame2 = tk.Frame(root)
frame = tk.Canvas(frame2)
message_label = tk.Label(frame, text="", bg="lightgray", font=("Arial", 16))
message_label.pack(pady=20)
def on_mouse_motion(event):
if event.x > 200:
frame2.config(cursor="xterm")
message_label.config(text=f"XTERM: x:{event.x}, y:{event.y})")
else:
frame2.config(cursor="crosshair")
message_label.config(text=f"CROSSHAIR: at x:({event.x}, y:{event.y})")
frame.bind("<Motion>", on_mouse_motion)
frame.pack(expand=True, fill="both")
frame2.pack(expand=True, fill="both")
root.mainloop()
event.x
being200
. That's what pops to me as a potential issue...your code is like 15 lines. It sounds like your cursor is having issues after a few movements, that's the only place that changes – ViaTech Commented yesterdayx
in a variable and use that instead ofevent.x
? Perhaps you are having an issue with the event object not doing what you expect. A global tracker of where the mouse is doesn't sound like a bad option anyways – ViaTech Commented yesterday