After making a code that generates random passwords in the previous article, it is time to write one that checks if they meet certain conditions, so what we are going to do in this article is write a code about how to make a password condition checker using Python.
Let’s get started!
Table of Contents
- Necessary Libraries
- Imports
- Password Conditions Function
- Password Visibility Function
- Create the Main Window
- Create a Label and an Entry Box For the Password
- Create a Show Password Button
- Create a Check Condition Button
- Display Results
- Starting the Event Loop
- Example
- Full Code
Necessary Libraries
To ensure this code operates correctly, make sure to install the tkinter library. This library allows us to create a graphical user interface (GUI).
$ pip install tk
Import
As a starter let us import everything *
from the tkinter
library.
from tkinter import *
Password Conditions Function
Once the tkinter
library is imported, we begin by defining a function. This function will retrieve the password from an entry box, which we will create later. It then checks whether the password adheres to the conditions that we are about to set. But first, let’s define the function.
def verify_pass_cond():
password = entry.get()
Now that we defined it we need to create the conditions:
conditions = [
lambda p: len(p) >= 8,
lambda p: sum(c.isdigit() for c in p) >= 1,
lambda p: sum(c.isalpha() for c in p) >= 2,
lambda p: any(not c.isalnum() for c in p),
lambda p: any(c.isupper() for c in p)
]
What we did here is set five conditions using the lambda
function which allows us to create a small function on the fly, in this code the lambda
function is going to take the password (p
) as a parameter and return a boolean value to show whether the condition is met or not for the example:
- The first condition dictates that the length of the password
>=8
. - the second dictates that the password contains at least one digit.
- the third dictates that the password contains at least two letters.
- the fourth dictates that the password contains at least one special character.
- the last dictates that the password contains at least one capital letter.
Now we need to put the messages that will appear once the conditions aren’t met. Basically what this does is create a list of messages that mention the condition the password failed to meet.
messages = [
"password should have at least 8 characters.",
"password should contain at least one digit.",
"password should contain at least two letters.",
"password should contain at least one special character.",
"password should contain at least one capital letter"
]
errors =[msg for condition, msg in zip(conditions, messages) if not condition(password)]
if not errors:
result.config(text="password meets all conditions.", fg="green")
else:
result.config(text="\n".join(errors), fg="red")
These last lines check the password against each condition creating a list of error messages (msg
) in red for each condition that is not met. Otherwise, when all the conditions are met a text in green appears (password meets all conditions).
Password Visibility Function
Now this part is optional. Here we are going to add the option of making the password visible or not.
def make_password_visible():
if appear_password.get():
entry.config(show="")
else:
entry.config(show="*")
The function presented here is for a checkbox, when the checkbox is checked the password is shown otherwise a *
appears.
Create the Main Window
root = Tk()
root.title("password condition checker - The pycodes")
root.geometry("300x200")
Here, we created the main window which serves as the container for other widgets, and gave it a title 300
pixels in width and 200
pixels in height.
Create a Label and an Entry Box For the Password
label = Label(root, text="Enter your password:")
label.grid(row=0, column=3)
entry = Entry(root, show="*", width=30)
entry.grid(row=2, column=3, ipadx=30, padx=30)
Then we create a label text that says (Enter your password) along with an entry box where we put the password.
Create a Show Password Button
appear_password = BooleanVar()
appear_password_button = Checkbutton(root, text="show password", variable=appear_password, command=make_password_visible)
appear_password_button.grid(row=3, column=3)
In this part, we add a check button to the main window and with the password visibility function command, we can choose to make the password visible or not.
Create a Check Condition Button
button = Button(root, text="Check conditions", command=verify_pass_cond)
button.grid(row=4, column=3, columnspan=2)
After that, we add a “Check Condition” button widget to the main window. Once pressed, it calls the verify_pass_cond
function to check if the password meets the specified conditions.
Display Results
result = Label(root, text="")
result.grid(row=5,column=3)
Next, we add a label widget that displays the results of the password conditions in the main window.
Starting the Event Loop
root.mainloop()
Starts an event loop and keeps the main window running.
Example
Full Code
from tkinter import *
def verify_pass_cond():
password = entry.get()
#Creating the password conditions
conditions = [
lambda p: len(p) >= 8,
lambda p: sum(c.isdigit() for c in p) >= 1,
lambda p: sum(c.isalpha() for c in p) >= 2,
lambda p: any(not c.isalnum() for c in p),
lambda p: any(c.isupper() for c in p)
]
#Creating the messages
messages = [
"password should have at least 8 characters.",
"password should contain at least one digit.",
"password should contain at least two letters.",
"password should contain at least one special character.",
"password should contain at least one capital letter"
]
errors =[msg for condition, msg in zip(conditions, messages) if not condition(password)]
if not errors:
result.config(text="password meets all conditions.", fg="green")
else:
result.config(text="\n".join(errors), fg="red")
#Creating password visibility function
def make_password_visible():
if appear_password.get():
entry.config(show="")
else:
entry.config(show="*")
#Create the main window
root = Tk()
root.title("password condition checker - The pycodes")
root.geometry("300x200")
#Create a label and an entry for the password
label = Label(root, text="Enter your password:")
label.grid(row=0, column=3)
entry = Entry(root, show="*", width=30)
entry.grid(row=2, column=3, ipadx=30, padx=30)
#Create a checkbutton to render password visible
appear_password = BooleanVar()
appear_password_button = Checkbutton(root, text="show password", variable=appear_password, command=make_password_visible)
appear_password_button.grid(row=3, column=3)
#Create a check condition button
button = Button(root, text="Check conditions", command=verify_pass_cond)
button.grid(row=4, column=3, columnspan=2)
#Display result
result = Label(root, text="")
result.grid(row=5,column=3)
#Starting the event loop
root.mainloop()
Happy Coding!