Kivy:如何在多个屏幕上运行相同的功能

时间:2019-07-04 23:13:08

标签: python kivy kivy-language

我正在尝试制作一个计时器,该计时器从一个随机的起始数字开始反复递减到零,直到达到预定的时限为止。我已经设法在一个屏幕上执行此操作,但是在将其适应多个屏幕时遇到了麻烦。

当前,如果我运行PleaseWork(boxLayout)类中的函数,它将完全按照其应有的方式运行。但是,我想添加一些屏幕,以便主屏幕上有一个显示“ Go!”的按钮。并在释放按钮时启动计时器。我希望能够从第二个屏幕切换到第三个屏幕,并且计时器上的值完全相同,但是我不确定如何执行此操作。

Py文件:

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()

kv文件:

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!

1 个答案:

答案 0 :(得分:1)

问题-AttributeError 'a'

  

AttributeError:“ SecondWindow”对象没有属性“ a”

根本原因

类属性a仅在类PleaseWork()中定义,而该类不是根。根是class WindowManager()

其他问题

  • 定义了两个应用程序类
  • 缺少属性self.lap_counter
  • id: count_down中缺少kv self.ids['count_down']

解决方案

py和kv文件需要以下增强功能。

py文件

  • pass中删除class WindowManager(),并删除类头文件class PleaseWork(App)
  • 实施方法reset()ab重置为零
  • 在方法*args中添加start()作为参数

代码段-py文件

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")

kv文件

  • 在“开始”按钮的reset()事件中调用方法start()on_release
  • 将所有出现的root.a替换为root.manager.a

摘要-kv文件

<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"

输出

SecondWindow ThirdWindow