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

Python 基维:如何在多个屏幕上运行相同的功能,python,kivy,kivy-language,Python,Kivy,Kivy Language,我正在尝试制作一个计时器,它从一个随机的起始数字反复倒计时到零,直到达到预定的时间限制为止。我已经设法在一个屏幕上实现了这一点,但我在将其适应多个屏幕时遇到了困难 目前,如果我运行PleaseWork(boxLayout)类中的函数,它将完全按照它应该的方式运行。但是,我想添加一些屏幕,以便主屏幕上有一个显示“Go!”的按钮,松开该按钮后,计时器将启动。我希望能够从第二个屏幕循环到第三个屏幕,计时器上的值完全相同,但我不确定如何做到这一点 Py文件: 从kivy.app导入应用 从kivy.la

我正在尝试制作一个计时器,它从一个随机的起始数字反复倒计时到零,直到达到预定的时间限制为止。我已经设法在一个屏幕上实现了这一点,但我在将其适应多个屏幕时遇到了困难

目前,如果我运行PleaseWork(boxLayout)类中的函数,它将完全按照它应该的方式运行。但是,我想添加一些屏幕,以便主屏幕上有一个显示“Go!”的按钮,松开该按钮后,计时器将启动。我希望能够从第二个屏幕循环到第三个屏幕,计时器上的值完全相同,但我不确定如何做到这一点

Py文件:
从kivy.app导入应用
从kivy.lang导入生成器
从kivy.uix.screenmanager导入screenmanager,屏幕
从kivy.animation导入动画
从kivy.properties导入NumericProperty
从随机导入randint
类主窗口(屏幕):
通过
第二类窗口(屏幕):
通过
第三类(屏幕):
通过
类WindowManager(屏幕管理器):
通过
课堂作业(应用程序):
a=数值属性(0)
b=数值属性(0)
运行t=15
最小值=3
最大值=7
def启动(自):
self.a=randint(self.min,self.max)
self.anim=动画(a=0,持续时间=self.a)
如果self.run\u t-self.b问题-属性错误
'a'
AttributeError:“SecondWindow”对象没有属性“a”

根本原因 类属性
a
仅在类
plesework()
中定义,该类不是根。根目录是
类WindowManager()

其他问题
  • 定义了两个应用程序类
  • 缺少属性,
    self.lap\u计数器
  • 缺少kv
    id:count\u down
    in
    self.id['count\u down']
解决方案 py和kv文件中需要以下增强功能

py文件
  • class WindowManager()
    中删除
    pass
    ,并删除类标题
    class plesework(App)
  • 执行方法
    reset()
    a
    b
    重置为零
  • *args
    作为参数添加到方法
    start()
代码片段-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()
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
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")
<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"