如何阻止海龟。屏幕点击'通过按键功能?

2024-10-01 11:39:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经做了一个简单的程序,让你可以点击屏幕上的东西绘制使用海龟。你移动到的每个点都记录在一个数组中。我想在用户完成绘制时使用JSON将这个数组写入文本文件。你知道吗

import turtle
from turtle import Turtle, Screen

pastMovementsX = [0]
pastMovementsY = [0]

screen = Screen()
screen.setup(500, 350)
screen.screensize(600, 600)

def move(x, y):
        moveto = turtle.goto(x, y)
        pastMovementsX.append(turtle.xcor())
        pastMovementsY.append(turtle.ycor())

turtle.onscreenclick(move) 

turtle.onscreenclick()之后的任何代码都不会运行。我假设它一直在检查屏幕上的点击,因此无法继续程序。你知道吗

我试过两件事。尤其是线程和多处理。它不起作用,经过一点研究后,turtle模块似乎不喜欢或不适合线程/多处理。你知道吗

如何让程序在按键后停止turtle.onscreenclick()并继续执行进一步的代码?你知道吗


Tags: 代码import程序move屏幕绘制数组线程
1条回答
网友
1楼 · 发布于 2024-10-01 11:39:21

Any code after turtle.onscreenclick() will not run. I assume that it keeps checking for a click on the screen and therefore can't continue with the program.

你的问题是无效的,因为你的前提是不正确的:

from turtle import Turtle, Screen

def move(x, y):
        moveto = turtle.goto(x, y)
        pastMovementsX.append(x)
        pastMovementsY.append(y)

pastMovementsX = [0]
pastMovementsY = [0]

screen = Screen()
screen.setup(500, 350)
screen.screensize(600, 600)

turtle = Turtle()

screen.onclick(move)

turtle.circle(50)

screen.mainloop()

onclick()方法设置一个处理函数并继续执行下一个语句,它不检查任何东西。这是由tkinter事件循环完成的,它由程序中的最后一个mainloop()调用传递到。你知道吗

我的猜测是您没有正确地考虑代码计划执行的操作序列,并且描述这些操作可能有助于解决实际问题。你知道吗

相关问题 更多 >