Passing arguments when calling a function inside the on_click() event

Hi there, Im trying to create a Pixel Art game in which the player can choose a color to draw something. I have multiple colored buttons, as shown in the image and i declared the variables for them and then placed them in a list. What Im trying to achieve is that for each color button in the list Im using the on_click event and then call a function named set_color inside the on_click method. The problem is that when Im passing an argument into the set_color function, the program doesnt work, the buttons arent clickable. It seems to only work if I don`t pass any arguments into the set_color function.

Did anyone ever try to do something similiar and could help me. I know I could create a separate function for each on_click event for the colored buttons, however it would require writing a lot of repetitive code and I`m trying to achieve a more optimized code.

So the solution is actually to use a lambda function when passing the argument into the set_color() function.

3 Likes

Hi @irinabacal :wave: @Alessio_Bonini created a drawing game [My drawing game] you can follow the discussion by the link.

2 Likes