Phil Penny
Phil Penny

Reputation: 123

Return ID of button when clicked Kivy Python

Simple problem I'm having:

When I click a button in my Kivy/Python app, I want to gain access to the ID of that button.

# Kivy code

Button:
    id: position_1
    on_press: app.access_button_id()

I have had various trial and error attempts but I cannot figure it out. The button is nested as follows (as part of a bigger app), if this is a factor in my problem:

FloatLayout:
    AnchorLayout:
        ScreenManager:
            Screen:
                BoxLayout:
                    FloatLayout:
                        Button:
                            id: position_1
                            on_press: app.access_button_id()

Here is my Python code, which returns all of the id's. That's as close as I have got:

# Python code

def access_button_id(name):
    print(self.root.ids)

The problem I've got is that I don't even know what I should be looking at in the documentation as I do not fully understand the terminology yet, so I can't find the right information to learn from.

EDIT:

The (name) passed into the function is for some other functionality, not related to this problem.

Upvotes: 0

Views: 2186

Answers (1)

Nykakin
Nykakin

Reputation: 8747

You already know the id - you set it yourself. These are mostly used as a hard-coded values in your code:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder


Builder.load_string('''
<MyWidget>:
    Button:
        id: id_value_1
        text: 'Print my id'
        on_press: print({'id_value_1': self.proxy_ref})
    Button:
        id: id_value_2
        text: 'Print all ids'
        on_press: print(root.ids)
''')


class MyWidget(BoxLayout):
    pass


class MyApp(App):
    def build(self):
        widget = MyWidget()
        print({'id_value_1': widget.ids['id_value_1']})
        return widget


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

Why do you need id of a Button if you already have an access to it? What are you trying to accomplish?


EDIT

An example solution to problem mentioned in the comment:

import random
import functools

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.lang import Builder


Builder.load_string('''
<MyWidget>:
    Button:
        text: 'B1'
        on_press: root.open_popup(self)
    Button:
        text: 'B2'
        on_press: root.open_popup(self)
''')

class MyWidget(BoxLayout):
    def open_popup(self, caller):
        fun = functools.partial(self.rename, caller)
        popup = Popup(
            title='Test popup',
            content=Button(
                text='rename',
                on_press=fun
            ),
            size_hint=(None, None), size=(400, 400)
        )
        popup.open()

    def rename(self, caller, *args):
        caller.text = ''.join(chr(random.randint(48, 90)) for i in range(5))


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


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

Upvotes: 1

Related Questions