Search code examples
pythonkivykivy-language

Clock doesn't refresh


I am trying to make an app with a clock but the clock never refresh and it only stayed on 00:00:00

python:

from kivy.config import Config
Config.set('graphics', 'width', '400')
Config.set('graphics', 'height', '600')
from kivy.app import App
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from datetime import date
from time import strftime
from kivy.clock import Clock

class start(BoxLayout):
    def update_time(self, dt):
        self.ids.time_button.text = strftime("[b]%H[/b]:%M:%S")
    def on_start(self):
        Clock.schedule_interval(self.update_time, 1)
        
class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("start.kv")

class xyapp(App):
    def build(self):
        return kv
    

xyapp().run()

kivy:

start:

<start>:
    BoxLayout:
        Button:
            id: time_button
            text:'[b]00[/b]:00:00'
            markup: True
            font_size: 100
            font_name: "DS-DIGIB.TTF"
            color: (.87, 0, .305, 1)
            background_color: (0.035, .094, .20, 1)

I am thinking is it the problem of on_start? I tried to change on_start to on_press and the function worked the screen with 00:00:00 My app looks like this now but the time never changes Can someone tell me how to change my code? Thankssss


Solution

  • The problem is that your on_start() method is never called. You can actually do what you want without that on_start() method. An easy fix is to start the updating from an __init__() method, like this:

    class start(BoxLayout):
        def __init__(self, **kwargs):
            super(start, self).__init__(**kwargs)
            Clock.schedule_interval(self.update_time, 1)
    

    By the way, class names should start with upper-case. Not doing so can cause problems in your kv.