How to get object identifier from Kivy language in Python code

I created a widget in the Kivy language, it has some properties that I use for some of them inside this widget's pictorial code. Since I use the id keyword in the .kv file, all of them have id. I need to use the same identifiers inside Python code. Here is a workaround:

CcaSwitch:
    id: out0_bit3
    name: "out0_bit3"

I use self.name instead of self.id in python code. How can I achieve the same goal without a β€œrepeating” entry for each widget?

Thank.

Edit

I use the "name" variable inside Python code as follows:

class CcaSwitch(BoxLayout):

    name = StringProperty()

    def __init__(self, *args, **kwargs):
        # ...
        Clock.schedule_interval(self.refresh, 5)

    def refresh(self, dt):
        self.comm.publish(get_bool_status(self.name), self.routing_key)

So, I need a string that identifies this instance of the widget itself, in a dry way.

I would like to achieve something like this:

In the .kv file:

CcaSwitch:
    id: out0_bit3

In Python, I would like to use it like:

 class CcaSwitch(BoxLayout):

    def __init__(self, *args, **kwargs):
        # ...
        self.name = self.id.__name__
        Clock.schedule_interval(self.refresh, 5)

    def refresh(self, dt):
        self.comm.publish(get_bool_status(self.name), self.routing_key)
+3
1

, python. ids ( , , , dict x, "apple", x.apple).

, , .kv:

<ExampleWidget>:
    CcaSwitch:
        id: out0_bit3
        state: 'ON'

.py:

w = ExampleWidget()
print(w.ids.out0_bit3.state)

w.ids.out0_bit3 CcaSwitch.

, id - . , , CcaSwitch (.. ):

class CcaSwitch(Widget):

    name = ''

    def __init__(self, *args, **kwargs):
        super(CcaSwitch, self).__init__(**kwargs)
        Clock.schedule_once(self.load_name)
        Clock.schedule_interval(self.refresh, 5)

    def load_name(self, *l):
        for id_str, widget in self.parent.ids.iteritems():
            if widget.__self__ is self:
                self.name = id_str
                return

    def refresh(self, dt):
        self.comm.publish(get_bool_status(self.name), self.routing_key)

, id id. self.load_name init, .. .

, , , , , , init set self.name = str (self).

+2

All Articles