问题描述
我正在尝试使用 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()
解决方法
您可能想用普通的 turtle.numinput()
替换 input()
,它从终端而不是从实际模块中获取输入,这样所有关键操作都可以正常进行
调用 numinput()
(和 textinput()
)会撤消 listen()
所做的事情,因为弹出输入窗口必须成为活动侦听器。解决方法是在每次 listen()
(和 numinput()
)调用后重做 textinput()
调用。
以下是我通过此修复程序和一些样式调整对您的代码进行的返工:
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()