Forum Archive

Changing switch color

janAaron

Hi!

I'm trying to write a program where you can flip a switch, and it changes the color of the switch flipped. I made a GUI with three switches, and put the action as switch, but I don't know what code to put in the defined method switch to change the switches color once pressed.

Thanks!

import ui

def switch (sender):
    print "hi"

ui.load_view("tic tac toe.pyui").present("sheet")
zencuke

Gack. I can't remember the Markdown for entering code. And I can't figure out how to delete until I do.

zencuke

I'm not sure you can change a switch color but if you can it probably uses the view attribute tint_color. Basically you want to use sender.enabled to make the decision and probably set sender.tint_color to set the color. Everything inherits view so the full doc for any gadget is what is listed plus what is in class view.

zencuke

tint_color has an underscore in it that Markdown is supressing. Look in the doc for view class for correct spelling. I hope my poor Markdown skills aren't making this too confusing.

janAaron

Thank you!

janAaron

I switched it from 3 switches to three buttons, and re-wrote the code as this:

# coding: utf-8
import speech
import ui
view = ui.View()

def switch (sender):
    speech.say("hello")
    ui.View().tint_color = 1.00, 0.00, 0.00

ui.load_view("tic tac toe.pyui").present("sheet")

Is that what you meant, because nothing happened.

ccc

Try:

def switch(sender):
    speech.say('Hello, ' + sender.name)
    sender.tint_color = 1.00, 0.00, 0.00

Make sure that in your .pyui file the button actions are set to switch.

An example...

import ui

view  = ui.View()
red   = ui.Switch(name='Red')
green = ui.Switch(name='Green')
blue  = ui.Switch(name='Blue')
red.tint_color   = (1, 0, 0)
green.tint_color = (0, 1, 0)
blue.tint_color  = (0, 0, 1)
view.present('sheet')
red.center = green.center = blue.center = view.center
red.y   -= 100
green.y -= 60
blue.y  -= 20
view.add_subview(red)
view.add_subview(green)
view.add_subview(blue)
janAaron

That did it! Thanks :-)