Kivy Clock&条件语句 [英] Kivy Clock & Conditional Statements

查看:160
本文介绍了Kivy Clock&条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了timer_loop变量以使其连续运行.我想使用其他变量进行条件语句,但是我无法这样做.下面是一个示例,如果开关被激活,我将尝试每30分钟执行一次操作".任何反馈将不胜感激:)

I created the variable timer_loop to run continuously. I'd like to make conditional statements with other variables, but I am unable to do so. Below is an example, I am trying to "do something" every 30 minutes if the switch is activated. Any feedback would be greatly appreciated :)

from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
import datetime
import time

theRoot = Builder.load_string('''

StackLayout:
    orientation: 'lr-tb'
    padding: 10
    spacing: 5

    Label:
        text: "Zone 1 Valve"
        size_hint: .5, .1

    Switch:
        id: switch_id
        on_active: app.switch_on1(self, self.active)
        size_hint: .5, .1
''')

class theApp(App):

    def build(self):
        Clock.schedule_interval(self.timer_loop, 2)
        return theRoot

    def timer_loop(self, dt):  
        now_minute = int(time.strftime("%M"))

        if switch_on1.active & now.minute ==30 : # how do I use the varible switch_on1 in this loop for conditonal statements???
            print("Do something")
        else:
            print("Do nothing")

    def switch_on1(self, instance, value):
        if value is True:
            print("Switch 1 On")
        else:
            print("Switch 1 Off")

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

推荐答案

通常建议将类视为黑匣子,我们可以建立输入并获得输出,并且可以通过属性来完成,例如在这种情况下,您创建了StackLayout的活动属性,该属性反映了switch_id的活动属性,并且由于theRoot是从Python可见的StackLayout对象,我们使用它:

It is always advisable to see a class as a black box that we can establish input and obtain outputs and that in kivy can be done through properties, for example in this case you create the active property of StackLayout that reflects the active property of switch_id, and since theRoot is the StackLayout object that is visible from python we use it:

from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
import time

theRoot = Builder.load_string('''
StackLayout:
    active: switch_id.active # <---
    orientation: 'lr-tb'
    padding: 10
    spacing: 5

    Label:
        text: "Zone 1 Valve"
        size_hint: .5, .1

    Switch:
        id: switch_id
        size_hint: .5, .1
''')

class theApp(App):
    def build(self):
        Clock.schedule_interval(self.timer_loop, 2)
        return theRoot

    def timer_loop(self, dt):  
        now_minute = int(time.strftime("%M"))
        if theRoot.active and now_minute == 30: # <---
            print("Do something")
        else:
            print("Do nothing")


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

您意识到没有必要使用switch_on1,因为主要条件是计时器的触发.

As you realize it is not necessary to use switch_on1 since the main condition is the triggering of the timer.

这篇关于Kivy Clock&amp;条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆