You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter中光标无法持续更新的问题求助

Tkinter中光标无法持续更新的问题求助

我最近在做Tkinter应用时遇到个头疼的问题——想让鼠标移动时切换光标样式,但试了好几次都不行。先给大家看个简化后的示例代码:

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

可是运行起来后,光标只会更新个一两次,之后就再也不切换了。

我发现如果把光标设置在Canvas组件上是能正常工作的,但我实际用的是非标准组件,没法这么做,只能设置在它的容器上。我试过用update()和update_idletasks()方法,完全没用。现在唯一能让它勉强工作的办法是在回调函数里加个print(),这办法太奇怪了,肯定不是长久之计。

有没有大佬之前遇到过这个问题?能不能给我支个招,怎么解决或者绕开这个问题呀?

备注:内容来源于stack exchange,提问作者Andereoo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 09:28:10