【问题标题】:Keybind within a while loop in pythons turtlepython turtle 中的while循环内的键绑定
【发布时间】:2019-11-25 05:40:41
【问题描述】:

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

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

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()

【问题讨论】:

    标签: python turtle-graphics key-bindings


    【解决方案1】:

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

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

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

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

    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()
    

    【讨论】:

    • 谢谢!我想出了一个不同的解决方案,我创建了一个 Tkinter 输入框并不断检索每个帧上框中的文本,如果内容与答案匹配,它将打破循环......也就是说我一直在搞乱这个解决方案并且有取得了一些不错的成绩。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-03
    • 2016-04-24
    相关资源
    最近更新 更多