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.
GUI textfield example
-
Hi -
I am looking for an example of code to read/get text from a textfield using the ui module. I have tried a number of things but can't seem to figure it out.
This is one attempt:import ui v = ui.load_view('descent_calc') v.present('sheet') t1 = 120 def text_entered(sender): '@type sender: ui.Textfield' t1 = str(sender.text('textfield1')) print(t1)
If someone could point me to some good examples it would be great. Thanks.
John. -
@jcallum , I hope this helps. I have tried to keep it simple. But it just uses a delegate as described in the help file for the ui.TextField.
import ui class MyTextFieldDelegate (object): def textfield_should_begin_editing(self, textfield): return True def textfield_did_begin_editing(self, textfield): pass def textfield_did_end_editing(self, textfield): pass def textfield_should_return(self, textfield): textfield.end_editing() return True def textfield_should_change(self, textfield, range, replacement): return True def textfield_did_change(self, textfield): print(textfield.text) #only changed this #pass f = (0, 0, 300, 480) v = ui.View(frame=f) tf = ui.TextField(frame = v.bounds) tf.height =32 tf.delegate = MyTextFieldDelegate() v.add_subview(tf) v.present('sheet')
-
Thanks very much. That works. I need to study it carefully to see why. This OOP is not intuitively obvious to me!
John. -
Another approach...
import ui user_value = '120' def text_entered(sender): user_value = sender.text print(user_value) text_field = ui.TextField() text_field.action = text_entered text_field.text = user_value text_field.present()
-
@jcallum So True.
As much as I love Pythonista, and though UI Help Module has the details, it is not intuitive for beginners like me.But going through 2000 odd posts in this forum really helps. And friends on this forum are really helpful, including the developer @omz !
Also because several elements in UI module have different capabilities of action and the fact that some of these require delegates makes it complex.
Maybe next update will have more examples pertaining to UI Module in documentation. :)
-
I highly recommend the ui-tutorial and scene-tutorial for learning theses Pythonista modules: https://github.com/humberry
-
Guys, I would just say don't be scared of the delegates. I admit I hard a hard time with them when I started. But it was more out of fear than anything else. But in reality they are so easy to use and they make a lot of sense to have them when you want more control over a object.
Still @ccc approach here is a lot more straight forward for the problem at hand. Also keep in mind, these delegate classes can be copied directly from the help file. No need to memorise them. -
Below is a better example of using a delegate, I think. But it also shows how flexible the delegates are. The fact that you can point the delegate to your own class is very nice. Most of the lines are setting up the view. To make a real,example you need it. But 95% of the lines in the example are just a pattern so to speak.
This is just another way. Not saying it's the best way.# Pythonista Forum - @Phuket2 import ui class MyClass(ui.View): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.make_view() self.value = None def make_view(self): tf = ui.TextField(frame = self.bounds.inset(10, 10)) tf.height =32 tf.delegate = self tf.flex = 'w' self.add_subview(tf) # because the delegate is pointed to this class, tf.delegate=self, # then you can define the delegate methods here. in the docs it # explains you only need to define the methods you need. the meaning # being is that the the caller is checking to see if the method # exists before calling it. def textfield_did_change(self, textfield): self.value = textfield.text if __name__ == '__main__': w, h = 600, 800 f = (0, 0, w, h) style = 'sheet' mc = MyClass(frame=f, bg_color='white') mc.present(style=style, animated=False) mc.wait_modal() print(mc.value) ```
-
Btw, in the next version of Pythonista the make_view can be like below. But in the latest beta you can pass all those Params to any ui.View subclassed object
def make_view(self): tf = ui.TextField(frame = self.bounds.inset(10, 10), height=32, delegate=self,flex='w') #tf.height =32 #tf.delegate = self #tf.flex = 'w' self.add_subview(tf)