我正试着用海龟做一个非常基础的MS-Paint版本。我有几个按键和鼠标事件,但有一个按键事件,即turtle.width的turtle.numinput(),在我输入所需的笔宽整数后,会导致其他按键事件停止工作。不过,鼠标事件会继续工作。
当turtle窗口打开时,首先会询问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()发布于 2021-07-17 18:25:30
对numinput() (和textinput())的调用撤消了listen()所做的事情,因为弹出的输入窗口必须成为活动的侦听器。修复方法是在每次numinput() (和textinput())调用之后重做listen()调用。
下面是我使用此修复和一些样式调整对您的代码所做的修改:
from turtle import Screen, Turtle
from random import choice, randrange
COLORS = ['Red', 'Yellow', 'Green', 'Blue']
# 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(choice(COLORS))
# Size change
def size():
paint.width(screen.numinput("width", "Type line size (in numbers):"))
screen.listen()
# 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 _ in range(4):
paint.forward(50)
paint.left(90)
def circle():
paint.circle(randrange(50, 100))
def rectangle():
for _ in range(2):
paint.forward(50)
paint.left(90)
paint.forward(100)
paint.left(90)
screen = Screen()
screen.screensize(600, 600)
paint = Turtle('turtle')
paint.width(screen.numinput("width", "Type line size (in numbers):"))
paint.speed('fastest')
# Key-events
screen.onkey(up, 'Up')
screen.onkey(down, 'Down')
screen.onkey(left, 'Left')
screen.onkey(right, 'Right')
screen.onkey(size, 'q')
screen.onkey(colorChange, 'c')
# Shape-events
screen.onkey(square, 's')
screen.onkey(circle, 'o')
screen.onkey(rectangle, 'r')
screen.listen()
# Mouse-events
screen.onclick(clearScreen, 3)
paint.ondrag(dragging)
screen.mainloop()发布于 2021-07-17 23:19:55
您可能希望用普通的input()替换turtle.numinput(),它接受来自终端的输入,而不是来自实际模块的输入,这允许所有按键操作照常发生
https://stackoverflow.com/questions/68421698
复制相似问题