Python tkinter 鼠标位置"bind('<Motion>')"返回错误的位置



问题是,当光标位于这些小部件ListboxLabelText中的一个上时,位置会变为错误的

右侧

位置错误

from tkinter import *
root = Tk()
root.geometry('250x250')
position = Label(root)
position.place(relx = 0.25, rely = 0.25)
listbox = Listbox(root)
listbox.place(relx = .5, rely = .5, relwidth = .5, relheight = .5)
root.bind('<Motion>', lambda e : position.config(text = f'({e.x},{e.y})'))
root.mainloop()

位置相对于小部件,而不是窗口。当鼠标在列表框上移动时,事件将转到列表框,而不是根窗口。

如果您想要相对于窗口的坐标,请使用e.x_roote.y_root

最新更新