You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Python Turtle模块中鼠标跟随的海龟会出现偏移?

海龟跟随鼠标移动偏移问题

我正在编写一个让海龟(turtle)跟随鼠标移动的程序,当前代码如下:

import turtle

t = turtle.Turtle()

canvas = turtle.getcanvas()
width = canvas.winfo_width()
height = canvas.winfo_height()

midpointX = width / 2
midpointY = height / 2

t.speed(0)

while True:
    mouseX, mouseY = canvas.winfo_pointerxy()

    turtleX = mouseX - midpointX
    turtleY = (mouseY - midpointY) * -1 

    t.goto(turtleX, turtleY)

在PyCharm或命令行运行时海龟会出现偏移,但在Replit上运行则无此问题,我使用的是Windows 11系统。当鼠标位于屏幕边缘时,海龟的移动范围如图所示:

鼠标在屏幕边缘时海龟的移动范围

内容的提问来源于stack exchange,提问作者sbottingota

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 22:21:03