omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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

    Pythonista
    5
    12
    7470
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • SeaUrchin
      SeaUrchin last edited by

      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()
         ```
      1 Reply Last reply Reply Quote 1
      • Cethric
        Cethric last edited by

        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)
        
        1 Reply Last reply Reply Quote 1
        • SeaUrchin
          SeaUrchin last edited by

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

          1 Reply Last reply Reply Quote 0
          • SeaUrchin
            SeaUrchin last edited by

            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?

            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by

              Add this line at the end of the script:

              view.present()
              
              1 Reply Last reply Reply Quote 0
              • SeaUrchin
                SeaUrchin last edited by

                Thank you very much!

                1 Reply Last reply Reply Quote 0
                • ccc
                  ccc last edited by

                  def segment_action(sender):
                      seg_name = sender.segments[sender.selected_index]
                      console.alert(seg_name + ' has been selected')
                  
                  1 Reply Last reply Reply Quote 0
                  • SeaUrchin
                    SeaUrchin last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • SeaUrchin
                      SeaUrchin last edited by

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

                      1 Reply Last reply Reply Quote 0
                      • JonB
                        JonB last edited by

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

                        1 Reply Last reply Reply Quote 0
                        • AtomBombed
                          AtomBombed last edited by

                          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.

                          1 Reply Last reply Reply Quote 0
                          • SeaUrchin
                            SeaUrchin last edited by

                            @AtomBombed thanks!

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post
                            Powered by NodeBB Forums | Contributors