Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Help with Ui Segment Bar
-
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() ```
-
ui.SegmentControl
has a generalised action attribute
Ieimport 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)
-
Oh ok, I see, so you just made it yourself instead of the ui feature. Thanks!
-
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?
-
Add this line at the end of the script:
view.present()
-
Thank you very much!
-
def segment_action(sender): seg_name = sender.segments[sender.selected_index] console.alert(seg_name + ' has been selected')
-
I have also changed the console alerts to speech, so that it can be said while the Ui is running. Very cool speech module!
-
Whoops, didn't see your post before mine. I will try that as well!
-
console.hud_alert is also a good alternative to console.alert, as it does not interfere with the ui.
-
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.
-
@AtomBombed thanks!