How to index/id a button?

469 views
Skip to first unread message

Peter Bell

unread,
Feb 5, 2021, 6:03:20 AM2/5/21
to Kivy users support
I have been happily using 'id' as a button property, allowing me to associate actions with a particular button when processing an on-event.

    for j in range(len(Lights.lights)):
        i = j
        button = Button(
                        text = Lights.lights[i][2], font_size = 24,
                        text_size = (160,60),
                        halign = 'center', valign = 'center',
                        size_hint_x = None, width = 188,
                        size_hint_y = None, height = 68,
                        id=str(i),
                        color = black,
                        background_normal = '',
                        background_color = grey,
                        on_press = self.on_event
                       )
        self.mylights.append([grey,button])
        grid.add_widget(button)

    def on_event(self, obj):
        import Lights
        objid = int(obj.id)
        Lights.lights[objid][1].toggle()
        self.mylights[objid][0] = grey
        if (Lights.lights[objid][1].read()):
            self.mylights[objid][0] = yellow
        obj.background_color = self.mylights[objid][0] 


This worked perfectly on kivy v1.11.1 and earlier.  However, on kivy v2.0.0 it generates a TypeError:
        TypeError: object.__init__() takes exactly one argument (the instance to initialize)

I reported this as an issue on kivy github and was surprised to be told that id is not a button property.
From perusing Stack Overflow, I find that I am not the only person to have been using id as a button property in this way.  Perhaps an undocumented feature of earlier kivy versions which has been brought into line with documentation for v2.0.0?

Anyway, my issue now is how to tie an index value/id to a button, without using id as a button property.  Any suggestions?

Hazhir

unread,
Feb 5, 2021, 6:52:56 AM2/5/21
to Kivy users support
id may not be a button property, but so what? Add it as an attribute and use it later. That's what I usually do in these situations.
button = Button(some_parameters)

Peter Bell

unread,
Feb 6, 2021, 1:09:48 AM2/6/21
to kivy-...@googlegroups.com
On 05/02/2021 19:52, Hazhir wrote:
id may not be a button property, but so what? Add it as an attribute and use it later. That's what I usually do in these situations.
button = Button(some_parameters)

Thanks - that was too obvious and I was trying to make it difficult!
Reply all
Reply to author
Forward
0 new messages