I recently wrote a script to perform dynamic password strength check and @martineau showed that I could us functools.partial
to supply a tk.StringVar
argument to my static method _passwordStrength()
. However, when I apply this solution to the solution by @BryanOakley on switching between multiple frames/pages, somehow my dynamic strength check code fails. My Test script and the error msg that I get are given below. I don't understand why my script is telling python that 'Login' has attribute '_LoginPage__passwordStrength'. It should be searching for static method __passwordStrength
in class Login
and not class LoginPage
. Somehow, I am not able to register
the @staticmethod
.
Appreciate guidance on fixing this AttributeError.
Test Script:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# python3 modules
import re
import functools
# tkinter modules
import tkinter as tk
import tkinter.ttk as ttk
class Login(ttk.Frame):
def __init__(self, parent=None, *args, **options):
"""
"""
# Initialise App Frame
ttk.Frame.__init__(self, parent)
self.parent = parent
self.__createPages()
self.parent.rowconfigure(0, weight=1)
self.parent.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
def __createPages(self):
"""Create Login and NewUser pages."""
self._pages = {}
for F in (LoginPage, NewUserPage):
#for F in (LoginPage, ):
page_name = F.__name__
page = F(parent=self)
self._pages[page_name] = page
page.grid(row=0, column=0, sticky="nsew")
self.showPage("LoginPage")
self.parent.rowconfigure(0, weight=1)
self.parent.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
def showPage(self, page_name):
'''Show the desired page. '''
#1. Bring to foreground the desired page.
page = self._pages[page_name]
page.tkraise()
#2. Always want window to resize to fit new page.
top = page.winfo_toplevel()
top.geometry('')
@staticmethod
def __passwordStrength(svar, P):
'''Check password strength.
A password is considered strong if:
8 characters length or more
1 digit or more
1 symbol or more
1 uppercase letter or more
1 lowercase letter or more'''
password = P
print (password, len(password))
# check length
if password == '':
svar.set('')
return True
# check length
if len(password) < 8:
svar.set('Password is too short')
return True
# check for digits
if not re.search(r"\d", password):
svar.set('Password missing a number.')
return True
# check for uppercase
if not re.search(r"[A-Z]", password):
svar.set('Password missing upper case letter.')
return True
# check for lowercase
if not re.search(r"[a-z]", password):
svar.set('Password missing lower case letter.')
return True
# check for symbols
if not re.search(r"\W", password):
svar.set('Password missing a symbol.')
return True
# Passed all checks.
svar.set('Strong password provided.')
return True
class LoginPage(ttk.Frame):
def __init__(self, parent):
ttk.Frame.__init__(self, parent)
self.parent=parent
self.pwstrength = tk.StringVar()
pwLabel = ttk.Label(self, text='Password:')
valcommand = self.register(
functools.partial(Login.__passwordStrength, self.pwstrength)
)
vcmd = (valcommand, '%P')
pwEntry = ttk.Entry(self, width=15, show='*', validate="key",
validatecommand=vcmd)
#pwEntry = ttk.Entry(self, width=15, show='*') #This works
pwWarn = ttk.Label(self, textvariable=self.pwstrength)
switchpage = ttk.Button(self, text='Create New User?',
command=lambda:parent.showPage("NewUserPage"))
pwLabel.grid(row=1, column=0, sticky='nse', padx=10)
pwEntry.grid(row=1, column=1, sticky='nsw', padx=10)
pwWarn.grid( row=2, column=1, sticky='nsw', padx=10)
switchpage.grid(row=3, column=0, sticky='nsew', columnspan=2, ipady=10)
class NewUserPage(ttk.Frame):
def __init__(self, parent):
ttk.Frame.__init__(self, parent)
self.pwstrength = tk.StringVar()
pwLabel = ttk.Label(self, text='Password:')
pwEntry = ttk.Entry(self, width=15, show='*')
pwWarn = ttk.Label(self, textvariable=self.pwstrength)
switchpage = ttk.Button(self, text='Login?',
command=lambda:parent.showPage("LoginPage"))
pwLabel.grid(row=1, column=0, sticky='nse', padx=10)
pwEntry.grid(row=1, column=1, sticky='nsw', padx=10)
pwWarn.grid( row=2, column=1, sticky='nsw', padx=10)
switchpage.grid(row=3, column=0, sticky='nsew', columnspan=2, ipady=10)
if __name__ == '__main__':
root = tk.Tk()
root.geometry('400x350+300+300')
login = Login(root)
login.grid(row=0, column=0, sticky='nsew')
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.mainloop()
Error Msg:
Traceback (most recent call last):
File "~/login_v4.py", line 152, in <module>
login = Login(root)
File "~/login_v4.py", line 22, in __init__
self.__createPages()
File "~/login_v4.py", line 35, in __createPages
page = F(parent=self)
File "~/login_v4.py", line 113, in __init__
functools.partial(Login.__passwordStrength, self.pwstrength)
AttributeError: type object 'Login' has no attribute '_LoginPage__passwordStrength'
__
. They will be name mangled. – Klaus D. Mar 16 '18 at 4:58_passwordStrength
to__passwordStrength
which causes name-mangling to occur. See link in @KlausD's comment. – martineau Mar 16 '18 at 7:38