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.
How to trigger an action when a TextField is edited
-
Hi everyone,
I read in the doc and on the forum that text fields have an action attached to them. When I looked in the UI editor, I don't see the possibility to define an action (but I do have this possibility on a button for example).
I also tried to manually edit my pyui to add an action on my TextField but it is never triggered.
Is there a way to define such an action without putting in place all the delegate piece of code?
Best regards,
-
I don't believe so. I think you have to code in the delegates yourself. It's not too bad though. Something like this should work (NB: Not tested).
import ui class MyTextFieldDelegate(object): def textfield_did_change(self, textfield): print(textfield.text) textfield = ui.TextField() textfield.delegate = MyTextFieldDelegate()
-
The textfield does have an action attribute, but it is not settable in the ui editor (pribably an oversight). Often people set this in the custom class init code. But you can also create a custom attribute in the ui editor. If you do something like:
{'action':myfun}
then be sure to define myfun before you load the view
def myfun(sender): print sender.text v=ui.load_view(your_pyui_name)
then the textfield is created, and the action is set to myfun.
You could also simply do something like
v=ui.load_view(your_pyui_name) v['textfield1'].action=myfun
Often people create a custom class for the top level view which handles the various delegate methods which are not easily settable via the ui editor.
-
Thanks a lot both. All the different solutions are working.