Python Forum
[Kivy] Acces atributes from a button defined in a class inside the .kv - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Kivy] Acces atributes from a button defined in a class inside the .kv (/thread-33907.html)



Acces atributes from a button defined in a class inside the .kv - Tomli - Jun-09-2021

Hello!
Lately I have been trying my luck on kivy, and I'm always having problem when I try to access attributes that have been defined in the .kv

For example, this time I'm trying to access the attributes of my buttons inside my class SlidingPopup (id:btn_yes and id:btn_no) where I'm using it in the FloatLayout.

<SlidingPopup@GridLayout>:
    id:class_sliding
    btn_no: btn_no
    rows:2
    canvas:
        Color:
            rgba: 1,1,1,.8
        Rectangle:
            size: self.size
            pos: self.pos
    Label:
        text: "Are you sure?"
        font_size: 24
    GridLayout:
        rows: 1
        spacing: 10
        padding: 10
        Button:
            id: btn_no
            text: "No"
            color: 1,0,0,1
        Button:
            id: btn_yes
            text: "Yes"
            color: 0,0,0,1

FloatLayout:
    Button:
        size_hint: .3,.3
        pos_hint: {"center_x": .5, "center_y": .5}
        text:"Open Menu"
        on_release:
            app.show_the_button(the_sliding_menu)
    
    SlidingPopup:
        id: the_sliding_menu
        size_hint: 1, .3
        top_hint: 0
        pos_hint: {"top":self.top_hint, "right":1}
I'm just looking for a way to define their bindings where I'm using them and not in the class definition (something like btn_no.one_release: ... but that actually works :D). Someone who can give me a hand with their wisdom?


RE: Acces atributes from a button defined in a class inside the .kv - topfox - Jun-09-2021

Hi Tomli
I don't have a full answer for you, but a couple of suggestions.
I have written some very complicated assemblies of Kivy widgets, and have become frustrated with trying to control complicated interactions in kv language. Instead I have moved to coding everything in Python except where a rule only works in the kv code.
You may be able to refer to widget ids using syntax like root.ids.<myobjectid>.<attribute>
I hope that helps you move in the right direction!


RE: Acces atributes from a button defined in a class inside the .kv - Tomli - Jun-10-2021

(Jun-09-2021, 04:22 PM)topfox Wrote: Hi Tomli
I don't have a full answer for you, but a couple of suggestions.
I have written some very complicated assemblies of Kivy widgets, and have become frustrated with trying to control complicated interactions in kv language. Instead I have moved to coding everything in Python except where a rule only works in the kv code.
You may be able to refer to widget ids using syntax like root.ids.<myobjectid>.<attribute>
I hope that helps you move in the right direction!

Hi Topfox,
You know what? I took the same approach until now but decided to give .kv files a last try, and they are being a hell of a work when the code becomes big and you need a lot of interactions within widgets. There must be a way around besides not using the .kv, but I couldn't find it by myself and that's why I'm here, but as you are telling me seems that I wasn't the only one who found problems with these files. Guess I'm coming back to work only in python files after all.

Thanks for your reply! Big Grin