Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk # Creates a Tkinter window so the code runs in it's own window and not the Python shell.
- import random # Randomises integers.
- class MainWindow(tk.Frame):
- def __init__(self, master=None):
- # Master constructor (constucts the Tkinter window).
- super().__init__(master)
- self.grid(columnspan=2)
- # Calls the creation of all UI items used.
- self.create_widgets() # Calls the function.
- def create_widgets(self): # Window creation, all GUI related items defined here
- # Banner
- self.banner = tk.Label(self, text="WELCOME TO OUR BANKING SYSTEM") # Welcoming text.
- self.banner.grid(row=0, columnspan=2, padx=5, pady=10) # Location of the welcoming text (at the top of the window).
- # Buttons
- self.loginBtn = tk.Button(self, text="Login with existing account", command=self.loginForm) # Buttom that allows you to log in.
- self.loginBtn.grid(row=1, column=0, padx=5) # Location of the text. In this case it will be in row 1, column 1 (on the left).
- self.createBtn = tk.Button(self, text="Setup an account", command=self.registerForm) # Button that allows you to create a profile.
- self.createBtn.grid(row=1, column=1, padx=5) # Location of the text. In this case it will be in row 1, column 2 (on the right).
- self.quitBtn = tk.Button(self, text="QUIT", fg="red", command=root.destroy) # Quit button that quits the code. Also makes the letters red.
- self.quitBtn.grid(row=2, column=0, columnspan=2, pady=20) # Location of the text. In this case it will be in row 2 (in the middle).
- # User widgets for seting up an account.
- 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)
- # User widgets for logging in with an existing one.
- 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): # Function for the loggin phase.
- self.clearWarnings() # Clears any previous warnings (if any)
- username = self.usernameEntry.get() # Gets input value from the username field.
- password = self.passwordEntry.get() # Gets input value from the password field.
- userData = self.findUserData(username) # Checks if the username and password exist
- if userData:
- # Checks if given password matches the stored password.
- if userData[1]==password:
- # Shows the hidden phrase challenge (last step of login).
- self.hiddenPhraseLabel.grid(row=5, column=0, padx=10) # Location of the text.
- self.hiddenPhraseEntry.grid(row=5, column=1, padx=10) # Location of the text.
- self.doLoginBtn.grid_forget()
- # Gets three random numbers or letters from the hidden phrase to complete login.
- challengeIndexes = self.getPhraseChallenge()
- challengeText = "Enter the characters number\n" + str(challengeIndexes[0]+1) # Makes you enter your first out of three number or letter.
- challengeText += ", " + str(challengeIndexes[1]+1) + " and " + str(challengeIndexes[2]+1) # Makes you enter your second out of three number or letter.
- challengeText += " from your hidden phrase.\n (comma separated)" # Makes you enter your third out of three number or letter.
- # Shows the hidden phrase challenge
- self.confirmPhraseLabel["text"]=challengeText # Shows the text
- self.confirmPhraseLabel.grid(row=8, column=0) # Location of the text.
- self.confirmPhraseBtn["command"] = lambda: self.confirmPhrase(userData[2], challengeIndexes)
- self.confirmPhraseBtn.grid(row=8, column=1) # Location of the text.
- else:
- # If the password doesn't match, a message saying that the password is incorrect will pop up.
- self.warningLabel["text"]="The password is incorrect" # The message saying that the password is incorrect.
- self.warningLabel.grid(row=7, columnspan=2) # Location of the text.
- else:
- #If the username is not recognised a message saying that the username was not found will pop up.
- self.warningLabel["text"]="Username not found" # The message saying that the username was not found.
- self.warningLabel.grid(row=7, columnspan=2) # Location of the text.
- def confirmPhrase(self, hiddenPhrase, indexes): # Function for the hidden phrase.
- # Gets the text from user input.
- confirmText = self.hiddenPhraseEntry.get()
- # Constructs 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" # This text will appear if the login is completed successfully.
- self.warningLabel.grid(row=9, columnspan=2) # Location of the text.
- else:
- #If the text does not match, show an alert
- self.warningLabel["text"]="Hidden phrase challenge failed" # This text will appear if the login is not completed successfully.
- self.warningLabel.grid(row=9, columnspan=2) # Location of the text.
- # Checks if the user is in the file and returns the data in a list.
- def findUserData(self, username):
- try:
- users = open("users.txt", "r") # Opens the local file in reading mode.
- lines = users.readlines() # Reads all the lines of the text and puts them in a list.
- # Checks each line.
- for line in lines:
- # The user data is separated by ; so it splits the text and checks if the username matches the first field in the line
- data = line.split(";")
- if data[0]==username:
- # If found, returns the data.
- return data
- return None
- except FileNotFoundError:
- # If the file does not exist, nothing will register.
- return None
- # Generates 3 random positions from 0 to 9 to ask the user for the hidden phrase challenge.
- def getPhraseChallenge(self):
- # Creates an empty list
- indexes = []
- #3 times loop
- for i in range(1,4): # This line of code will determine how many letters or numbers of the hidden phrase the user must confirm with 1 being the least to 4 being the most.
- # If the number is already in the list, it will generate one that isn't.
- while num in indexes:
- num = random.randint(0,10)
- # Adds the number to the list.
- indexes.append(num)
- return indexes # Returns the list
- def register(self): # Function for making an account.
- self.clearWarnings() # Clears any previous warnings (if any).
- username = self.usernameEntry.get() # Gets the username input fields.
- password = self.passwordEntry.get() # Gets the password input fields.
- hiddenPhrase = self.hiddenPhraseEntry.get() # Gets the hidden phrase input fields.
- if hiddenPhrase.isalnum() and len(hiddenPhrase) == 10: # Checks if the hidden phrase is 10 characters long and alphanumeric.
- # Checks that the username hasn't already been registered
- if not self.findUserData(username):
- # Opens 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"
- # Writes the data to the file and closes it
- users.write(data)
- users.close()
- # Cleans the form and shows a confirmation message
- self.cleanWidgets()
- self.warningLabel["text"]="Account setup successful"
- self.warningLabel.grid(row=7, columnspan=2) # Location of the text.
- else:
- # Shows an alert for a duplicate username entry
- self.warningLabel["text"]="Username already exists"
- self.warningLabel.grid(row=7, columnspan=2) # Location of the text.
- else:
- # Shows 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) # Location of the text.
- def registerForm(self):
- self.cleanWidgets()
- # Displays the username, password and hidden phrase input fields and labels for the register form.
- self.usernameLabel.grid(row=3, column=0, padx=10) # Location of the text.
- self.usernameEntry.grid(row=3, column=1, padx=10) # Location of the text.
- self.passwordLabel.grid(row=4, column=0, padx=10) # Location of the text.
- self.passwordEntry.grid(row=4, column=1, padx=10) # Location of the text.
- self.hiddenPhraseLabel.grid(row=5, column=0, padx=10) # Location of the text.
- self.hiddenPhraseEntry.grid(row=5, column=1, padx=10) # Location of the text.
- # Displays the register button
- self.doRegisterBtn.grid(row=6, columnspan=2, pady=10) # Location of the text.
- def loginForm(self):
- self.cleanWidgets()
- # Displays the username and password inut fields for the login form.
- self.usernameLabel.grid(row=3, column=0, padx=10) # Location of the text.
- self.usernameEntry.grid(row=3, column=1, padx=10) # Location of the text.
- self.passwordLabel.grid(row=4, column=0, padx=10) # Location of the text.
- self.passwordEntry.grid(row=4, column=1, padx=10) # Location of the text.
- # Displays the login button
- self.doLoginBtn.grid(row=6, columnspan=2, pady=10) # Location of the text.
- def cleanWidgets(self):
- # Hides 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):
- # Hides 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()
- # Sets the frame title.
- root.title("WELCOME")
- # Creates a new window with the root frame
- app = MainWindow(master=root)
- # Starts/displays the window.
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement