GridLayout button in loop

35 views
Skip to first unread message

Suprabhat Mohapatra

unread,
Apr 7, 2022, 2:36:54 AM4/7/22
to Kivy development
Hi,

I was trying to add buttons in each line in a GridLayout and call a function on_press of the button to pass few details from the current row where the button is clicked.

It always brings in the last item in the row and not the one where is it clicked from.

I am not sure if I am doing everything right here or any inherent bug is there in kivy as I am new to this environment and kivy also seems to be under huge development.

I understand if such is the case, then kivy should not have been published for developers to code using it.

Pls suggest.

Rgds,
Suprabhat

Gregor Verweyen

unread,
Sep 28, 2022, 1:35:05 PM9/28/22
to Kivy development
I did similar with a selectable grid and assign each button a ID. With that the klicked button is selected when klicking on it and the ID can be read in the on_ touch_down  method. It shold also be possible to get the node ID but this may be different dependent from the order in which the buttons were added. If you look into CompundSelectionBehavior you find the details - it looks complicated, but it is quite easy (just copy the code there and when creating the grid use YourGrid = SelectableGrid instead of YourGrid = GridLayout). Also the 'on_press=' shold be removed from the buttons which may fire duplicate events in this scenario. You can also put a BoxLayout into a grid row and give this the ID (in case you want e.g. a button and a Label in one row - or an image... in such case you get the event, if the boxLayout in a row is clicked and read the ID of the Box.
Reply all
Reply to author
Forward
0 new messages