Reputation: 51
I am trying to make a timer that counts down to zero from a random starting number repeatedly until a predetermined time limit is reached. I have managed to do this on one screen, but I am having trouble adapting it to several screens.
Currently, if I run the functions in the PleaseWork(boxLayout) class it does exactly as it should. However, I would like to add in some screens so that the home screen has a button that says "Go!" and on release of the button the timer is started. I would like to be able to cycle from the second to third screen with the value on the timer being exactly the same, but I am unsure of how to do this.
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.animation import Animation
from kivy.properties import NumericProperty
from random import randint
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class ThirdWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class PleaseWork(App):
a = NumericProperty(0)
b = NumericProperty(0)
run_t= 15
min = 3
max = 7
def start(self):
self.a = randint(self.min, self.max)
self.anim = Animation(a=0, duration=self.a)
if self.run_t - self.b <= self.max:
self.a = self.run_t - self.b
print("a=",self.a,"b=",self.b)
self.anim = Animation(a=0, duration = self.a)
else:
print(self.run_t - self.b)
self.anim.bind(on_complete = self.start)
print('starting anim number:', self.lap_counter)
self.anim.start(self)
def count_up(self):
self.anim = Animation(b = self.run_t, duration = self.run_t)
self.anim.bind(on_complete = self.finish_callback)
self.anim.start(self)
def finish_callback(self, animation, param):
print('in finish_callback')
end_1 = self.ids['count_down']
end_1.text = 'Finished'
Animation.cancel_all(self)
kv = Builder.load_file("integrate.kv")
class PageScrollerApp(App):
def build(self):
return kv
if __name__ == "__main__":
PageScrollerApp().run()
WindowManager:
MainWindow:
SecondWindow:
ThirdWindow:
<MainWindow>:
name: "home"
FloatLayout:
Button:
pos_hint: {"x":0.4, "y":0.05}
text: "Go!"
on_release:
app.root.current = 'low'
<SecondWindow>:
name: 'low'
FloatLayout:
Label:
id: count_down1
text: str(round(root.a, 1))
pos_hint: {"x": 0.4, "y": 0.55}
Button:
background_color: 0.5,0.1,1,1
text: 'next'
pos_hint: {"x":0.4, "y":0.05}
on_release:
app.root.current = "medium"
<ThirdWindow>:
name: "medium"
FloatLayout:
Label:
id: count_down2
text: str(round(root.a, 1))
pos_hint: {"x": 0.4, "y": 0.55}
Button:
background_color: 0.5,0.1,1,1
text: 'Cancel'
pos_hint: {"x":0.4, "y":0.05}
on_release:
app.root.current = "home"
<Button>
font_size: 20
color:1,0.2,0.5,1
size_hint: 0.2, 0.1
background_color: 0.5,0.8,0.2,1
<Label>
font_size: 20
color:1,0.2,0.5,1
size_hint: 0.2, 0.1
background_color: 0.5,0.2,0.9,1
Currently if I try to run the program it comes up with the following error message:
AttributeError: 'SecondWindow' object has no attribute 'a'
But I also realise I am not actually calling the functions inside the PleaseWork(App) class at any point. I just have been unable to work out how. Sorry if the answer is obvious, but I am new to using kivy!
Upvotes: 0
Views: 259
Reputation: 16031
'a'
AttributeError: 'SecondWindow' object has no attribute 'a'
The class attribute, a
is only defined in the class PleaseWork()
and this class is not the root. The root is class WindowManager()
.
self.lap_counter
id: count_down
in self.ids['count_down']
The following enhancements are needed in the py and kv files.
pass
from class WindowManager()
, and class header, class PleaseWork(App)
reset()
to reset a
and b
to zero*args
as arguments in method start()
class WindowManager(ScreenManager):
a = NumericProperty(0)
b = NumericProperty(0)
run_t = 15
min = 3
max = 7
def reset(self):
self.a = 0
self.b = 0
def start(self, *args):
self.a = randint(self.min, self.max)
self.anim = Animation(a=0, duration=self.a)
if self.run_t - self.b <= self.max:
self.a = self.run_t - self.b
print("a=", self.a, "b=", self.b)
self.anim = Animation(a=0, duration=self.a)
else:
print(self.run_t - self.b)
self.anim.bind(on_complete=self.start)
# print('starting anim number:', self.lap_counter)
self.anim.start(self)
def count_up(self):
self.anim = Animation(b=self.run_t, duration=self.run_t)
self.anim.bind(on_complete=self.finish_callback)
self.anim.start(self)
def finish_callback(self, animation, param):
print('in finish_callback')
end_1 = self.ids['count_down']
end_1.text = 'Finished'
Animation.cancel_all(self)
class PageScrollerApp(App):
def build(self):
return Builder.load_file("integrate.kv")
reset()
and start()
in on_release
event of "Go" buttonroot.a
with root.manager.a
<MainWindow>:
name: "home"
FloatLayout:
Button:
pos_hint: {"x":0.4, "y":0.05}
text: "Go!"
on_release:
root.manager.reset()
root.manager.start()
root.manager.current = 'low'
<SecondWindow>:
name: 'low'
FloatLayout:
Label:
id: count_down1
text: str(round(root.manager.timer.a, 1))
pos_hint: {"x": 0.4, "y": 0.55}
Button:
background_color: 0.5,0.1,1,1
text: 'next'
pos_hint: {"x":0.4, "y":0.05}
on_release:
root.manager.current = "medium"
<ThirdWindow>:
name: "medium"
FloatLayout:
Label:
id: count_down2
text: str(round(root.manager.timer.a, 1))
pos_hint: {"x": 0.4, "y": 0.55}
Button:
background_color: 0.5,0.1,1,1
text: 'Cancel'
pos_hint: {"x":0.4, "y":0.05}
on_release:
root.manager.current = "home"
Upvotes: 2