Petar Luketina
Petar Luketina

Reputation: 449

Changing Kivy class property within KV file

I want to change a property of a toggle button that is in multiple places. When I run the code, I get AttributeError: 'super' object has no attribute '__getattr__'. Do I have to create the button in the python file for this to work?

from kivy.app import App
from kivy.lang import Builder

KV = Builder.load_string("""

ScreenManager:
    Screen:
        name: 'screen'
        GridLayout:
            cols:1
            rows:3

            TButton:
            TButton:

            Button:
                text:
                    'Reset button'
                on_release:
                    app.root.get_screen('screen').ids.toggle_buttons.state = 'normal'

<TButton@ToggleButton>:
    id: toggle_buttons
    allow_no_selection: True
    text: 'Toggle Button'

""")

class MyApp(App):
    def build(self):
        return KV

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

When I press the 'Reset button', the TButtons should have their states reset to 'normal'.

Upvotes: 1

Views: 374

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

First you must understand that the id must have a local scope and you should not use it outside of it. So the toggle_buttons id should only be used within the implementation of TButton.

With your logic let's say you want to reset just one button through that id, how do I identify that button if they have the same id? as we see it is impossible.

The solution is to implement a property that stores the ids of the buttons and iterates by setting the property.

ScreenManager:
    buttons: [btn1, btn2] # <---
    Screen:
        name: 'screen'
        GridLayout:
            cols:1
            rows:3
            TButton:
                id: btn1
            TButton:
                id: btn2
            Button:
                text:
                    'Reset button'
                on_release: for btn in root.buttons: btn.state = 'normal'

<TButton@ToggleButton>:
    id: toggle_buttons
    allow_no_selection: True
    text: 'Toggle Button'

Upvotes: 1

Related Questions