Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Binding Buttons to Callback Functions

37 views
Skip to first unread message

Edward Anderson

unread,
Apr 11, 2025, 11:49:38 AMApr 11
to Kivy users support
I am attempting to generate buttons with loops in a py file and then display them in a kv file. Both files are attached.  When I rem out the py line labeled "Problem Child" it works as desired by displaying an array of 150 random color and random text buttons that change color when clicked. When "Problem Child" is not remmed out, no display appears. an error message is generated and the "SelectTile" callback prints the correct data for he first button only. How do I bind the same callback to all 150 buttons or bind a common callback to all buttons?
Main.kv
test.py

Chisoul Munthali

unread,
Apr 12, 2025, 9:17:08 AMApr 12
to kivy-...@googlegroups.com
Check out this solution to address your issue you encountered, the source code is self-explanatory.

On Fri, 11 Apr 2025, 17:49 Edward Anderson, <edand...@gmail.com> wrote:
I am attempting to generate buttons with loops in a py file and then display them in a kv file. Both files are attached.  When I rem out the py line labeled "Problem Child" it works as desired by displaying an array of 150 random color and random text buttons that change color when clicked. When "Problem Child" is not remmed out, no display appears. an error message is generated and the "SelectTile" callback prints the correct data for he first button only. How do I bind the same callback to all 150 buttons or bind a common callback to all buttons?

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/kivy-users/2e9d96b6-f995-4ab9-8627-c0751cdf38d8n%40googlegroups.com.
test.py
test.kv

ElliotG

unread,
Apr 12, 2025, 9:51:11 AMApr 12
to Kivy users support
A more object orientated approach would be to recognize that a Tile is a Button, and you can create a specialized Button class, and use it as you would a Button, rather than creating a Utility class.

class TileStack(GridLayout):  # use a layout to hold the buttons
    TARGET = 0
    N_ROWS = 10
    N_COLS = 15

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        TARGET = random.randint(0, 9) + random.randint(0, 9)
        print(TARGET)
        for i in range(0, self.N_ROWS):
            for j in range(0, self.N_COLS):
                rand_int1 = random.randint(100, 254) / 255
                rand_int2 = random.randint(100, 254) / 255
                rand_int3 = random.randint(100, 254) / 255
                rand_int4 = random.randint(0, 9)
                my_color = (rand_int1, rand_int2, rand_int3, 1)
                my_id = str(i) + '-' + str(j) + '-' + str(rand_int4)
                self.add_widget(Tile(my_id, my_color, rand_int4))    # instance a Tile and add to the Layout 


class Tile(Button):  # a Tile is a Button
    def __init__(self, my_id, my_color, my_value, **kwargs):
        super().__init__(**kwargs)
        self.text = str(my_value)
        self.font_size = 32
        self.bold = True  # use True not a string "true"
        self.color = (0, 0, 0, 1)
        self.background_normal = ""
        self.background_color = my_color
        self.tile_id = my_id              
        self.bind(on_press=self.selected)

    def selected(self, obj):
        print(f"selected {self.tile_id}")  # self refers to this instance of the button


test.py

Edward Anderson

unread,
Apr 12, 2025, 10:18:02 AMApr 12
to kivy-...@googlegroups.com
Thank you all. Elliot's solution wass exactly what I was attempting to accomplish!

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages