wxpythonwxtextctrl

wxPython multiline password field


In wxPython, the password style works only for single line text controls. I need a multiline password field. I've thought of two ways:

  1. I've created a font with a single glyph (solid dot) in every code point. However, I can understand users not wanting fonts installed on their machines willy-nilly. In wxWidgets you can use private fonts, but not in wxPython. I am missing a way of dynamically loading this font for this particular dialog.

  2. Subclassing wx.TextCtrl and implementing storing the text as entered, but displaying only a single character. This sounds a lot more complicated. And need some suggestions of how I might approach this.

I DO need this, I HAVE thought it over. So, I'm looking for some thoughts on the two ways I've thought of above, or any other possible implementations.


Solution

  • Storing the password in a textctrl as it's entered!

    import wx
    
    ########################################################################
    class LoginDialog(wx.Dialog):
        """
        Class to define login dialog
        """
        def __init__(self):
            wx.Dialog.__init__(self, None, title="Login")
            self.logged_in = False
            self.attempts = 3
            self.stored_password = ""
            # user info
            user_sizer = wx.BoxSizer(wx.HORIZONTAL)
            user_lbl = wx.StaticText(self, label="Username:")
            user_sizer.Add(user_lbl, 0, wx.ALL|wx.CENTER, 5)
            self.user = wx.TextCtrl(self)
            user_sizer.Add(self.user, 0, wx.ALL, 5)
    
            # password info
            p_sizer = wx.BoxSizer(wx.HORIZONTAL)
            p_lbl = wx.StaticText(self, label="Password:")
            p_sizer.Add(p_lbl, 0, wx.ALL|wx.CENTER, 5)
            self.password = wx.TextCtrl(self, style=wx.TE_MULTILINE)
            self.password.Bind(wx.EVT_TEXT,self.OnMask)
            p_sizer.Add(self.password, 0, wx.ALL, 5)
    
            main_sizer = wx.BoxSizer(wx.VERTICAL)
            main_sizer.Add(user_sizer, 0, wx.ALL, 5)
            main_sizer.Add(p_sizer, 0, wx.ALL, 5)
    
            btn = wx.Button(self, label="Login")
            btn.Bind(wx.EVT_BUTTON, self.onLogin)
            main_sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
    
            self.SetSizer(main_sizer)
    
        def OnMask(self, event):
            disp_pass = self.password.GetValue()
            #Test for backspace/clear event
            p_len = disp_pass.count('*')
            if p_len < len(self.stored_password):
                self.stored_password = self.stored_password[:p_len]
                return
            #Store the last input character
            try:
                char = disp_pass[-1]
                self.stored_password = self.stored_password + char
            #Mask the input
                self.password.ChangeValue("*" * len(disp_pass))
            except:
                pass
    
        def onLogin(self, event):
            valid_password = "password1\npassword2\npassword3"
            user_password = self.stored_password
            if user_password == valid_password:
                self.logged_in = True
                self.Close()
                return
            else:
                wx.MessageBox('Login failed', 'Error', wx.OK | wx.ICON_ERROR)
                self.stored_password = ""
                self.password.SetValue("")
            self.attempts -= 1
            if self.attempts < 1:
                wx.MessageBox('Too many Login attempts', 'Error', wx.OK | wx.ICON_ERROR)
                self.Close()
    
    class MyPanel(wx.Panel):
        def __init__(self, parent):
            wx.Panel.__init__(self, parent)
            user_lbl = wx.StaticText(self, label="Log in Successfull")
    
    class MainFrame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, title="Main App")
            panel = MyPanel(self)
            dlg = LoginDialog()
            dlg.ShowModal()
            authenticated = dlg.logged_in
            dlg.Destroy()
            if not authenticated:
                wx.MessageBox('Login failed', 'Error', wx.OK | wx.ICON_ERROR)
                self.Destroy()
            self.Show()
    
    if __name__ == "__main__":
        app = wx.App()
        frame = MainFrame()
        app.MainLoop()
    

    Note the self.password.ChangeValue() rather than self.password.SetValue(), prevents the EVT_TEXT from being fired on the update.