Search code examples
kivykivy-language

Accessing id/widget of different class from a kivy file (.kv) using Kivy's clock?


I've spent the day working with the code from How to access id/widget of different class from a kivy file (.kv)? I've paired it down to the simplest code I could because what I want to do is use Kivy's Clock function to change the text for me instead of clicking the button.

As best as I can understand the code that changes the text should go on Line 38 of the program but all the code I tried stopped executing because it could not access the text to change it. The clock function is working in the code provided.

I've left the button press active but it's the Clock code that I want to change the text. I was wondering if someone knows the solution.

Thanks in advance.

....brad....

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.properties import ObjectProperty

Builder.load_string("""
<SingleScreen>:
    id: single_screen
    orientation: 'vertical'
    Button:
        text: "You Can Change Me By Button Press Here But I Really Want To Be Changed By Kivy's Clock Function"
        on_release: root.rooted()
    Change_Label:
        id: gb
<Change_Label>:
    _python_access: ChangeLabel
    Label:
        id: ChangeLabel
        text: "I'm Gonna Change."
""")

class SingleScreen(BoxLayout):

    def rooted(self):
        self.ids.gb._python_access.text = "I Changed It By Clicking!. But That's Not What I Wanted To Program!"

class Change_Label(BoxLayout):
    _python_access = ObjectProperty(None)

class OnlyScreen(App):
    def build(self):
        Clock.schedule_interval(self.Callback_Clock, 3)
        return SingleScreen()

    def Callback_Clock(self, dt):
        print "Hello, world! I'm the clock working...."
        # What code goes here that will change the text via Kivy's clock instead of using the button?

if __name__ == '__main__':
    OnlyScreen().run()

Solution

  • Please do the following and refer to my example for details:

    Snippet

    class OnlyScreenApp(App):
    
        def build(self):
            Clock.schedule_interval(self.Callback_Clock, 3)
            return SingleScreen()
    
        def Callback_Clock(self, dt):
            self.root.ids.gb._python_access.text = "I Changed It By Clock! dt=" + str(dt)
    

    Example

    main.py

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.clock import Clock
    from kivy.properties import ObjectProperty
    
    
    class SingleScreen(BoxLayout):
        pass
    
    
    class Change_Label(BoxLayout):
        _python_access = ObjectProperty(None)
    
    
    class TestApp(App):
    
        def build(self):
            Clock.schedule_interval(self.Callback_Clock, 3)
            return SingleScreen()
    
        def Callback_Clock(self, dt):
            self.root.ids.gb._python_access.text = "I Changed It By Clock! dt=" + str(dt)
    
    
    if __name__ == '__main__':
        TestApp().run()
    

    test.kv

    #:kivy 1.10.0
    
    <SingleScreen>:
        id: single_screen
        orientation: 'vertical'
        Change_Label:
            id: gb
    
    <Change_Label>:
        _python_access: ChangeLabel
        Label:
            id: ChangeLabel
            text: "I'm Gonna Change."
    

    Output

    enter image description here