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.


    Switch to next text field and update text with the same buttons

    Pythonista
    3
    16
    4346
    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.
    • image-tech-nick
      image-tech-nick last edited by

      I made a number keypad that works like a Pythonista calculator. Only difference is the I have two text fields. I added a "Next Field" button so you can switch to the next text field. How can when I switch text fields use the same number pad? I looked all through the wiki and don't see anything. There a way you check what text field is active for text input?

      def num_input(sender):
          '@type sender: ui.Button'
          t = sender.title
          sze_tf = sender.superview['size_tf'] # 1st text field
          spd_tf = sender.superview['speed_tf'] # second text field
          if t in '0123456789':
              if spd_tf.text == 'avg. speed':
                  spd_tf.text = t
              else:
                  spd_tf.text += t
          elif t == '.' and spd_tf.text[-1] != '.':
              spd_tf.text += t
          elif t == 'DEL':
              spd_tf.text = spd_tf.text[:-1]
          else:
              spd_tf.text += t
          ```
      cvp mikael 3 Replies Last reply Reply Quote 0
      • cvp
        cvp @image-tech-nick last edited by cvp

        @image-tech-nick Sorry if I don't understand correctly.
        Do you have made a Pythonista keyboard with digits buttons and a next field button?
        And do you use it in a normal script having two TextFields?

        Or do you have a normal script with the 10 digits buttons and a next field button?

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

          As your input_num def refers to sender.superview['textfield name'], I suppose that all ui objects are in the same script, thus you don't use a Pythonista keyboard.
          When you say that a TextField is active, that will say you have put the cursor on it.
          But as soon you press a digit button, the TextField would no more be active.

          1 Reply Last reply Reply Quote 0
          • image-tech-nick
            image-tech-nick last edited by

            With buttons I made a ui number pad 0-9. with the block of code above when you press the buttons it adds numbers to text field object spd_tf. I want to change to the second text field object sze_tf. Then the user can add numbers to sze_tf.

            cvp 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @image-tech-nick last edited by

              @image-tech-nick I understand that but why do you use TextFields if you don't type in them with a normal keyboard but with your special digits buttons?

              1 Reply Last reply Reply Quote 0
              • image-tech-nick
                image-tech-nick last edited by

                Is there a way to write a function that checks what text field has the curser?

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

                  Try this, quick and dirty as usual,

                  import ui
                  mv = ui.View()
                  mv.background_color = 'white'
                  def num_inp(sender):
                  	if sender.title == 'next field':
                  		sender.active = 1 - sender.active
                  		return
                  	tf_idx = sender.superview['nf'].active
                  	tf = sender.superview[['t1','t2'][tf_idx]]
                  	tf.text += sender.title
                  
                  t1 = ui.TextField(name='t1')
                  t1.frame = (10,10,100,20)
                  mv.add_subview(t1)
                  t2 = ui.TextField(name='t2')
                  t2.frame = (10,40,100,20)
                  mv.add_subview(t2)
                  for i in range(0,10):
                  	b = ui.Button()
                  	b.action = num_inp
                  	b.title = str(i)
                  	b.frame = (10,70+i*30,32,20)
                  	mv.add_subview(b)
                  nf =ui.Button(name='nf')
                  nf.title = 'next field'
                  nf.action = num_inp
                  nf.active = 0
                  nf.frame = (10,400,100,20)
                  mv.add_subview(nf)
                  mv.present() 
                  
                  1 Reply Last reply Reply Quote 0
                  • image-tech-nick
                    image-tech-nick last edited by

                    Or maybe I could have a while loop based on TextField.delegate conditions. It could break out of the loop with the "Next Field" button, hmmm!

                    cvp 1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @image-tech-nick last edited by cvp

                      @image-tech-nick said:

                      is there a way to write a function that checks what text field has the curser?

                      As soon you press a digit button, the TextField looses its cursor...thus you have to store yourself where you want, a global, or like in my little script in an attribute, which TextField was active.

                      image-tech-nick 1 Reply Last reply Reply Quote 0
                      • image-tech-nick
                        image-tech-nick @cvp last edited by

                        @cvp Thank you so much, that worked perfectly.

                        cvp 1 Reply Last reply Reply Quote 0
                        • cvp
                          cvp @image-tech-nick last edited by cvp

                          @image-tech-nick to see the cursor,
                          Add this line at end of the script

                          t1.begin_editing() 
                          

                          And this line at end of the num_inp def

                          	tf.begin_editing() 
                          

                          But, so, the keyboard appears...it is a choice that you have to do.
                          Or draw yourself a kind of cursor...

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

                            This one is better

                            import ui
                            mv = ui.View()
                            mv.background_color = 'white'
                            def num_inp(sender):
                            	if sender.title == 'next field':
                            		sender.active = 1 - sender.active
                            	tf_idx = sender.superview['nf'].active
                            	tf = sender.superview[['t1','t2'][tf_idx]]
                            	if sender.title != 'next field':
                            		tf.text += sender.title
                            	tf.begin_editing()
                            t1 = ui.TextField(name='t1')
                            t1.frame = (10,10,100,20)
                            mv.add_subview(t1)
                            t2 = ui.TextField(name='t2')
                            t2.frame = (10,40,100,20)
                            mv.add_subview(t2)
                            for i in range(0,10):
                            	b = ui.Button()
                            	b.action = num_inp
                            	b.title = str(i)
                            	b.frame = (10,70+i*30,32,20)
                            	mv.add_subview(b)
                            nf =ui.Button(name='nf')
                            nf.title = 'next field'
                            nf.action = num_inp
                            nf.active = 0
                            nf.frame = (10,400,100,20)
                            mv.add_subview(nf)
                            mv.present()
                            t1.begin_editing()
                            
                            1 Reply Last reply Reply Quote 0
                            • mikael
                              mikael @image-tech-nick last edited by

                              @image-tech-nick, just to be sure, did you consider using one of the iOS keyboard types like ui.KEYBOARD_DECIMAL_PAD? (Set with TextField.keyboard_type.)

                              cvp 1 Reply Last reply Reply Quote 0
                              • cvp
                                cvp @mikael last edited by

                                @mikael you're right but don't forget that his main question is relative to a "next field"

                                1 Reply Last reply Reply Quote 0
                                • mikael
                                  mikael @image-tech-nick last edited by

                                  @image-tech-nick, also, for usability and depending on your use case, you could consider using ui.KEYBOARD_NUMBERS which has the enter key, and then have a delegate with textfield_did_end_editing method to do the jump to the next text field.

                                  1 Reply Last reply Reply Quote 0
                                  • cvp
                                    cvp @image-tech-nick last edited by cvp

                                    @image-tech-nick or see here to use keyboard like

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