In Pythonista how do you change to background colour of a button by tapping on it

12 hours ago 1
ARTICLE AD BOX

I am attempting to write a small game using Pythonista. In the game the player needs to select one of six colours, which I have working, then the tap on a selected button to change the background colour from a gray to the selected colour. The code for button tapping in Pythonista basically only allows one parameter, which must be sender, however this can be changed to multiple parameters if a lambda function is used, but this creates my error - “‘tuple’ object has no attribute ‘action’”.

The code is:

# Current color is set by the player further back in the code \\\\\\ Pythonista # Changes buttons colour to the current color def guess_tapped(sender, col): sender.background_color = col # This creates the row of guess squares. 1st one is displayed then up to # 7 subsquent ones displayed until the player guesses rightguess guess = [] for j in range(6): guess_sq = ui.Button() guess_sq.frame = (25 + (j * 65), 250, 50, 50) guess_color = 'lightgray' guess_sq.background_color = guess_color guess_sq.hidden = False guess.append(guess_sq.background_color) main_view.add_subview(guess_sq) print(f'Current color1- {current_color}') # This displays the current color #This should send the request to change the colour of the button guess[0].action = lambda sender: guess_tapped(sender, current_color) guess[1].action = lambda sender: guess_tapped(sender, current_color guess[2].action = lambda sender: guess_tapped(sender, current_color) guess[3].action = lambda sender: guess_tapped(sender, current_color) guess[4].action = lambda sender: guess_tapped(sender, current_color) guess[5].action = lambda sender: guess_tapped(sender, current_color)

The error “‘tuple’ object has no attribute ‘action’s occurs in the first line of this last section and on the 7 subsequent lines (I have to selected colours for 6 buttons). I believe the problem is in the current_color which is tuple of RGB. I have tried changing the current color to hexadecimal string with no success. I have also tried separating the current_color variable into individual parameters i.e. one each for R, G and B with no success

Where am I going wrong?

Read Entire Article