python - python turtle 中 while 循环内的按键绑定(bind)

标签 python turtle-graphics key-bindings

我正在尝试创建一个学习游戏,其中一个问题会向下落,然后您输入答案,但是我不明白如何在不停止下降问题运动的情况下记录关键输入。

简单来说,我希望能够在不停止 Action 的情况下,同时降低问题并使用键盘输入。

text_letter = 0

def text_insert(answer):
    global text_letter
    print("hello")
    text_letter += 1

def text_lower(question,answer):
    global text_letter
    text.penup()
    text.goto(random.randint(-250,250),355)
    text.pendown()
    text.color("white")
    text.write("Start", font=("Arial", 20, "normal"))
    x,y = text.pos()
    delay = .01
    wn.textinput("Answer", "Answer:")  
    turtle.listen()
    turtle.onkey(text_insert(answer),answer[text_letter])
    while y > -355:
        time.sleep(delay)
        y -= 1
        text.goto(x,y)
        text.write(question, font=("Arial", 20, "normal"))
        text.clear()

最佳答案

这可能是一个比您预期的更复杂的答案:如果您省略 turtle 的 onkeypress() 函数的第二个 key 参数,它将调用您的按下任何键时的按键处理程序代码。它根本不会告诉您哪个键!

我们可以通过重写底层代码来解决这个糟糕的设计,在没有设置键的情况下将 tkinter 的 event.char 传递给 turtle 的事件处理程序。

完成后,我们可以使用 turtle 定时事件将问题从窗口顶部降低,同时用户键入的输入显示在窗口底部。

这是我模拟的一个问题,可以帮助您入门:

from turtle import Screen, Turtle
from functools import partial

FONT_SIZE = 20
FONT = ("Arial", FONT_SIZE, "normal")

def text_lower(question):
    question_turtle.forward(1)
    question_turtle.clear()
    question_turtle.write(question, align="center", font=FONT)
    screen.update()

    if question_turtle.ycor() - answer_turtle.ycor() > FONT_SIZE:
        screen.ontimer(lambda: text_lower(question), 15)
    else:
        question_turtle.clear()

def _onkeypress(self, fun, key=None):
    if fun is None:
        if key is None:
            self.cv.unbind("<KeyPress>", None)
        else:
            self.cv.unbind("<KeyPress-%s>" % key, None)
    else:
        if key is None:
            def eventfun(event):
                fun(event.char)
            self.cv.bind("<KeyPress>", eventfun)
        else:
            def eventfun(event):
                fun()
            self.cv.bind("<KeyPress-%s>" % key, eventfun)

def display_character(character):
    global answer

    if not character:
        return

    if ord(character) == 13:
        answer_turtle.clear()
        answer_turtle.setx(0)
        # do something with answer and then:
        answer = ""
    else:
        answer += character
        answer_turtle.write(character, move=True, font=FONT)

    screen.update()

screen = Screen()
screen.tracer(False)
screen._onkeypress = partial(_onkeypress, screen)

question_turtle = Turtle(visible=False)
question_turtle.penup()
question_turtle.setheading(270)
question_turtle.sety(screen.window_height()/2 - FONT_SIZE)

answer_turtle = Turtle(visible=False)
answer_turtle.penup()
answer_turtle.sety(FONT_SIZE - screen.window_height()/2)

answer = ""

screen.onkeypress(display_character)
screen.listen()

text_lower("What is the air-speed velocity of an unladen swallow?")  # A: An African or European swallow?

screen.mainloop()

关于python - python turtle 中 while 循环内的按键绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59025815/

相关文章:

python - MySQL 和 web2py

python - type函数如何判断对象的类型?

python - 相互围绕的递归彩虹色圆圈

c++ - 如何在 Arch 中安装 libx11-dev? (以下为原标题)

emacs - 如何找到在 emacs 中定义键绑定(bind)的位置?

python - 如何在 python 中裁剪和旋转图像的一部分?

python - 从 csv 文件 : Error IO operation on closed file 中获取数据

python - 键绑定(bind) 1-5 不工作 Tkinter

python - 您可以将一个 for 循环嵌套在另一个 for 循环中,并且它们具有相同的循环变量吗?

key-bindings - 如何设置自定义键绑定(bind)以在 Atom 编辑器中运行脚本或执行命令?