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 last edited by technoway
Check out this topic. This probably has what you need.
GUI textfield example
There's also a good example in the last post in this thread:
Basic UI Question
These other topics have useful information.
textfield validation example
Ui.textfield drives slider
Pre fill Textfield
Plotting on a single screen with text fields
How to set focus to textfield (2)
How to set the focus on a TextField?
Tell cursor position in a TextField?
How to reference textfield
Button + Textfield Saving to Variable
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.
@technoway WOW that is way more than I expected! Thank you so much for these references. I will dig in.
omz last edited by omz
To summarize, there are essentially two ways of interacting with a
- Set its
actionattribute 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')
- 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
- Set its
@omz Thank you.