Kivy Clock & Conditional Statements
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 '
Solution 1:
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
''')
classtheApp(App):
defbuild(self):
Clock.schedule_interval(self.timer_loop, 2)
return theRoot
deftimer_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()
As you realize it is not necessary to use switch_on1
since the main condition is the triggering of the timer.
Post a Comment for "Kivy Clock & Conditional Statements"