I want to design a text field for my simple calculator, so a textfield where I can write number in. I found that here https://omz-software.com/pythonista/docs/ios/ui.html#textfield but I don‘t know where I beginn?
Forum Archive
Textfield with ui
@Python567 start from here
import ui
tf = ui.TextField()
tf.name = 'example for @Python567'
tf.frame = (0,0,300,32)
tf.keyboard_type = ui.KEYBOARD_DECIMAL_PAD
tf.present('sheet')
tf.begin_editing()
Oh, thanks. But two question.
1. For what is the „tf“ ?
2. And there I can write something else in? For example „TextField.action = ...“ ?
@Python567
tf is the name you give to your TextField
to set something in iT, use the text attribute
tf.text = 'your text'
Ok and where/how can I define that for example:
```
if textfield == „Test123“:
print („Hello“)
@Python567 it depends: if you want to test that during introduction of data or at end...
@Python567 try this and press enter at end of typing Test123
import ui
tf = ui.TextField()
tf.name = 'example for @Python567'
tf.frame = (0,0,300,32)
tf.keyboard_type = ui.KEYBOARD_DECIMAL_PAD
tf.text = ''
def tf_action(sender):
if sender.text == "Test123":
print("Hello")
tf.action = tf_action
tf.present('sheet')
tf.begin_editing()
@Python567 you can use also this
tf.placeholder = 'Your own explanation'
test it
Oh thanks, i test it
I have here a programm which can find out all prime numbers. But I have the Problem, that I only can print the result in the console. I write tf.text = Right but it don‘t works why? I get a error with strand int. But I don‘t know how to fix that.
```
import ui
dialog = ui.View()
tf = ui.TextField()
tf.name = 'Test'
tf.frame = (50,50,1000,100)
tf.keyboard_type = ui.KEYBOARD_DECIMAL_PAD
tf.text = ''
tf.placeholder = 'Gib was ein!'
def primzahl():
p = int(tf.text)
i = 2
istPrimzahl = True
teiler = 0
while (i < p):
if p % i == 0:
istPrimzahl = False
teiler = i
break
i += 1
if istPrimzahl:
tf.text = 'Die Zahl', p , 'ist eine Primzahl.'
else:
print ("Die Zahl",p, "ist keine Primzahl, weil sie durch " , i , " teilbar ist.")
def tf_action(sender):
primzahl()
tf.action = tf_action
dialog.add_subview(tf)
ebutton = ui.Button()
ebutton.title = '='
ebutton.frame = (50,200,1000,100)
ebutton.background_color = "red"
def ebutton_action(sender):
tf.text = "gedrückt!"
ebutton.action = ebutton_action
menubutton = ui.ButtonItem()
menubutton.title = 'fghjk'
dialog.right_button_items = [menubutton]
dialog.add_subview(ebutton)
dialog.present('fullscreen')
tf.begin_editing()
sheet
@Python567 try already with
tf.text = 'Die Zahl ' + str(p) + ' ist eine Primzahl.'
No, thanks:)
@Python567 said:
No
Why no?
@cvp Sorry, I wanna write a first something else wich beginn with „no“. 😐
@Python567 no problem 😉
F-strings (tf.text = f'Die Zahl {p} ist eine Primzahl.') are a single instruction in Python bytecode.
This makes them faster and more efficient than legacy string formatting https://www.scivision.dev/python-f-string-speed
Improved readability is also a clear value.
@cvp that code works cool but what if I want to use it without presenting, the keyboard shortcuts?
@MrEVandQuestions said:
use it without presenting, the keyboard
Remove the line
tf.begin_editing()
OK. Thanks!