Forum Archive

Changing a variable with UI switches

techteej

How would I go about changing a variable with UI switches?

I am learning from everyone's help on here. Thanks in advance.

ccc

Short answer:

the_captain_wants_warp_speed = view['button_warp_speed'].value
# or in an action function...
def warp_action(sender):
    the_captain_wants_warp_speed = sender.value
view['button_warp_speed'].action = warp_action

Long answer: warp_speed.py

techteej

Not quite sure if I'm doing this right

def warp_action(sender):
    global lang;
    val = sender.value
    if val:
      lang = 'en-US'
    else:
      lang = 'en-GB'
ccc

Looks right to me. An alternate formulation of brit_switch_action:

def brit_switch_action(sender):
    global lang
    lang = 'en-US' if sender.value else 'en-GB'
    print('lang is now set to: ' + lang)
techteej

I like that better, less code :). Anyway the reason I posted it was because it didn't work with this code below it.

def button_action(sender):
    global lang
    v['switch1'].action = brit_switch_action
    text = textview.text
    if text == '':
        speech.say('Please tell me something to say.', lang, 0.1) 
    else:
        speech.say(text, lang, 0.1)
JonB

Is v in scope here? Seems like you might want to use sender.superview['switch1']
Also, note that in this case your switch does not become 'live' until you click the button! You might want to set the switch action when you create the view, not inside the button action.

techteej

Here is what I have so far

import speech
import ui

v = ui.load_view('speech')
speech.say('Greetings', 'en-GB', 0.1) 
textview = v['textview1']
switch1 = v['switch1']

def brit_switch_action(sender):
    global lang
    lang = 'en-GB' if sender.value else 'en-US'

def button_action(sender):
    global lang
    v['switch1'].action = brit_switch_action
    text = textview.text
    if text == '':
        speech.say('Please tell me something to say.', lang, 0.1) 
    else:
        speech.say(text, lang, 0.1)

button1 = v['button1']
button1.action = button_action
v.present('sheet')

And the .pyui file:

[{"class":"View","attributes":{"name":"Siri Speak","tint_color":"RGBA(0.000000,0.478000,1.000000,1.000000)","background_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","enabled":true,"border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","flex":""},"frame":"{{0, 0}, {540, 565}}","nodes":[{"class":"Button","attributes":{"tint_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","font_size":15,"enabled":true,"font_bold":true,"name":"button1","flex":"","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","uuid":"684E2AD0-6CF1-467C-B46A-6C4060F47C1B","background_color":"RGBA(0.541378,0.045918,0.642857,1.000000)","image_name":"ionicons-ios7-volume-high-32","title":""},"frame":"{{453, 510}, {80, 37}}","nodes":[]},{"class":"TextView","attributes":{"background_color":"RGBA(0.857143,0.857143,0.857143,1.000000)","alignment":"left","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","font_size":17,"enabled":true,"flex":"","text":"Enter your text here\\n","text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","name":"textview1","corner_radius":0,"editable":true,"border_width":1,"uuid":"31844FF1-6024-47A8-B0C6-C8B45345DCE2"},"frame":"{{6, 16}, {441, 531}}","nodes":[]},{"class":"Switch","attributes":{"name":"switch1","value":true,"uuid":"379CDE5C-D02A-4EE3-B685-F53DDD0944EC","enabled":true,"border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","flex":""},"frame":"{{467, 56}, {51, 31}}","nodes":[]},{"class":"Label","attributes":{"font_size":17,"enabled":true,"text":"British","flex":"","name":"label1","border_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","text_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","alignment":"center","uuid":"90D9B957-9BAD-47E8-8FD1-972F0F77E5BB"},"frame":"{{453, 16}, {80, 32}}","nodes":[]}]}]
ccc

Please put this in a GitHub repo. It is easier to collaborate there.

techteej

It already is.

ccc

Pull request sent.

techteej

If I have two switches, it is possible for when one is active for the other to turn off?

ccc

Yes. Set .enabled to True or False the make a button active/inactive.