Forum Archive

Button thing?

Bumbo Cactoni

How would I create a button on-screen that changes color when you tap it? Like, a square that starts white with a black border then changes to a square that’s black with a white border?

cvp

@Bumbo-Cactoni like this?

import ui
v = ui.View()
v.background_color = 'gray'
def tapped(sender):
    sender.background_color = ['black','white'][sender.flag]
    sender.border_color = ['black','white'][1-sender.flag]  
    sender.flag = 1-sender.flag
b = ui.Button()
b.frame = (10,10,100,32)
b.title = 'button'
b.border_width = 1
b.flag = 0
b.action = tapped
v.add_subview(b)
tapped(b)
v.present('fullscreen') 
Bumbo Cactoni

@cvp
That was exactly what I needed!
Thank you!