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.


    How to trigger an action when a TextField is edited

    Pythonista
    action delegate textfield ui editor
    3
    4
    3770
    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.
    • camfrout
      camfrout last edited by

      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,

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

        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()
        
        1 Reply Last reply Reply Quote 0
        • JonB
          JonB last edited by

          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.

          1 Reply Last reply Reply Quote 2
          • camfrout
            camfrout last edited by

            Thanks a lot both. All the different solutions are working.

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