[Python] Cursor not always updating in Tkinter

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to update 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?

    Continue reading...

Compartilhe esta Página