Turtle 中的按键事件导致其他按键事件停止工作

A Key press event in Turtle is causing other key events to stop working

我正在尝试使用 Turtle 制作一个非常基本的 MS-Paint 版本。我有几个键和鼠标事件,但一个键事件,即 turtle.width 的 turtle.numinput(),导致我的其他键事件在我输入所需的笔宽整数后停止工作。鼠标事件继续工作。

当乌龟 window 打开时,首先询问 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() 所做的操作,因为弹出输入 window 必须成为主动侦听器。解决方法是在每次 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()