Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import random
- class MainWindow(tk.Frame):
- def __init__(self, master=None):
- #master constructor
- super().__init__(master)
- self.grid(columnspan=2)
- #call the creation of all UI items used
- self.create_widgets()
- def create_widgets(self):
- #Widget creation, all GUI related items defined here
- #Banner
- self.banner = tk.Label(self, text="WELCOME TO THE BANKING SYSTEM")
- self.banner.grid(row=0, columnspan=2, padx=5, pady=10)
- #Buttons
- self.loginBtn = tk.Button(self, text="Login with existing account", command=self.loginForm)
- self.loginBtn.grid(row=1, column=0, padx=5)
- self.createBtn = tk.Button(self, text="Setup an account", command=self.registerForm)
- self.createBtn.grid(row=1, column=1, padx=5)
- self.quitBtn = tk.Button(self, text="QUIT", fg="red", command=root.destroy)
- self.quitBtn.grid(row=2, column=0, columnspan=2, pady=20)
- #user widgets
- self.usernameLabel = tk.Label(self, text="Username:")
- self.usernameEntry = tk.Entry(self)
- self.passwordLabel = tk.Label(self, text="Password:")
- self.passwordEntry = tk.Entry(self, show="*")
- self.hiddenPhraseLabel = tk.Label(self, text="Hidden phrase:")
- self.hiddenPhraseEntry = tk.Entry(self)
- #
- self.doLoginBtn = tk.Button(self, text="Login", command=self.login)
- self.doRegisterBtn = tk.Button(self, text="Register", command=self.register)
- self.warningLabel = tk.Label(self, fg="red")
- self.confirmPhraseLabel = tk.Label(self, fg="blue")
- self.confirmPhraseBtn = tk.Button(self, text="Confirm phrase")
- def login(self):
- #clear previous warning messages (if any)
- self.clearWarnings()
- #get input values from the username and password fields
- username = self.usernameEntry.get()
- password = self.passwordEntry.get()
- #check if user exists in the text file
- userData = self.findUserData(username)
- if userData:
- #Check if given password matches the stored password
- if userData[1]==password:
- #Show the hidden phrase challenge (last step of login)
- self.hiddenPhraseLabel.grid(row=5, column=0, padx=10)
- self.hiddenPhraseEntry.grid(row=5, column=1, padx=10)
- self.doLoginBtn.grid_forget()
- #Get the random positions to ask the user from the phrase
- challengeIndexes = self.getPhraseChallenge()
- challengeText = "Enter the characters number\n" + str(challengeIndexes[0]+1)
- challengeText += ", " + str(challengeIndexes[1]+1) + " and " + str(challengeIndexes[2]+1)
- challengeText += " from your hidden phrase.\n (comma separated)"
- #show the hidden phrase challenge
- self.confirmPhraseLabel["text"]=challengeText
- self.confirmPhraseLabel.grid(row=8, column=0)
- self.confirmPhraseBtn["command"] = lambda: self.confirmPhrase(userData[2], challengeIndexes)
- self.confirmPhraseBtn.grid(row=8, column=1)
- else:
- #If the password doesn't match, show an alert
- self.warningLabel["text"]="The password is incorrect"
- self.warningLabel.grid(row=7, columnspan=2)
- else:
- #If the user does not exists in the file, show an alert
- self.warningLabel["text"]="Username not found"
- self.warningLabel.grid(row=7, columnspan=2)
- def confirmPhrase(self, hiddenPhrase, indexes):
- #Get the text from user input
- confirmText = self.hiddenPhraseEntry.get()
- #Construct how the user input should look using the random indexes
- challengeText = hiddenPhrase[indexes[0]] + "," + hiddenPhrase[indexes[1]] + "," + hiddenPhrase[indexes[2]]
- if confirmText == challengeText:
- #If the texts match, complete login
- self.cleanWidgets()
- self.warningLabel["text"]="Login Successful!\nWelcome"
- self.warningLabel.grid(row=9, columnspan=2)
- else:
- #If the text does not match, show an alert
- self.warningLabel["text"]="Hidden phrase challenge failed"
- self.warningLabel.grid(row=9, columnspan=2)
- #Check if the user is in the file and return the data in a list
- def findUserData(self, username):
- try:
- #open the local file in reading mode
- users = open("users.txt", "r")
- #read all the lines of the text and put them in a list
- lines = users.readlines()
- #Check each line
- for line in lines:
- #The user data is separated by ; so split the text and check if the username matches the first field in the line
- data = line.split(";")
- if data[0]==username:
- #If found, return the data
- return data
- return None
- except FileNotFoundError:
- #if the file does not exists, no user is registered yet
- return None
- #generates 3 random positions from 0 to 9 to ask the user for the hidden phrase challenge
- def getPhraseChallenge(self):
- #create an empty list
- indexes = []
- #3 times loop
- for i in range(1,4):
- #generate random number
- num = random.randint(0,10)
- #if the number is already in the list, search for a new one that isn't
- while num in indexes:
- num = random.randint(0,10)
- #add the number to the list
- indexes.append(num)
- #return the list
- return indexes
- def register(self):
- #clear any previous warnings if any
- self.clearWarnings()
- #get the username, password and hidden phrase input fields
- username = self.usernameEntry.get()
- password = self.passwordEntry.get()
- hiddenPhrase = self.hiddenPhraseEntry.get()
- #Check if the hidden phrase is 10 characters long and alphanumeric
- if hiddenPhrase.isalnum() and len(hiddenPhrase) == 10:
- #Check that the username isn't already been registered
- if not self.findUserData(username):
- #Open the local file in append mode (to write new data at the end of the file)
- users = open("users.txt", "a+")
- #creates the structure of the saved data (username;password;hiddenphrase)
- data = username + ";" + password + ";" + hiddenPhrase + "\n"
- #write the data to the file and close it
- users.write(data)
- users.close()
- #Clean the form and show a confirmation message
- self.cleanWidgets()
- self.warningLabel["text"]="Account setup successful"
- self.warningLabel.grid(row=7, columnspan=2)
- else:
- #Show an alert for a duplicate username entry
- self.warningLabel["text"]="Username already exists"
- self.warningLabel.grid(row=7, columnspan=2)
- else:
- #Show an alert stating the hidden phrase constraints
- self.warningLabel["text"]="The hidden phrase needs to be alphanumeric\n with no spaces and 10 characters long"
- self.warningLabel.grid(row=7, columnspan=2)
- def registerForm(self):
- self.cleanWidgets()
- #Display the username, password and hidden phrase input fields and labels for the register form
- self.usernameLabel.grid(row=3, column=0, padx=10)
- self.usernameEntry.grid(row=3, column=1, padx=10)
- self.passwordLabel.grid(row=4, column=0, padx=10)
- self.passwordEntry.grid(row=4, column=1, padx=10)
- self.hiddenPhraseLabel.grid(row=5, column=0, padx=10)
- self.hiddenPhraseEntry.grid(row=5, column=1, padx=10)
- #display the register button
- self.doRegisterBtn.grid(row=6, columnspan=2, pady=10)
- def loginForm(self):
- self.cleanWidgets()
- #Display the username and password inut fields for the login form
- self.usernameLabel.grid(row=3, column=0, padx=10)
- self.usernameEntry.grid(row=3, column=1, padx=10)
- self.passwordLabel.grid(row=4, column=0, padx=10)
- self.passwordEntry.grid(row=4, column=1, padx=10)
- #Display the login button
- self.doLoginBtn.grid(row=6, columnspan=2, pady=10)
- def cleanWidgets(self):
- #Hide all the login/register related items (if present)
- self.usernameLabel.grid_forget()
- self.usernameEntry.grid_forget()
- self.passwordLabel.grid_forget()
- self.passwordEntry.grid_forget()
- self.hiddenPhraseLabel.grid_forget()
- self.hiddenPhraseEntry.grid_forget()
- self.doRegisterBtn.grid_forget()
- self.doLoginBtn.grid_forget()
- self.warningLabel.grid_forget()
- self.confirmPhraseLabel.grid_forget()
- self.confirmPhraseBtn.grid_forget()
- def clearWarnings(self):
- #hide all the warning/messages in the UI (if any)
- self.warningLabel.grid_forget()
- self.confirmPhraseLabel.grid_forget()
- self.confirmPhraseBtn.grid_forget()
- #Creates a root Frame from Tkinter
- root = tk.Tk()
- #Set the frame title
- root.title("WELCOME")
- #Creates a new window with the root frame
- app = MainWindow(master=root)
- #start/display the window
- app.mainloop()
Add Comment
Please, Sign In to add comment