Forum Archive

Password Fields in UI

lachlantula

Hi,

I've created a program using the UI editor with a single text field being used for logging in. First the password is entered, so I set the text field to be a password field in the editor. Is there any way I can switch back to a regular text field (ie. Typing without the dots) in code?

Thanks!

Phuket2

@lachlantula , sorry I don't have an exact answer for you. I doubt you can do what you want to do though exactly using the one field, but I am not really sure.
But you could have 2 fields on the view. Hide and show them depending on if the user clicked a button on the view to show or hide text for example.

abcabc

Set secure to False

import ui

v = ui.View(frame=(0,0,400,100))
t = ui.TextField(frame=v.frame)
#t.secure = True
t.secure = False
v.add_subview(t)
v.present('sheet')

Phuket2

@abcabc , nice you are right. The label in the Designer would be better if it was 'Secure' though. I did a dir print and looked at the help, I still missed it. My brain was looking for something else.

Phuket2

There appears to be a problem here though when using the system font. It wants to default to times font, even you set it explicitly. Something like Menlo font behaves a lot better, or as expected.

import ui

_font = ('Menlo', 24)
#_font = ('<System>', 24)

def btn_action(sender):
    fld = sender.superview['pwd']
    fld.secure = not fld.secure
    fld.font = _font
    if fld.secure:
        sender.title = 'Clear Text'
    else:
        sender.title = 'Protected'

v = ui.View(frame=(0,0,400,100), bg_color ='white')
t = ui.TextField(name = 'pwd', frame=(0,0,v.width, 48))
t.font= _font
#t.secure = True
t.secure = False
v.add_subview(t)
btn = ui.Button(frame = (10,0, 80, 32), title = 'Protected')
btn.y = t.frame.max_y + 10
btn.width += btn.width * 1
btn.border_width = .5
btn.corner_radius = 3
btn.action = btn_action
v.add_subview(btn)
v.present('sheet')
lachlantula

@Phuket2 that was my original idea, but I'd rather a solution that's a bit cleaner, just like what @abcabc suggested - which works perfectly! Interesting find though Phuket, I didn't run into that w/ Pythonista 3 and the UI builder.

Phuket2

@lachlantula , no problems. I was just intrested in trying it after @abcabc pointed out the secure attr. I have see things go strange before when you play with the size of the system font. But initially I was using the default size. Oh, well