[英]how do I get mouse position while pressing a button in tkinter?
I'm trying to get the mouse position while pressing a button, but instead of getting the position of the mouse on Tkinter root, it gives me the position of the mouse on the button. 我试图在按下按钮的同时获得鼠标位置,但不是在Tkinter根上获得鼠标的位置,而是在按钮上给出了鼠标的位置。 For example, if the button is placed on 200, 200, and I press the top left of the button, it prints 0, 0 instead of 200, 200.
例如,如果按钮放在200,200上,按下按钮的左上角,它会打印0,0而不是200,200。
import Tkinter as tk
def leftclick(event):
print("left")
x, y = event.x, event.y
print('{}, {}'.format(x, y))
root = tk.Tk()
add_user = tk.Button(root, height=63, width=195 ,text="sign up a user")
add_user.place(x= 20, y = 30)
root.bind("<Button-1>", leftclick)
root.mainloop()
You can call the winfo_pointerx
and winfo_pointery
methods of a widget to get each individual coordinate, or you can call winfo_pointerxy
to get them both: 您可以调用窗口小部件的
winfo_pointerx
和winfo_pointery
方法来获取每个单独的坐标,或者您可以调用winfo_pointerxy
来获取它们:
def leftclick(event):
x, y = event.widget.winfo_pointerxy()
print('{}, {}'.format(x, y))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.