Search code examples
pythonandroidkivykivy-language

I am making a stop watch for one part of my app using kivy.clock but it's giving Attribute Error


There are multiple windows in my app and a "Tracker" is one of them. The tracker will have a stop watch on display and will keep track of the time spent by entering the total time at the end into a database. (I have not coded the database part yet. I am simply printing the total time into the console for now).

For some reason, I keep running into an Attribute Error.

Code for Tracker Class:

class Tracker(Screen):
    time = 0
    start = False

    def start_stop(self):
        self.root.ids.start_stop_btn.text = ('Start'
                                         if self.start else 'Stop')
        self.start = not self.start
        Clock.schedule_interval(self.add_time, 1)

    def add_time(self, *args):
        while self.start:
            self.root.ids.sec_ones.text = str(int(self.root.ids.sec_ones.text) + 1)
            if self.root.ids.sec_ones.text == '10':
                self.root.ids.sec_tens.text = str(int(self.root.ids.sec_tens.text) + 1)
                self.root.ids.sec_ones.text = '0'
                if self.root.ids.sec_tens.text == '6':
                    self.root.ids.minute_ones.text = str(int(self.root.ids.minute_ones.text) + 1)
                    self.root.ids.sec_tens.text = '0'
                    if self.root.ids.minute_ones.text == '10':
                        self.root.ids.minute_ones.text = '0'
                        self.root.ids.minute_tens.text = str(int(self.root.ids.minute_tens.text) + 1)
            self.time += 1
        print(self.time)

And the Tracker Class in kv file:

<Tracker>:

    name: "Tracker"
    Label:
        text: "Timer: "
        font_size: 50
        pos_hint: {'x': 0.1, 'top': 1}
        size_hint: 0.2, 0.2

    Label:
        id: minute_tens
        text: '0'
        pos_hint: {'x': 0.36, 'top': 0.99}
        font_size: 40
        size_hint: 0.1, 0.2
        canvas.before:
            Color:
                rgba: (0.349, 0.349, 0.349, 1)
            Rectangle:
                pos: self.pos
                size: self.size

    Label:
        id: minute_ones
        text: '0'
        pos_hint: {'x': 0.47, 'top': 0.99}
        font_size: 40
        size_hint: 0.1, 0.2
        canvas.before:
            Color:
                rgba: (0.349, 0.349, 0.349, 1)
            Rectangle:
                pos: self.pos
                size: self.size

    Label:
        text: ':'
        pos_hint: {'x': 0.59, 'top': 0.99}
        font_size: 40
        size_hint: 0.05, 0.2

    Label:
        id: sec_tens
        text: '0'
        pos_hint: {'x': 0.65, 'top': 0.99}
        font_size: 40
        size_hint: 0.1, 0.2
        canvas.before:
            Color:
                rgba: (0.349, 0.349, 0.349, 1)
            Rectangle:
                pos: self.pos
                size: self.size

    Label:
        id: sec_ones
        text: '0'
        pos_hint: {'x': 0.76, 'top': 0.99}
        font_size: 40
        size_hint: 0.1, 0.2
        canvas.before:
            Color:
                rgba: (0.349, 0.349, 0.349, 1)
            Rectangle:
                pos: self.pos
                size: self.size

    Button:
        id: start_stop_btn
        text: 'Start'
        pos_hint: {'x': 0.6, 'top': 0.77}
        size_hint: 0.1, 0.06
        on_release: root.start_stop()

    SmoothButton:
        text: "Back"
        pos_hint: {'x': 0.8, 'top': 0.15}
        size_hint: 0.15, 0.05
        on_release:
            app.root.current = "HomePage"
            root.manager.transition.direction = "right"

The error looks like this: error

The app itself runs. The error only comes after clicking the start button.


Solution

  • the only class has root is the App class so the Tracker has no root attribute there

    in the start_stop method remove the "root" like the follwing

    def start_stop(self):
            self.ids.start_stop_btn.text = ('Start'
                                             if self.start else 'Stop')