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.


    Referring to ui.TextField

    Pythonista
    4
    7
    1837
    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.
    • tmessers
      tmessers last edited by

      Hello everybody, within my code i am creating ui.TextFields by a function. I need the input of that TextFields as values for further calculations. My question is, how i can refer to that TextFields?
      Below i‘m showing my code with the GUI part.

      import ui
      
      def button_tapped(sender):
      	if sender.title == "Umrechnen":
      		lon = tf1.text 
                      Lat = tf2.text
      	elif sender.title == "Felder leeren":
      		tf1.text = ""
      		tf2.text = ""
      
      mg=20
      w=100
      h=30
      mg1=((2*mg)+w)
      mg2=((3*mg)+(2*w))
      
      
      def make_label(text,frame):
      	return ui.Label(text=text, frame=frame, bordered=True, border_width=2)		
      
      def make_textfield(text,frame):
      	return ui.TextField(text=text, frame=frame, bordered =True, clear_button_mode='always')
      
      def make_button(title, frame):
      	return ui.Button(title=title, frame=frame, bordered=True, border_width=2,	action=button_tapped)
      
      
      v=ui.View(name='Koordinaten',bg_color='grey',tint_color='black',border_width=1)
      v.add_subview(make_label(text="Nordwert",frame=(mg1, mg, w,h)))
      v.add_subview(make_label(text="Ostwert",frame=(mg2, mg,w,h)))
      v.add_subview(make_label(text="Dezimalgrad",frame=(mg,((2*mg)+h),w,h)))
      v.add_subview(make_textfield(text='',frame=(mg1,((2*mg)+h),w,h))) #tf1
      v.add_subview(make_textfield(text='',frame=(mg2, ((2*mg)+h),w,h))) #tf2
      v.add_subview(make_button(title='Umrechnen',frame=(mg1,((6*mg)+(5*h)),w,h)))
      v.add_subview(make_button(title='Felder leeren',frame=(mg2,((6*mg)+(5*h)),w,h)))
      v.present() ```
      
      Can anybody help me?
      mikael 1 Reply Last reply Reply Quote 0
      • mcriley821
        mcriley821 last edited by mcriley821

        Do you mean read the TextField text?

        user_typed = v['name_of_Textfield'].text
        

        Just make sure you name the TextField objects

        Edit: @cvp

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

          @mcriley821 said:

          user_typed = v['name_of_label'].text

          user_typed = v['name_of_TextField'].text
          
          1 Reply Last reply Reply Quote 1
          • tmessers
            tmessers last edited by

            Thanks for the answers.
            Yes, i want to read the TextFields.
            How can i name them?

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

              I mean, how can i name it by using the function to create it?
              Where in the documentation can i find information about it?

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

                @tmessers Just pass an argument called name to your make_textfield function and say name=name during the ui.TextField call.

                def make_textfield(text,frame,name):
                    return ui.TextField(text=text,frame=frame,name=name)
                

                You can find all you’d need in

                http://omz-software.com/pythonista/docs/ios/ui.html#module-ui

                If I’m not mistaken, most ui objects (ui.Label, ui.TextView, etc) inherit the attributes of a ui.View (like name)

                1 Reply Last reply Reply Quote 1
                • mikael
                  mikael @tmessers last edited by

                  @tmessers, name works, but in this case you could also just use global variables, so instead of:

                  view.add_subview(make_textfield())
                  

                  ... you would have e.g.

                  field1 = make_textfield()
                  view.add_subview(field1)
                  

                  I tend to never use the name approach, because then you do not get code completion. Of course, if you use the UI editor to create the fields, then you have to use names.

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