python turtle 获取鼠标坐标_python turtle 获取鼠标坐标_如何使用tkinter和turtle获得鼠标位置?...

在Python turtle中,由于没有内置的'Motion'事件类型,直接使用Canvas的事件处理会出现问题。解决方法是添加缺失的turtle方法,使程序能够在turtle环境中正确处理鼠标移动事件。示例代码展示了如何在turtle画布上监听鼠标移动,并更新turtle的位置。
摘要由CSDN通过智能技术生成

You've got an issue working against you that isn't of your own making. The general rule is when in a turtle canvas, use turtle methods. But turtle doesn't have an inherent 'Motion' event type, so you were trying to use the raw Canvas one as a substitute. Thus the conflict.

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

我已经按照您的意图重写了下面的程序,使其可以正常工作。解决方法是添加缺少的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('')

else:

def eventfun(event):

fun(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值