Forum Archive

Understanding TextField

Sketo

Once I place a TextField on the grid in the .pyui editor, what does the script in the .py file look like to retrieve the text input by the user? An example would be great.

I understand the action attribute that shows when you place a button on the grid but action does not show on a TextField, so I’m not sure how to make it call a function, like button, or store the user input.

Any help would be great.

technoway

Check out this topic. This probably has what you need.
GUI textfield example
https://forum.omz-software.com/topic/3609/gui-textfield-example

There's also a good example in the last post in this thread:
Basic UI Question
https://forum.omz-software.com/topic/3503/basic-ui-question

These other topics have useful information.
textfield validation example
https://forum.omz-software.com/topic/2499/textfield-validation-example/2

Ui.textfield drives slider
https://forum.omz-software.com/topic/3051/ui-textfield-drives-slider/8

Pre fill Textfield
https://forum.omz-software.com/topic/2379/pre-fill-textfield

Plotting on a single screen with text fields
https://forum.omz-software.com/topic/1665/plotting-on-a-single-screen-with-text-fields/7

How to set focus to textfield (2)
https://forum.omz-software.com/topic/1453/how-to-set-focus-to-textfield-2

How to set the focus on a TextField?
https://forum.omz-software.com/topic/955/how-to-set-the-focus-on-a-textfield/4

Tell cursor position in a TextField?
https://forum.omz-software.com/topic/1442/tell-cursor-position-in-a-textfield

How to reference textfield
https://forum.omz-software.com/topic/863/how-to-reference-textfield

Button + Textfield Saving to Variable
https://forum.omz-software.com/topic/848/button-textfield-saving-to-variable

about 'TextField'
https://forum.omz-software.com/topic/740/about-textfield

I used the "Search" feature above to find these last few topics, but the first one I found using Google. I searched for "Pythonista TextField example" without the double quotes. That lead to this forum.

I'd have given an example myself, but while I've written several Pythonista programs now, I still haven't used a TextField. I learned a lot searching for this! Good luck.

Sketo

@technoway WOW that is way more than I expected! Thank you so much for these references. I will dig in.

omz

To summarize, there are essentially two ways of interacting with a ui.TextField:

1) Set its action attribute programmatically. This is currently not possible in the UI editor, but still easy to do: After loading the view, you can access the text field by its name (given in the UI editor) like this:

def text_field_action(sender):
    print('Text was entered:')
    # `sender` is the text field that caused the action.
    print(sender.text)

v = ui.load_view('myui.pyui') # load the view
tf = v['textfield1'] # Access subview by name
tf.action = text_field_action # Important: No parentheses here!
# ...
v.present('sheet')
  1. By setting the text field's delegate. This is a lot more complex, but it allows you to control the behavior of the text field while text is entered (and not just afterwards, like with an action).
Sketo

@omz Thank you.