Forum Archive

Help with Ui Segment Bar

SeaUrchin

Hi everyone! This is my first day coding ever, and I have chosen this app to start with! The question I am about to ask and the things I'm about to show you are very basic, so please bare with me.
So I am starting out with making a simple program to be able to take a poll. After doing research I found that to give UI things to do you must give them an action. With the segment bar there is no option to add a action, so I was wondering how I would add one for it. If it helps, here is my code. Thank you all in advance!
import ui import console def segment1(self): console.alert('Segment 1 Has been selected and accepted!') def segment2(self): console.alert('Segment 2 Has been selected and accepted!') v=ui.load_view() v.present()

Cethric

ui.SegmentControl has a generalised action attribute
Ie

import ui
import console

def segment1(sender):
    console.alert('Segment 1 Has been selected')

def segment2(sender):
    console.alert('Segment 2 Has been selected')

def segment_action(sender):
    selection = sender.selected_index
    if selection == 0:
        segment1(sender)
    elif selection == 1:
        segment2(sender)

view = ui.View()
segview = ui.SegmentedControl()
segview.segments = ['seg1', 'seg2']
segview.action = segment_action
view.add_subview(segview)
SeaUrchin

Oh ok, I see, so you just made it yourself instead of the ui feature. Thanks!

SeaUrchin

This does not seem to run in the output. I'm sorry if I needed to do something, after all this is my first day programming. What do I do to make it run?

ccc

Add this line at the end of the script:

view.present()
SeaUrchin

Thank you very much!

ccc
def segment_action(sender):
    seg_name = sender.segments[sender.selected_index]
    console.alert(seg_name + ' has been selected')
SeaUrchin

I have also changed the console alerts to speech, so that it can be said while the Ui is running. Very cool speech module!

SeaUrchin

Whoops, didn't see your post before mine. I will try that as well!

JonB

console.hud_alert is also a good alternative to console.alert, as it does not interfere with the ui.

AtomBombed

If you want to make a console.alert pop up over a UI, then just do this:

@ui.in_background
def popup():
    console.alert("hello")
popup()

This makes the program run in the background that way you can have it pop up.

SeaUrchin

@AtomBombed thanks!