pythonkivy

Can't set .text to label with id in BoxLayout from .kv file on VSCode


I'm developing an android app which shows schedule. I followed some tutorials and eventually made these pieces of code:

ui.py

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

from kivy.lang import Builder

kivy.require('1.9.0')

class schedule(BoxLayout):
    def __init__(self):
        super(schedule, self).__init__()
    def showSchedule(self):
        self.scheduleText.text = str("12345")

class appFrontEnd(App) :
    def build(self):
        return schedule()
    
Builder.load_file("frontend.kv")
frontEnd = appFrontEnd()
frontEnd.run()

frontend.kv

<schedule>:
    scheduleText: scheduleText
    BoxLayout:
        orientation:'vertical'

        Label:
            text: "Schedule"
            font_size: 64
            color: 0.92, 0.45, 0
        Label:
            id: scheduleText
            text: "No"
            font_size: 21
        Button:
            text: "Update schedule"
            font_size: 32
            size: 100, 50

Code runs with no errors and I can see and interact with the button, but text with id scheduleText is still printing "No".

I have tried Builder.load_string to access this text, but no luck there. Wanted to ask how to fix this problem?


Solution

  • Button doesn't know that it has to run code - because you forgot on_press: ...

        Button:
            text: "Update schedule"
            font_size: 32
            size: 100, 50
            on_press: root.showSchedule()
    

    That's all.


    Full code (with smaller modifications):

    main.py

    import kivy
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    
    
    class Scheduler(BoxLayout):  # PEP8: `CamelCaseNames` for classes 
    
                                 # no need `__init__`
        
        #def show_schedule(self):  # PEP8: `lower_case_names` for functions
        #    print("clicked")      # for tests - to check if Button even executes it
        #    self.schedule_text.text = "12345"  # no need `str()`
    
        def show_schedule(self, widget):  # with access to clicked button
            print("clicked:", widget.text)
            self.schedule_text.text = "12345"
    
    class FrontEndApp(App):   # `FronEndApp` loads automatically `frontend.kv`
        def build(self):
            return Scheduler()
    
    
    frontend = FrontEndApp()
    frontend.run()
    

    frontend.kv

    <Scheduler>:
        schedule_text: schedule_text
        BoxLayout:
            orientation:'vertical'
    
            Label:
                text: "Schedule"
                font_size: 64
                color: 0.92, 0.45, 0
            Label:
                id: schedule_text
                text: "No"
                font_size: 21
            Button:
                text: "Update schedule"
                font_size: 32
                size: 100, 50
                #on_press: root.show_schedule()  # without `widget`
                on_press: root.show_schedule(self)  # with access to clicked button
    

    PEP 8 -- Style Guide for Python Code