简体   繁体   English

如何使用Python和kivy输入条件语句?

[英]How to enter conditional statements using Python and kivy?

I'm new to kivy. 我是kivy的新手。 I was writing the following code using python and kivy which is a quite simple one that asks the user for his/her name and hunger level. 我正在使用python和kivy编写以下代码,这是一个非常简单的代码,询问用户他/她的名字和饥饿程度。

import kivy #import kivy module
kivy.require('1.0.6') # replace with your current kivy version !

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout  
from kivy.uix.textinput import TextInput  

class human(GridLayout):
    #This is a human class
    def __init__(self, **kwargs):
        super(human, self).__init__(**kwargs)
        self.cols = 2 
        self.add_widget(Label(text='Enter your name')) 
        self.name = TextInput(multiline=False) 
        self.add_widget(self.name)
        self.add_widget(Label(text='Enter your hunger level(0-10)'))
        self.hunger = TextInput(multiline=False)
        self.add_widget(self.hunger)

class MyApp(App):

    def build(self):
        return human()


if __name__ == '__main__':
    MyApp().run()

Now I would like to have a conditional statement that will check whether the hunger level entered is less than 5 or not. 现在我想要一个条件语句来检查输入的饥饿程度是否小于5。 If so, it will print "normal" else it will print "abnormal". 如果是这样,它将打印“正常”,否则将打印“异常”。

What should I do? 我该怎么办?

You obviously can't check for the hunger level immediatly, before the end of the build() method, the user probably didn't even see a window yet, so you have to organise the code for it to check the value after some time, the question is when , usually, you want to react to some event, rather than wait a specific amount of time. 您显然无法立即检查饥饿程度,在build()方法结束之前,用户可能甚至没有看到窗口,因此您必须为其组织代码以在一段时间后检查值,问题是什么时候 ,通常情况下,你要做出反应的一些事情,而不是等待特定的时间量。 Kivy offers ways to do either. Kivy提供了两种方法。

You could decide that the user has to validate its input by pressing a button. 您可以决定用户必须通过按下按钮来验证其输入。 To do that, you would add. 要做到这一点,你会添加。 self.add_widget(Button(text="ok", on_press=self.check_hunger)

and add a check_hunger method to human , which would check the current text in self.hunger. 并向human添加check_hunger方法,该方法将检查self.hunger中的当前文本。

But you could decide to be more reactive, and instead to react to any validation of the textinput. 但是你可以决定更具反应性,而不是对文本输入的任何验证做出反应。 To achieve this instead, you can just bind check_hunger to the on_text_validate event of hunger. 要实现这一点,您可以将check_hunger绑定到饥饿的on_text_validate事件。

add self.hunger.bind(on_text_validate=self.hunger) after self.hunger = TextInput(multiline=False) self.hunger = TextInput(multiline=False)后添加self.hunger.bind(on_text_validate=self.hunger) self.hunger = TextInput(multiline=False)

or simply edit this line to read self.hunger = TextInput(multiline=False, on_text_validate=self.check_hunger) 或者只是编辑这一行来读取self.hunger = TextInput(multiline=False, on_text_validate=self.check_hunger)

and just for the sake of the example, if you wanted to check the hunger after some definite time instead, you could add at the top of your program: from kivy.clock import Clock and put Clock.schedule_once(self.check_hunger, 5) at the end of your __init__ method. 并且仅仅为了示例,如果你想在一段确定的时间之后检查饥饿,你可以在程序的顶部添加: from kivy.clock import Clock并放入Clock.schedule_once(self.check_hunger, 5)__init__方法的最后。

Anyway, to achieve what you asked for, the check_hunger method would look something like: 无论如何,为了实现你所要求的, check_hunger方法看起来像:

def check_hunger(self, *args): if int(self.hunger.text) < 5: print("normal") else: print("abnormal")

hope it helps! 希望能帮助到你!

After you've instantiated your human class you can access to hunger level by just writing human_instance.hunger . 在实例化人类课程后,您只需编写human_instance.hunger即可访问饥饿等级。

You can try the below code: 您可以尝试以下代码:

import kivy #import kivy module
kivy.require('1.0.6') # replace with your current kivy version !

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout  
from kivy.uix.textinput import TextInput  

class human(GridLayout):
    #This is a human class
    def __init__(self, **kwargs):
        super(human, self).__init__(**kwargs)
        self.cols = 2 
        self.add_widget(Label(text='Enter your name')) 
        self.name = TextInput(multiline=False) 
        self.add_widget(self.name)
        self.add_widget(Label(text='Enter your hunger level(0-10)'))
        self.hunger = TextInput(multiline=False)
        self.add_widget(self.hunger)

class MyApp(App):

    def build(self):
        return human()


if __name__ == '__main__':
    my_app = MyApp()
    my_app.run()

    my_human = my_app.build()

    if my_human.hunger < 5:
        print('Normal')
    else:
        print('Abnormal')

I'm not sure if this code will run or not because I don't know if kivy's app's run method is blocking or non-blocking. 我不确定这段代码是否会运行,因为我不知道kivy的应用程序run方法是阻塞还是非阻塞。 But you can understand the logic from this code. 但是你可以从这段代码中理解逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM