如何使 canvas 创建的图像跟随鼠标指针?
How to make a canvas created image follow the mouse pointer?
在 tkinter 中,python,我正在创建一个 'game',其中涉及由 canvas 创建的图像以跟随鼠标指针。这是我尝试过的:
def move(event):
x = event.x
y = event.y
canvas.move(example, x, y)
canvas.tag_bind(example, "<Motion>", move)
如您所见,我绑定了鼠标光标,以便在检测到任何运动时,example
将移动到该位置。但出于某种原因,它开始了它的旅程。非常感谢您的帮助,感谢您的宝贵时间:)
move
命令采用偏移量,但您传递的是坐标。如果你想让它跟随鼠标,使用coords
方法,这样你设置图像的坐标和事件的坐标相同。
例如:
def move(event):
x = event.x
y = event.y
canvas.coords(example, x, y)
在 tkinter 中,python,我正在创建一个 'game',其中涉及由 canvas 创建的图像以跟随鼠标指针。这是我尝试过的:
def move(event):
x = event.x
y = event.y
canvas.move(example, x, y)
canvas.tag_bind(example, "<Motion>", move)
如您所见,我绑定了鼠标光标,以便在检测到任何运动时,example
将移动到该位置。但出于某种原因,它开始了它的旅程。非常感谢您的帮助,感谢您的宝贵时间:)
move
命令采用偏移量,但您传递的是坐标。如果你想让它跟随鼠标,使用coords
方法,这样你设置图像的坐标和事件的坐标相同。
例如:
def move(event):
x = event.x
y = event.y
canvas.coords(example, x, y)