【问题标题】:turtle - How to get mouse cursor position in window?turtle - 如何在窗口中获取鼠标光标位置?
【发布时间】:2016-03-01 20:30:56
【问题描述】:

如何在海龟屏幕中找到鼠标指针的当前位置?我想要它,所以我在单击之前和移动光标时都有鼠标位置。我已经搜索了谷歌,这个网站除了点击后如何获得位置之外找不到任何东西。

【问题讨论】:

  • 你在使用 Python 的 turtle 模块吗?如果是这样,您是否看过文档中的"Using Events"
  • 是的。它仅具有 onClick、单击拖动、单击释放事件。我需要它来监听鼠标指针的位置。
  • 我只需要在海龟屏幕上获取鼠标坐标。所以我可以让乌龟旋转并看鼠标。

标签: python python-3.x turtle-graphics


【解决方案1】:

turtle.getcanvas() 返回一个 Tkinter 画布。

Like with a Tkinter window,通过winfo_pointerx.winfo_pointery就可以得到当前鼠标指针坐标:

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

如果您只想对运动做出反应,而不是例如循环轮询鼠标指针位置,注册一个事件:

def motion(event):
    x, y = event.x, event.y
    print('{}, {}'.format(x, y))

canvas = turtle.getcanvas()
canvas.bind('<Motion>', motion)

请注意,只有当鼠标指针悬停在海龟画布上时才会触发事件。

所有这些坐标都是窗口坐标(原点(0, 0)在乌龟窗口的左上角),而不是乌龟坐标(原点(0, 0)在画布中心),所以如果你想使用它们对于海龟的定位或方向,您必须进行一些转换。

【讨论】:

    【解决方案2】:

    我不知道

    print("I do not know")
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2018-10-26
    • 2021-04-13
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2013-01-20
    相关资源
    最近更新 更多