如何使用 tkinter 和 turtle 获取鼠标位置?

How do I get mouse position with tkinter and turtle?

我正在尝试编写一个程序,让我可以使用 turtle 在 tkinter window 上绘图。由于某种原因,我无法获得鼠标的绝对坐标。

我完成了 root.winfo_pointerx() - root.winfo_rootx()(和 vrootx)。 我也试过:

def mousePos(event):
     x,y = event.x , event.y
     return x,y

我的代码:

import turtle
import tkinter as tk
root = tk.Tk()
root.title("Draw!")
cv = tk.Canvas(root, width=500,height=500)
cv.focus_set()
cv.pack(side = tk.LEFT)
pen = turtle.RawTurtle(cv)
window = pen.getscreen()

def main():
    window.setworldcoordinates(-500,-500,500,500)
    window.bgcolor("white")
    frame = tk.Frame(root)
    frame.pack(side = tk.RIGHT,fill=tk.BOTH)
    pointLabel = tk.Label(frame,text="Width")
    pointLabel.pack()
    def getPosition(event):
       x = root.winfo_pointerx()-root.winfo_vrootx()
       y = root.winfo_pointery()-root.winfo_vrooty()
       pen.goto(x,y)
    cv.bind("<Motion>", getPosition)
    cv.pack
    tk.mainloop()
    pass

我希望光标位于箭头上方,但它总是在右下方。另外,当我向上移动鼠标时,箭头向下移动,反之亦然。

Tkinter 的鼠标位置:

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

乌龟的鼠标位置:

    canvas = turtle.getcanvas()
    x, y = canvas.winfo_pointerx(), canvas.winfo_pointery()

希望对您有所帮助。

你遇到了一个不是你自己造成的问题。一般规则是在海龟 canvas 中使用海龟方法。但是 turtle 没有固有的 'Motion' 事件类型,因此您试图使用原始 Canvas 事件类型作为替代。于是产生了冲突。

您自己造成的一个问题是,当您处于快速移动的事件处理程序中时,您需要首先禁用事件处理程序,然后在退出时重新启用。否则,事件重叠,坏事发生。 (无意的递归和其他奇怪的事情。)

我已经在下面重写了您的程序,以确保按照您的预期运行。解决方法是添加缺少的 turtle 方法,这样我们就可以留在 turtle 域内:

import tkinter as tk
from turtle import RawTurtle, TurtleScreen
from functools import partial

def onscreenmove(self, fun, add=None):  # method missing from turtle.py

    if fun is None:
        self.cv.unbind('<Motion>')
    else:
        def eventfun(event):
            fun(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale)

        self.cv.bind('<Motion>', eventfun, add)

def getPosition(x, y):
    screen.onscreenmove(None)  # disable events inside handler

    pen.setheading(pen.towards(x, y))
    pen.goto(x, y)

    screen.onscreenmove(getPosition)  # reenable handler on exit

root = tk.Tk()
root.title("Draw!")

cv = tk.Canvas(root, width=500, height=500)
cv.focus_set()
cv.pack(side=tk.LEFT)

screen = TurtleScreen(cv)
screen.onscreenmove = partial(onscreenmove, screen)  # install missing method

pen = RawTurtle(screen)

frame = tk.Frame(root)
frame.pack(side=tk.RIGHT, fill=tk.BOTH)

tk.Label(frame, text="Width").pack()

screen.onscreenmove(getPosition)
screen.mainloop()

好好想想你是如何设置setworldcoordinate()的。 -500 - 500 表示您的世界大小为 1,000,而 window 大小为 500。此外,鼠标指针与 window 根的偏移量 - 应使用两个绝对坐标。您混淆了绝对坐标 - 鼠标指针和不同比例的 vrootx,因此两者的距离没有意义。以下代码可能更接近您的意图。请注意,我将世界坐标设置为与鼠标指针从 window 的 top/left 角偏移的绝对坐标相匹配。

import turtle
import tkinter as tk
root = tk.Tk()
root.title("Draw!")
cv = tk.Canvas(root, width=500,height=500)
cv.focus_set()
cv.pack(side = tk.LEFT)
pen = turtle.RawTurtle(cv)
window = pen.getscreen()

def main():
    window.setworldcoordinates(0,500,500,0)
    window.bgcolor("white")
    frame = tk.Frame(root)
    frame.pack(side = tk.RIGHT,fill=tk.BOTH)
    pointLabel = tk.Label(frame,text="Width")
    pointLabel.pack()
    print(dir(root))
    def getPosition(event):
       x = root.winfo_pointerx()-root.winfo_rootx()
       y = root.winfo_pointery()-root.winfo_rooty()
       print(x, y)
       pen.goto(x,y)
       pass
    cv.bind("<Motion>", getPosition)
    cv.pack
    tk.mainloop()
    pass

if __name__ == "__main__":
  main()
  pass