Create a customized password dialog (Phoenix)

Keywords : Dialog, TextCtrl, Password, CheckBox.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


First example

img_sample_one.png

ICON file : wxwin.ico

   1 # sample_one.py
   2 
   3 import os
   4 import wx
   5 from   wx.lib.embeddedimage import PyEmbeddedImage
   6 
   7 # class MyFrame
   8 # class MyDlgLogin
   9 # class MyApp
  10 
  11 Key_Png = PyEmbeddedImage(
  12     b'iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAACXBIWXMAAAsTAAALEwEAmpwY'
  13     b'AAAKU0lEQVRoBe2aaXBV533Gf2e5517p6mqXkARahBYDYsEglhG4SbBZHPBiJ23G6ZdMnHxI'
  14     b'xp52OvnSTnA77dClkzbJmLgd4oZAxw4uocbU4NQRBBtjBAgkawEhoUW6aNGVdBfp7vec85YZ'
  15     b'vR80GtJJMxfUzOSZ+c97vun3PHre9yySIoTgd1kqS63fG/gdl84CKYrCo9KxlXnFaoxvZ+Xb'
  16     b'fxgNMXQ0EPuz5khyBIjxG0oIgc4j1turCvV4VHzJ5RCHap9O1Kx+KcbwOWON/1+dRf1J65XB'
  17     b'lNUFRPm/uJDDw9abNUU7j9UWXWx+Pkf4TjqEGEaIgfvTj+j6E5f4YW7WzVqHtg3I/E3Z5TH6'
  18     b'cCt0ZE3xKpHg78obUs9s+EpEK21MojiAWUBIXAfcPWpw8XXH3eOxxLcuJcwWIAKIJavQ4Q0l'
  19     b'BVpc+YusPPMbG56LZNc/HUPPFhBifjTAiaSB2peTaDq1/MA4fM+0vzlo2TeBCL9eD+83cHhj'
  20     b'6S6XxpsNT0aq1z4bIavSghAwBwAYEt4hry0AwA0DP3Jw/vv67e/FE9/stew2IPpIK3R4U2ml'
  21     b'Q1Gad7/qr61+Mg5hmXgS0CW4hBeoDN9Q8d3QcWTalDWmuF84ho46+Phf9DvHo8lXL6SsK8g6'
  22     b'PZIKWUnth9u/Gqqt3hWHSSAo6+KS4IZM3mkwfKWcjw/6kh3T1mSppjs3NOjF61+JUf2NFEaG'
  23     b'eMz4nuNIwBYvt1n2dSAMiId6I/v+xhWvVG+OP7fxhTCEAD+gL4SXowFUMPLLUn4+Fr/9T6Ho'
  24     b'+9/xh/+5s0Pp8l0yIAzLD5h87tVU1auG4/A6Td38oNNJTSv8lhU1OYX2azu/HkIzBASQSS+C'
  25     b'd0oDSiF2TBVjtj0B3ADOxSxxJjWtQoYCloeyPRZPfc1c/R1Df2Orpm4B3ICSdgOvN5U7VEV5'
  26     b'4wsv+4vyqlLgAywgY+FmXWxijOoDjcqXM7Q1uaoSqdRVX52qPZ7bYIHDCWv3Q0ED5Qcs9n3b'
  27     b'XP3nLu3fD2jqLiBLmkjfHojGlT/dtCu8p2p7HPxAQtbGWDiL9oB/hIqmLl44+PXy6iNnDztT'
  28     b'iVjt58MrVuxJQqoeMiphzWqInKJ4Zwc7LFbM/lg99H7EfhnoBqJpMfCP2yuallcl/3LHV4MQ'
  29     b'B8KLYOeB5SrHBZQqKPeaeeyZBio2VRWIuVEyimdQXEVQLoNWyyBvPQx1kLHMxtCUEmAdMJo2'
  30     b'A4rgW4/vnXPf/wEwBqgPSn+RKV0FxQOV2TAzRYY2BrYLjEZYthmcy4F8iIbh9i+wgtB/Quc/'
  31     b'wvYAkAnoaatQIGk237ng/uOahphi5NkQAfTFlVl4rczDkw9aLhTnQ3EukAfkA9ly8qHjTQhN'
  32     b'M9yscaaL0Xdtuw0YBmbTton/tm3s9Eet6tuXfpwDSZmPrMzikfASNkeOB8iWq1uuZeDthLu/'
  33     b'JHQXOi+osZ8I6ybQCdwCwuk8hSL/dnfqtU9aXN1dP8sEGxCABhjSkBsJny3hc5HroskBiiGe'
  34     b'gBvHsMPQf9bBT0P27SFbfAbcBMYBM50GzGcqc4JOTYtHHTqIBfvADf2tMN63AP5/nYL5aX0L'
  35     b'Zrx4W1Te7cZ72rKuSfh+IAaQtj3w900r8rW49rPGvbObt740CyqQA7YHhq6AoxXEGgPxWB7K'
  36     b'orQXXEv4chg8D71nmR2Dtg8dieOpZBvwmZwAINJm4B92VpSrKeXt7c+Fdu74Sgh0IBvsTBj+'
  37     b'BNw9sKzJCVuKH1CZvAXwpWA6oPsn0HESMwJ95wzembF6R2zRJeFldSAtBg79QUWBaitvPfHl'
  38     b'4M5tL4RAmeexnND/EeQNQ9F2F2wqAWVhTXKQBuSUQsgHl9+A0U6iAbj9C4Nzner4iWSyBWiT'
  39     b'1YkDpMXAoc9XLSOhnGx6PvDEthdDIOaZUg4YuABFo5C/RcIjockHIeGVAmmiCLyt8/DhENND'
  40     b'Ct3vO8V/9YmRH8Xjl4BWmb4fEGkx8De7qstIKe827Q9ubXo+CGKeMWVAfzOU+CG30Qkbli04'
  41     b'1wvAcsOtK5AQ0PASGC747C3ofg9zTjDQqtN53kgd96W6z8RTLcB14ArgBcy0vJH99VMrl+uW'
  42     b'8t9PvTjTsGVfENR5xriAwQtQkQD34xmwugRELtjZoJVB3IK2j8A3CSbgKgQyYWaE2QnoOJvB'
  43     b'r7oU35HZ+PV7pt0hk28DxoBEWt6J/2p3zXIs/nP3H003NO4NgS3v9hYMnYcKE9xbMqFOJt8/'
  44     b'CoF+KInDSD/4pxlp0wlPKdRvnkbXYaRDo/PDDHFiyOw9EY61mII2Cd8H+Bcn/1tX6LV9tZV2'
  45     b'Ujn9hf0zGxt3y84XQDgBIxehUgH3JhfUlcz3vM8LEz5IAoGrkIKeXxl8+kFWMhrFDIzGMjPd'
  46     b'Nm2fOuM/9cXbL0WT14FrEn4EiC3u/G9doYNP16/WFfudPftn1m3fEwANyIfZCEx+CisMyNjo'
  47     b'gYplkDCgbxSCIe616oz3OKjbFGPwZgbnPnQFj47PXRtLWgNfK8hc71aUvJPB2K3hpNUt+94O'
  48     b'TAKptH0X+u4X66s1VZx+9ku+9Zs+J5MvgmAAxi9DtQdcFRqUecBywVgIwjGGr+qceycnMeZX'
  49     b'Ig2FIq/DZ/le94Y+Dlvihux2BCgBHMAgcBcIAhZAWgx8d399vbCV95550bdq25MBsIFC8M+A'
  50     b'rwVWFoBR4YCkCXEBKcCEnotOms/kJI8OzrbeDMbb13oMx0A0FYhaYgi4KvudAtyACkQWViYN'
  51     b'BubhFXhv74HpVTt2+0GZhw/OwNRVqC4EvSELDAf0B7j7iYGvTycRU7nanhk9OhC62juXaAFa'
  52     b'gHsSLgRMAFHSoF97Ch38Yv0qTVU+ePGl8ap122ZBwk/eg7lOWFkG2hoPFJaCN4A1B5cv5tsf'
  53     b'dCSHA0nT/1nA3zsZN7tk2h1AAACw5aRN+gPg1yqKcvoFCQ/z8BPDELkNK8tBrfdAQT6ETAhG'
  54     b'iIRUBn2Efz4SuGwJ0S77PAwMAbOLoR+agYMH6uo0ldP7np2sWS/hKYbxAYj1zsMrHhXmkjAx'
  55     b'ClETTAhNuZiO2OH78EPAZaBHdjoJCIBHYiCVUnbVrI7VbH1C3mHzYLQXbC+srJPfaiIWCW+C'
  56     b'0ISC3+dgZsLAO5TJaDjpB3zAOBBckr/QJE1RkJ1joqiAB0buAGNQUaNAuRumYkSmFc4cK2Rg'
  57     b'1EhMzNnhqYgZHAjFRtumI63AIBAGWBIDwlZy3R4TnDDUCVoUyuuAqhxIaRCZo68ri1Pt2siZ'
  58     b'3rErKVt4gUlgBhgB+oDZJTOgqSLf4zHx9YMRh7KVQEkWZGZDzxSpabja4rGujQe678O3ANeA'
  59     b'Cdn3sFzNJTPgdInCnIIk7mxwVwIpYDIK3hiYFn09blr6lGlvKH4HaAPa03Wmp8WAQ6fIFUlh'
  60     b'TcDMkMzSslEswILW1mxxfWy2FxgAvDJx/t8YiCXsnlOnlq1NpixNgIoUCHRUtXvSnrrlC7cD'
  61     b'd4ApQLDEWvwoUQxsBxqAzEWAiixV34K3JIsl1IOehYwF74AaiwU2MIv8/gyw9AZ+/98qS6v/'
  62     b'AUoSr1gPNRy7AAAAAElFTkSuQmCC')
  63 
  64 #---------------------------------------------------------------------------
  65 
  66 class MyFrame(wx.Frame):
  67     def __init__(self):
  68         wx.Frame.__init__(self, None, title=wx.GetApp().GetAppName())
  69         self.SetIcon(wx.Icon('wxwin.ico'))
  70 
  71         self._createControls()
  72 
  73         self.CenterOnScreen()
  74 
  75     def _createControls(self):
  76         # A Statusbar in the bottom of the window
  77         self.CreateStatusBar(1)
  78         sMsg = 'wxPython ' + wx.version() + ' - ' + wx.GetLibraryVersionInfo().VersionString
  79         self.SetStatusText(sMsg)
  80 
  81         # Add a panel to the frame (needed under Windows to have a nice background)
  82         pnl = wx.Panel(self, wx.ID_ANY)
  83 
  84         szrMain = wx.BoxSizer(wx.VERTICAL)
  85         szrMain.AddSpacer(15)
  86         label = wx.StaticText(pnl, wx.ID_STATIC, 'Well done!.\nYou have successfully entered the required password.')
  87         szrMain.Add(label, 0, wx.ALL, 15)
  88         szrMain.AddSpacer(15)
  89 
  90         pnl.SetSizer(szrMain)
  91 
  92         szrMain.SetSizeHints(self)
  93 
  94 #---------------------------------------------------------------------------
  95 
  96 class MyDlgLogin(wx.Dialog):
  97     def __init__(self, parent, password, maxAttemps):
  98         wx.Dialog.__init__(self, parent, title='Restricted access')
  99 
 100         self._iMaxAttemps = maxAttemps
 101         self._iAttemps = 0
 102         self._sPassword = password
 103 
 104         self._createInterface()
 105         self._txtPwd.SetFocus()
 106 
 107         self._connectControls()
 108 
 109         self.CenterOnParent()
 110 
 111     def _createInterface(self):
 112         szrMain = wx.BoxSizer(wx.VERTICAL)
 113 
 114         lnszr = wx.BoxSizer(wx.HORIZONTAL)
 115         bmpCtrl = wx.StaticBitmap(self, wx.ID_STATIC, Key_Png.GetBitmap())
 116         lnszr.Add(bmpCtrl, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 10)
 117         label = wx.StaticText(self, wx.ID_STATIC, 'Access to this application has been\nprotected with a password.')
 118         lnszr.Add(label, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 10)
 119         szrMain.Add(lnszr, 0, wx.ALL, 0)
 120 
 121         label = wx.StaticText(self, wx.ID_STATIC, 'For this test application, the password is "' + self._sPassword + '"')
 122         szrMain.Add(label, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM, 10)
 123 
 124         self._lblWrongPwd = wx.StaticText(self, wx.ID_STATIC, 'Incorrect password!')
 125         self._lblWrongPwd.SetFont(label.GetFont().MakeBold())
 126         self._lblWrongPwd.SetForegroundColour(wx.RED)
 127         szrMain.Add(self._lblWrongPwd, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_HORIZONTAL, 10)
 128 
 129         lnszr = wx.BoxSizer(wx.HORIZONTAL)
 130         label = wx.StaticText(self, wx.ID_STATIC, 'Enter the access password:')
 131         lnszr.Add(label, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 0)
 132         self._txtPwd = wx.TextCtrl(self, wx.ID_ANY, '', style=wx.TE_PASSWORD)
 133         lnszr.Add(self._txtPwd, 1, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 5)
 134         szrMain.Add(lnszr, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.EXPAND, 10)
 135 
 136         self._chkShowPwd = wx.CheckBox(self, wx.ID_ANY, 'Display password in plain text')
 137         szrMain.Add(self._chkShowPwd, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM, 10)
 138 
 139         btnSzr = self.CreateSeparatedButtonSizer(wx.OK|wx.CANCEL)
 140         btnOk = self.FindWindowById(wx.ID_OK)
 141         if not btnOk is None:
 142             btnOk.SetLabel('Access')
 143             btnOk.SetDefault()
 144         szrMain.Add(btnSzr, 0, wx.ALL|wx.EXPAND, 10)
 145 
 146         self.SetSizer(szrMain)
 147 
 148         self._lblWrongPwd.Hide()
 149 
 150         szrMain.SetSizeHints(self)
 151 
 152     def _connectControls(self):
 153         self._chkShowPwd.Bind(wx.EVT_CHECKBOX, self._onChkShowPasswordClicked)
 154         self.Bind(wx.EVT_BUTTON, self._onBtnOkClicked, id=wx.ID_OK)
 155 
 156     def _onChkShowPasswordClicked(self, evt):
 157         self.Freeze()
 158 
 159         sPass = self._txtPwd.GetValue()
 160         lnszr = self._txtPwd.GetContainingSizer()
 161         iStyle = 0
 162         if not self._chkShowPwd.IsChecked():
 163             iStyle = wx.TE_PASSWORD
 164 
 165         lnszr.Detach(self._txtPwd)
 166         self._txtPwd.Destroy()
 167         self._txtPwd = wx.TextCtrl(self, wx.ID_ANY, sPass, style=iStyle)
 168         lnszr.Add(self._txtPwd, 1, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 5)
 169 
 170         lnszr.Layout()
 171 
 172         self.Thaw()
 173 
 174     def _onBtnOkClicked(self, evt):
 175         iRes = wx.ID_OK
 176         if self._txtPwd.GetValue() != self._sPassword:
 177             iRes = wx.ID_CANCEL
 178 
 179         if (iRes == wx.ID_OK) or (self._iAttemps == (self._iMaxAttemps - 1)):
 180             self.EndModal(iRes)
 181 
 182         if self._iAttemps == 0:
 183             self._lblWrongPwd.Show()
 184             self.GetSizer().SetSizeHints(self)
 185             self.CenterOnParent()
 186 
 187         self._txtPwd.SetFocus()
 188         self._txtPwd.SetSelection(-1, -1)
 189 
 190         self._iAttemps += 1
 191         self._lblWrongPwd.SetLabel('Incorrect password: trial {:0d}/{:0d}'.format(self._iAttemps+1, self._iMaxAttemps))
 192         self.GetSizer().Layout()
 193 
 194 #---------------------------------------------------------------------------
 195 
 196 class MyApp(wx.App):
 197     def OnInit(self):
 198         print('Running wxPython ' + wx.version())
 199 
 200         self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
 201 
 202         # Set Current directory to the one containing this file
 203         os.chdir(os.path.dirname(os.path.abspath(__file__)))
 204 
 205         self.SetAppName('PwdProtect')
 206 
 207         dlg = MyDlgLogin(None, 'wxWidgets', 3)
 208         if dlg.ShowModal() != wx.ID_OK:
 209             wx.MessageBox('You can not access to this application!', 'Password required', wx.ICON_EXCLAMATION|wx.OK|wx.CENTER)
 210             return False
 211         dlg.Destroy()
 212 
 213         # Create the main window
 214         frm = MyFrame()
 215         self.SetTopWindow(frm)
 216 
 217         frm.Show()
 218         return True
 219 
 220 #---------------------------------------------------------------------------
 221 
 222 if __name__ == '__main__':
 223     app = MyApp()
 224     app.MainLoop()


Second example

img_sample_two.png

ICON file : wxwin.ico

   1 # sample_two.py
   2 
   3 """
   4 
   5 https://ideal-user-interface.hatenablog.com/entry/20110929/1317254204
   6 
   7 """
   8 
   9 import os
  10 import sys
  11 import wx
  12 
  13 # class MyLoginDialog
  14 # class MyFrame
  15 # class MyApp
  16 
  17 #---------------------------------------------------------------------------
  18 
  19 class MyLoginDialog(wx.Dialog):
  20     def __init__(self, *args, **kwargs):
  21         super(MyLoginDialog, self).__init__(*args, **kwargs)
  22 
  23         #------------
  24 
  25         # Return icons folder.
  26         self.icons_dir = wx.GetApp().GetIconsDir()
  27 
  28         #------------
  29 
  30         # Simplified init method.
  31         self.SetProperties()
  32         self.CreateCtrls()
  33         self.DoLayout()
  34 
  35     #-----------------------------------------------------------------------
  36 
  37     def SetProperties(self):
  38         """
  39         ...
  40         """
  41 
  42         frameIcon = wx.Icon(os.path.join(self.icons_dir,
  43                                          "wxwin.ico"),
  44                             type=wx.BITMAP_TYPE_ICO)
  45         self.SetIcon(frameIcon)
  46 
  47 
  48     def CreateCtrls(self):
  49         """
  50         ...
  51         """
  52 
  53         font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
  54         font.SetWeight(wx.BOLD)
  55 
  56         #------------
  57 
  58         self.pnl = wx.Panel(self)
  59 
  60         #------------
  61 
  62         self.username = wx.TextCtrl(self.pnl)
  63         self.passwd = wx.TextCtrl(self.pnl)
  64 
  65         self.userTxt = wx.StaticText(self.pnl, label="Username :")
  66         self.userTxt.SetFont(font)
  67         self.pwdTxt = wx.StaticText(self.pnl, label="Password :")
  68         self.pwdTxt.SetFont(font)
  69 
  70         #------------
  71 
  72         self.btnOk = wx.Button(self.pnl, wx.ID_OK)
  73         self.btnOk.SetDefault()
  74 
  75 
  76     def DoLayout(self):
  77         """
  78         ...
  79         """
  80 
  81         sizer = wx.FlexGridSizer(2, 2, 8, 8)
  82         sizer.Add(self.userTxt,
  83                   0, wx.ALIGN_CENTER_VERTICAL)
  84         sizer.Add(self.username, 0, wx.EXPAND)
  85         sizer.Add(self.pwdTxt,
  86                   0, wx.ALIGN_CENTER_VERTICAL)
  87         sizer.Add(self.passwd, 0, wx.EXPAND)
  88 
  89         #------------
  90 
  91         msizer = wx.BoxSizer(wx.VERTICAL)
  92         msizer.Add(sizer, 1, wx.EXPAND|wx.ALL, 20)
  93 
  94         btnszr = wx.StdDialogButtonSizer()
  95         btnszr.AddButton(self.btnOk)
  96         msizer.Add(btnszr, 0, wx.ALIGN_CENTER|wx.ALL, 12)
  97         btnszr.Realize()
  98         self.pnl.SetSizer(msizer)
  99 
 100         #------------
 101 
 102         vsizer = wx.BoxSizer(wx.VERTICAL)
 103         vsizer.Add(self.pnl, 1, wx.EXPAND)
 104         self.SetSizer(vsizer)
 105         self.SetInitialSize()
 106 
 107 
 108     def GetUser(self):
 109         """
 110         ...
 111         """
 112 
 113         return self.username.GetValue()
 114 
 115 
 116     def GetPasswd(self):
 117         """
 118         ...
 119         """
 120 
 121         return self.passwd.GetValue()
 122 
 123 #---------------------------------------------------------------------------
 124 
 125 class MyFrame(wx.Dialog):
 126     def __init__(self, parent, id, title):
 127         wx.Dialog.__init__(self, parent, id, title)
 128 
 129         #------------
 130 
 131         # Return icons folder.
 132         self.icons_dir = wx.GetApp().GetIconsDir()
 133 
 134         #------------
 135 
 136         # Simplified init method.
 137         self.SetProperties()
 138 
 139     #-----------------------------------------------------------------------
 140 
 141     def SetProperties(self):
 142         """
 143         ...
 144         """
 145 
 146         self.SetMinSize((420, 200))
 147         self.SetBackgroundColour(wx.Colour("#eceade"))
 148 
 149         #------------
 150 
 151         frameIcon = wx.Icon(os.path.join(self.icons_dir,
 152                                          "wxwin.ico"),
 153                             type=wx.BITMAP_TYPE_ICO)
 154         self.SetIcon(frameIcon)
 155 
 156 #---------------------------------------------------------------------------
 157 
 158 class MyApp(wx.App):
 159     def OnInit(self):
 160 
 161         #------------
 162 
 163         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 164 
 165         #------------
 166 
 167         self.frm = MyFrame(None, -1, 'Sample two')
 168 
 169         login = MyLoginDialog(None)
 170         loggedIn = False
 171         while not loggedIn:
 172             dlg = login.ShowModal()
 173             if dlg == wx.ID_OK:
 174                 uname = login.GetUser()
 175                 passwd = login.GetPasswd()
 176                 if (uname, passwd) == ("hoge", "hoge"):
 177                     loggedIn = True
 178                 else:
 179                     print("Username or password is incorrect !")
 180 
 181         self.frm.Show(True)
 182 
 183         return True
 184 
 185     #---------------------------------------------------------------------------
 186 
 187     def GetInstallDir(self):
 188         """
 189         Returns the installation directory for my application.
 190         """
 191 
 192         return self.installDir
 193 
 194 
 195     def GetIconsDir(self):
 196         """
 197         Returns the icons directory for my application.
 198         """
 199 
 200         icons_dir = os.path.join(self.installDir, "icons")
 201         return icons_dir
 202 
 203 #-------------------------------------------------------------------------------
 204 
 205 def main():
 206     app = MyApp(False)
 207     app.MainLoop()
 208 
 209 #-------------------------------------------------------------------------------
 210 
 211 if __name__ == "__main__" :
 212     main()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Xaviou (sample_one.py coding), Hatenablog (sample_two.py coding), the wxPython community...


About this page

Date (d/m/y) Person (bot) Comments :

25/05/18 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

Create a customized password dialog (Phoenix) (last edited 2020-10-07 09:23:37 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.