Blaupunkt
Blaupunkt

Reputation: 129

How to return the text of a clicked button which was created in a for loop?

Example:

.py code:

class PokemonWindow(Screen):
    form_button = ObjectProperty(None)
    type_grid = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(PokemonWindow, self).__init__(**kwargs)
        self.searched_pokemon_forms = ["Normal", "Purified", "Shadow"]
            
    def create_form_buttons(self):
        for text in self.searched_pokemon_forms:
            self.form_button = Button(text=text, on_press=self.get_text)
            self.type_grid.add_widget(self.form_button)

    def get_text(self, *args):
        return self.form_button.text

and the .kv code:

<PokemonWindow>:
    name: "pokemonWindow"
    type_grid: type_grid

    MDGridLayout:
        id: type_grid
        rows: 2
        cols: 4
        size_hint_x: .8
        size_hint_y: .05
        pos_hint: {"x": .1, "y": .8}

Goal: If I click one of the three buttons in this example I want to get the text of the specific button I clicked in return to use it for another method.

Problem: The problem I am facing is, that I always get the same text, no matter which button I click. I think this is happening because the ObjectProperty gets overwritten in every iteration of the for loop.

Surely there is a smart way to solve this, but I can not figure it out at the moment. Can anybody please explain to me what would be the best way to achieve my goal?

Edit: Okay, so since I propably won't be able to get the text of the button I will explain why I wanted the text. Maybe somebody knows a better way of approaching my problem.

There is a text input where you cant enter a pokemon name as a string and then click on a search button, that will search in a .json file for the entered string and return a list with sub_lists that stores information of this pokemon and its "form" (Shadow, Purified, Normal etc). The list will look like this:

self.information = [['Normal', 143, 'Snorlax', ['Normal']], ['Purified', 143, 'Snorlax', ['Normal']], ['Shadow', 143, 'Snorlax', ['Normal']]]

First value is the form, second value is the pokedex id, third value the name and fourth value a sub_list of the sub_list that contains the type of a pokemon.

Another list will be created (self.searched_pokemon_forms) that only stores the forms of the pokemon like shown further above. The length of this list defines the amount of buttons that will be created and each of those buttons will contain the form as text (string).

If I now click a button I want to be able to retrieve the information that I stored in the longer list (self.information) and use it to create a set of labels that display these informations, e.g.

Button with text (form) "Normal" was pressed and now I want to get the id (143), the name ("Snorlax") and the type (["Normal"]).

I hope this helps to clarify my goal.

Edit 2: I finally got it to work without putting my self.get_text() method into the App class, still it was good to know, so thank you @SHINJI.K.

I simply had to add the value to a list like this:

    def get_text(self, button):
        if len(self.text_list) != 0:
            del self.text_list[:]
        self.text_list.append(button.text)

Now I can always get the correct button text with self.text_list[-1].

Upvotes: 1

Views: 728

Answers (1)

SHINJI.K
SHINJI.K

Reputation: 308

self.form_button will always be "Shadow" since it is the last item in self.searched_pokemon_forms

Instead of using self.form_button, you might want to just modify get_text() to reference the clicked button:

    def get_text(self, button):
        return button.text

Once you click any button bound to get_text(), that button will be passed into the function as button. You can then access the text property of the clicked button using this reference.

It doesn't make sense though for the callback function to return the value since you have no way of retrieving it when it is called. I suggest that you simply do what you want to do with button.text within the callback function.

Upvotes: 1

Related Questions