Forum Archive

What's wrong with this?

janAaron

This is a program I wrote which is supposed to change a button's image from a smiley face to an ant when the face is touched. When I ran it, it did change, but also kept the face image. How do I get it so that it gets rid of the face when the image is touched?

Here's the code:

#coding: utf-8
import ui

def pressed (sender):
    sender.tint_color = 1.00, 0.00, 0.00
    button1.background_image = ui.Image.named('Ant')

view = ui.View()
view.name = "tic tac toe"
view.background_color = 0.90, 0.90, 0.90
view.present("sheet")


button1 = ui.Button(title = "😄")
button1.flex = "LRTB"
button1.action = pressed
view.add_subview(button1)
button1.center = (view.width *0.5,view.height *0.5)
button1.font = ('AmericanTypewriter-Condensed', 60)
button1.size_to_fit()

Thanks!

dgelessus

You're using two different types of button "decoration" here - the first one, the smiley face, is technically text, a string containing an Emoji character. The second one, the ant, is technically an image. This leads to the two types overlapping instead of replacing each other.

It would probably be easiest to have the pressed method set the button text to the ant emoji (🐜), although you could of course also use the image version of the smiley face initially. Both methods work just fine.

janAaron

Thanks!