Forum Archive

Referring to ui.TextField

tmessers

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=((2mg)+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,((2mg)+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, ((2mg)+h),w,h))) #tf2
v.add_subview(make_button(title='Umrechnen',frame=(mg1,((6
mg)+(5h)),w,h)))
v.add_subview(make_button(title='Felder leeren',frame=(mg2,((6
mg)+(5*h)),w,h)))
v.present() ```

Can anybody help me?

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

cvp

@mcriley821 said:

user_typed = v['name_of_label'].text

user_typed = v['name_of_TextField'].text

tmessers

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

tmessers

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

mcriley821

@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)

mikael

@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.