Hello,

I am wondering the best way to go about this in python. Basically what I want is to encrypt a password (using something in python or open source) while masking the password in a wxpython text field(i.e. instead of 'password' it will show '********' on the screen). I realize I can create a event in wxpython to output * for each char instead of the regular char but I'm wondering if there's a way to show a variable amount of '*' to hide the actual length of the password as well. Any suggestions?

That's going to be awfully confusing to users.

I mean, they type in the 3rd letter say, think they've got it wrong and look at the screen to see 4 stars.

What do they do next, press delete twice I wager and make a bad situation worse.

Comments
Programmers rarely think about ux.

Well the simplest solution is just to use the style flag wx.TE_PASSWORD when you create the TextCtrl for the password. That automatically makes the output asterisks.

Salem has a very good point, it would be very confusing to the user to show ***** that does not match the password string in length.

One thing you can do, is to make the password dots less visible from a distance and clear after entry ...

# testing wxPython's
# wx.TextCtrl(parent, id, value, pos, size, style)
# for password entry and masking

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
        self.SetBackgroundColour("yellow")

        self.pw = wx.TextCtrl(self, -1, value="", size=(200, 20), 
            style=wx.TE_PASSWORD|wx.TE_PROCESS_ENTER)
        # optional, make the black dots less visible
        self.pw.SetBackgroundColour("blue")
        # respond to enter key when focus is on edit1
        self.pw.Bind(wx.EVT_TEXT_ENTER, self.onEnter)
        # label for result
        self.result_label = wx.StaticText(self, -1)

        # this is the main sizer
        sizer_v = wx.BoxSizer(wx.VERTICAL)        
        # framed static box
        sbox = wx.StaticBox(self, wx.ID_ANY,
            "Enter your password below:", size=(200, -1))
        # group widgets vertical in the staticbox
        sizer = wx.StaticBoxSizer(sbox, wx.VERTICAL)        
        sizer.Add(self.pw, 0, wx.ALL|wx.EXPAND, border=5)
        sizer.Add(self.result_label, 0, wx.ALL|wx.EXPAND, border=5)
        
        sizer_v.Add(sizer, 0, wx.ALL|wx.EXPAND, border=10)        
        self.SetSizerAndFit(sizer_v)
        
    def onEnter(self, event):
        pw = self.pw.GetValue()
        # optional, clear the password entry
        self.pw.Clear()
        self.result_label.SetLabel(pw)  # test


app = wx.App(0)
# create a MyFrame instance and show the frame
MyFrame(None, 'password entry', (220, 150)).Show()
app.MainLoop()
This article has been dead for over six months. Start a new discussion instead.