kivy 无休止的 while 循环

Posted

技术标签:

【中文标题】kivy 无休止的 while 循环【英文标题】:kivy endless while loop 【发布时间】:2021-12-30 10:55:01 【问题描述】:

我是 python 和 kivy 的新手,最近我正在尝试制作一个学习应用程序,但我不知道如何让它不停地运行?这是我的代码

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from random import randrange
import time


#verbs and answers
verbs = ["arise", "awake", "be", "bear", "beat", "become", "begin", "bend", "bet", "bind", "bite", "bleed", "blow", "break", "breed", "bring", "broadcast", "build", "burn", "burst"]
answers =["arose", "awoke", "was", "bore", "beat", "became", "began", "bent", "bet", "bound", "bit", "bled", "blew", "broke", "bred", "brought", "broadcast", "built", "burnt", "burst"]
range = 20


    


rand = randrange(0, range)




class english_learning(App):
    
    def build(self):


        


        self.window = GridLayout()
        self.window.cols = 1
            

        self.window.add_widget(Image(source='cat.png'))

        self.greeting = Label(text=verbs[rand])
        self.window.add_widget(self.greeting)




        self.answer = TextInput(multiline=False)
        self.window.add_widget(self.answer)

        self.button = Button(text="check")
        self.button.bind(on_press=self.check_answer)
        
        self.window.add_widget(self.button)
        return self.window
    def check_answer(self, instance):

        if self.answer.text == answers[rand]:
            self.greeting.text = 'yes!' + self.answer.text + ' is the correct verb'

        else:
            self.greeting.text = "no!"


        

        


        

if __name__ == "__main__":
    english_learning().run()

应用程序要求一个动词,然后检查它是否正确,但它正在工作一次。 如何添加while循环以使其每次都要求答案并检查它是否正确?

【问题讨论】:

【参考方案1】:

它已经永远存在了,对吧?唯一的问题是它只选择一个词,并且永远坚持这个词。所以,当他们猜对了,选择另一个词:

    def check_answer(self, instance):
        global rand
        if self.answer.text == answers[rand]:
            rand = randrange(0,range)
            self.greeting.text = 'yes!' + self.answer.text + ' is the correct verb\n\nNow try: " + verb[rand]
        else:
            self.greeting.text = "no!"

现在,与其使用全局变量,不如将其存储在类变量中:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from random import randrange
import time


#verbs and answers
verbs = ["arise", "awake", "be", "bear", "beat", "become", "begin", "bend", "bet", "bind", "bite", "bleed", "blow", "break", "breed", "bring", "broadcast", "build", "burn", "burst"]
answers =["arose", "awoke", "was", "bore", "beat", "became", "began", "bent", "bet", "bound", "bit", "bled", "blew", "broke", "bred", "brought", "broadcast", "built", "burnt", "burst"]
range = 20



class english_learning(App):
    def build(self):
        self.target = randrange(0, range)

        self.window = GridLayout()
        self.window.cols = 1
        self.window.add_widget(Image(source='cat.png'))

        self.greeting = Label(text=verbs[self.target])
        self.window.add_widget(self.greeting)

        self.answer = TextInput(multiline=False)
        self.window.add_widget(self.answer)

        self.button = Button(text="check")
        self.button.bind(on_press=self.check_answer)
        
        self.window.add_widget(self.button)
        return self.window

    def check_answer(self, instance):

        if self.answer.text == answers[self.target]:
            self.target = randrange(0, range)
            self.greeting.text = 'yes!' + self.answer.text + ' is the correct verb\n\nNow try:' + verbs[self.target]
        else:
            self.greeting.text = "no!"

if __name__ == "__main__":
    english_learning().run()

真的,您可能希望“动词”和“问候”分开一行。

【讨论】:

以上是关于kivy 无休止的 while 循环的主要内容,如果未能解决你的问题,请参考以下文章

循环while do---while for循环

在 Pycharm 和 Kivy 中使用 while 循环

循环结构

第五章 循环结构

第五章 循环结构

C语言中啥叫死循环?怎么避免?