Skip to content Skip to sidebar Skip to footer

Kivy Change Label Text With Python

I'm semi-OK with Python but brand new to Kivy, I know my problem is referencing the label ID but I can't seem to solve it and searching doesn't seem to give me what I need. I'm try

Solution 1:

This was more of a Python problem rather than a Kivy one. You were calling the update_time of the class MainScreen class, not of the object/instance of the MainScreen. Basically, you would need to save a reference to the object (self.main_screen) in the build method, and then use it in the on_start.

class ScreenManagerApp(App):

    def build(self):
        self.main_screen = MainScreen()
        return self.main_screen

    def on_start(self):
        Clock.schedule_interval(self.main_screen.update_time, 1)

Also you cannot access id outside of the kv language, i.e. in the python. You have to reference the id by adding a property, e.g. the_time:

<MainScreen>:
    name: 'main'
    the_time: _id_lbl_time
    BoxLayout:
        orientation: 'vertical'
        Label:
            id: _id_lbl_time

A minor problem is that the update_time() receives to parameters. Also, there were quite a few weird things in your code, so I couldn't run the code. I fixed all the above in the code below:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.properties import ObjectProperty, StringProperty
from kivy.clock import Clock
import time
from datetime import datetime

Builder.load_string('''
<MainScreen>:
    name: 'main'
    the_time: _id_lbl_time
    BoxLayout:
        orientation: 'vertical'
        Label:
            id: _id_lbl_time
            text: 'Time' 
            font_size: 60
 ''')

class MainScreen(Screen):

    def update_time(self, sec):
        MyTime = time.strftime("%H:%M:%S")
        self.the_time.text = MyTime


class ScreenManagerApp(App):

    def build(self):
        self.main_screen = MainScreen()
        return self.main_screen

    def on_start(self):
        Clock.schedule_interval(self.main_screen.update_time, 1)

ScreenManagerApp().run()

Post a Comment for "Kivy Change Label Text With Python"