我正在尝试制作一个计时器,该计时器从一个随机的起始数字开始反复递减到零,直到达到预定的时限为止。我已经设法在一个屏幕上执行此操作,但是在将其适应多个屏幕时遇到了麻烦。
当前,如果我运行PleaseWork(boxLayout)类中的函数,它将完全按照其应有的方式运行。但是,我想添加一些屏幕,以便主屏幕上有一个显示“ Go!”的按钮。并在释放按钮时启动计时器。我希望能够从第二个屏幕切换到第三个屏幕,并且计时器上的值完全相同,但是我不确定如何执行此操作。
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
当前,如果我尝试运行该程序,它将显示以下错误消息:
AttributeError:“ SecondWindow”对象没有属性“ a”
但是我也意识到我实际上并没有在任何时候调用PleaseWork(App)类中的函数。我只是无法解决。抱歉,答案很明显,但是我不喜欢使用kivy!
答案 0 :(得分:1)
'a'
AttributeError:“ SecondWindow”对象没有属性“ a”
类属性a
仅在类PleaseWork()
中定义,而该类不是根。根是class WindowManager()
。
self.lap_counter
id: count_down
中缺少kv self.ids['count_down']
py和kv文件需要以下增强功能。
pass
中删除class WindowManager()
,并删除类头文件class PleaseWork(App)
reset()
将a
和b
重置为零*args
中添加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()
事件中调用方法start()
和on_release
root.a
替换为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"