Python Forum
[Kivy] Acces atributes from a button defined in a class inside the .kv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Kivy] Acces atributes from a button defined in a class inside the .kv
#1
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?
Reply
#2
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!
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] image inside button rwahdan 4 6,895 Jul-12-2021, 08:49 PM
Last Post: deanhystad
  [Tkinter] Modify Class on Button Click KDog 4 3,904 May-11-2021, 08:43 PM
Last Post: KDog
  [Tkinter] Redirecting all print statements from all functions inside a class to Tkinter Anan 1 2,601 Apr-24-2021, 08:57 AM
Last Post: ndc85430
  Class function does not create command button Heyjoe 2 2,226 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [Tkinter] Creation of Buttons with Shared Command Inside Class MulliganAgain 1 1,683 Jul-08-2020, 06:22 PM
Last Post: Yoriz
  [PyQt] I get a name Name Error: when calling a method inside a class radoo 2 2,327 Jun-11-2020, 05:02 PM
Last Post: radoo
  [PyQt] Problem how to click a button inside a group box? mart79 2 3,376 Aug-05-2019, 01:21 PM
Last Post: mart79
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,949 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  Button in one class, methods in another one alan9979 4 2,796 Jul-11-2019, 01:57 AM
Last Post: joe_momma
  [Tkinter] button not defined OldManProgrammer 2 4,448 Nov-21-2018, 07:53 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020