【发布时间】:2021-07-17 15:04:40
【问题描述】:
我正在尝试使用 Turtle 制作一个非常基本的 MS-Paint 版本。我有几个键和鼠标事件,但一个键事件,即 turtle.width 的 turtle.numinput(),导致我的其他键事件在我为笔宽键入所需的整数后停止工作。鼠标事件仍然有效。
turtle.numinput() 在海龟窗口打开时首先被询问,但之后其他关键事件工作正常。在我按下所需的键来更改我的笔宽之后,其他键事件停止工作。
我尝试稍微改变一下序列,甚至将代码的宽度部分复制到另一个基于 Turtle 的 python 文件中进行检查。我也遇到了同样的问题。
我不知道是什么原因造成的,非常感谢您的帮助
import turtle, random
wn = turtle.Screen()
wn.screensize(600, 600)
paint = turtle.Turtle('turtle')
colors = ['Red', 'Yellow', 'Green', 'Blue']
paint.width(turtle.numinput('width', 'Type line size (in numbers): '))
paint.speed(0)
# Arrow-Keys control function
def up():
paint.setheading(90)
paint.forward(100)
def down():
paint.setheading(270)
paint.forward(100)
def left():
paint.setheading(180)
paint.forward(100)
def right():
paint.setheading(0)
paint.forward(100)
# Color change
def colorChange():
paint.color(random.choice(colors))
# Size change
def size():
paint.width(turtle.numinput('width', 'Type line size (in numbers): '))
# Mouse Control (Clear + Drag) function
def clearScreen(x, y):
paint.clear()
def dragging(x, y):
paint.ondrag(None)
paint.setheading(paint.towards(x, y))
paint.goto(x, y)
paint.ondrag(dragging)
# Shapes with random size
def square():
for i in range(4):
paint.forward(50)
paint.left(90)
def circle():
paint.circle(random.randrange(50, 100))
def rectangle():
for i in range(2):
paint.forward(50)
paint.left(90)
paint.forward(100)
paint.left(90)
turtle.listen()
# Key-events
turtle.onkey(up, 'Up')
turtle.onkey(down, 'Down')
turtle.onkey(left, 'Left')
turtle.onkey(right, 'Right')
turtle.onkey(size, 'q')
turtle.onkey(colorChange, 'c')
# Mouse-events
turtle.onscreenclick(clearScreen, 3)
paint.ondrag(dragging)
# Shape-events
turtle.onkey(square, 's')
turtle.onkey(circle, 'o')
turtle.onkey(rectangle, 'r')
turtle.mainloop()
【问题讨论】:
标签: python events turtle-graphics python-turtle