鼠标位置Python Tkinter [英] Mouse Position Python Tkinter
本文介绍了鼠标位置Python Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以获取鼠标的位置并将其设置为var?
Is there a way to get the position of the mouse and set it as a var?
推荐答案
您可以设置回调以响应<Motion>
事件:
You could set up a callback to react to <Motion>
events:
import Tkinter as tk
root = tk.Tk()
def motion(event):
x, y = event.x, event.y
print('{}, {}'.format(x, y))
root.bind('<Motion>', motion)
root.mainloop()
我不确定您想要哪种变量.在上面,我将局部变量x
和y
设置为鼠标坐标.
I'm not sure what kind of variable you want. Above, I set local variables x
and y
to the mouse coordinates.
如果将motion
设为类方法,则可以将实例属性self.x
和self.y
设置为鼠标坐标,然后可以从其他类方法访问这些属性.
If you make motion
a class method, then you could set instance attributes self.x
and self.y
to the mouse coordinates, which could then be accessible from other class methods.
这篇关于鼠标位置Python Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文