Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.52 KB | None | 0 0
  1. import tkinter as tk # Creates a Tkinter window so the code runs in it's own window and not the Python shell.
  2. import random # Randomises integers.
  3.  
  4. class MainWindow(tk.Frame):
  5.  
  6.     def __init__(self, master=None):
  7.         # Master constructor (constucts the Tkinter window).
  8.         super().__init__(master)
  9.         self.grid(columnspan=2)
  10.         # Calls the creation of all UI items used.
  11.         self.create_widgets() # Calls the function.
  12.  
  13.     def create_widgets(self): # Window creation, all GUI related items defined here
  14.         # Banner
  15.         self.banner = tk.Label(self, text="WELCOME TO OUR BANKING SYSTEM") # Welcoming text.
  16.         self.banner.grid(row=0, columnspan=2, padx=5, pady=10) # Location of the welcoming text (at the top of the window).
  17.         # Buttons
  18.         self.loginBtn = tk.Button(self, text="Login with existing account", command=self.loginForm) # Buttom that allows you to log in.
  19.         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).
  20.         self.createBtn = tk.Button(self, text="Setup an account", command=self.registerForm) # Button that allows you to create a profile.
  21.         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).
  22.         self.quitBtn = tk.Button(self, text="QUIT", fg="red", command=root.destroy) # Quit button that quits the code. Also makes the letters red.
  23.         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).
  24.         # User widgets for seting up an account.
  25.         self.usernameLabel = tk.Label(self, text="Username:")
  26.         self.usernameEntry = tk.Entry(self)
  27.         self.passwordLabel = tk.Label(self, text="Password:")
  28.         self.passwordEntry = tk.Entry(self, show="*")
  29.         self.hiddenPhraseLabel = tk.Label(self, text="Hidden phrase:")
  30.         self.hiddenPhraseEntry = tk.Entry(self)
  31.         # User widgets for logging in with an existing one.
  32.         self.doLoginBtn = tk.Button(self, text="Login", command=self.login)
  33.         self.doRegisterBtn = tk.Button(self, text="Register", command=self.register)
  34.         self.warningLabel = tk.Label(self, fg="red")
  35.         self.confirmPhraseLabel = tk.Label(self, fg="blue")
  36.         self.confirmPhraseBtn = tk.Button(self, text="Confirm phrase")
  37.  
  38.     def login(self): # Function for the loggin phase.
  39.         self.clearWarnings() # Clears any previous warnings (if any)
  40.         username = self.usernameEntry.get() # Gets input value from the username field.
  41.         password = self.passwordEntry.get() # Gets input value from the password field.
  42.         userData = self.findUserData(username) # Checks if the username and password exist
  43.         if userData:
  44.             # Checks if given password matches the stored password.
  45.             if userData[1]==password:
  46.                 # Shows the hidden phrase challenge (last step of login).
  47.                 self.hiddenPhraseLabel.grid(row=5, column=0, padx=10) # Location of the text.
  48.                 self.hiddenPhraseEntry.grid(row=5, column=1, padx=10) # Location of the text.
  49.                 self.doLoginBtn.grid_forget()
  50.                 # Gets three random numbers or letters from the hidden phrase to complete login.
  51.                 challengeIndexes = self.getPhraseChallenge()
  52.                 challengeText = "Enter the characters number\n" + str(challengeIndexes[0]+1) # Makes you enter your first out of three number or letter.
  53.                 challengeText += ", " + str(challengeIndexes[1]+1) + " and " + str(challengeIndexes[2]+1) # Makes you enter your second out of three number or letter.
  54.                 challengeText += " from your hidden phrase.\n (comma separated)" # Makes you enter your third out of three number or letter.
  55.                 # Shows the hidden phrase challenge
  56.                 self.confirmPhraseLabel["text"]=challengeText # Shows the text
  57.                 self.confirmPhraseLabel.grid(row=8, column=0) # Location of the text.
  58.                 self.confirmPhraseBtn["command"] = lambda: self.confirmPhrase(userData[2], challengeIndexes)
  59.                 self.confirmPhraseBtn.grid(row=8, column=1) # Location of the text.
  60.             else:
  61.                 # If the password doesn't match, a message saying that the password is incorrect will pop up.
  62.                 self.warningLabel["text"]="The password is incorrect" # The message saying that the password is incorrect.
  63.                 self.warningLabel.grid(row=7, columnspan=2) # Location of the text.
  64.         else:
  65.             #If the username is not recognised a message saying that the username was not found will pop up.
  66.             self.warningLabel["text"]="Username not found" # The message saying that the username was not found.
  67.             self.warningLabel.grid(row=7, columnspan=2) # Location of the text.
  68.  
  69.     def confirmPhrase(self, hiddenPhrase, indexes): # Function for the hidden phrase.
  70.         # Gets the text from user input.
  71.         confirmText = self.hiddenPhraseEntry.get()
  72.         # Constructs how the user input should look using the random indexes.
  73.         challengeText = hiddenPhrase[indexes[0]] + "," + hiddenPhrase[indexes[1]] + "," + hiddenPhrase[indexes[2]]
  74.         if confirmText == challengeText:
  75.             #If the texts match, complete login
  76.             self.cleanWidgets()
  77.             self.warningLabel["text"]="Login Successful!\nWelcome" # This text will appear if the login is completed successfully.
  78.             self.warningLabel.grid(row=9, columnspan=2) # Location of the text.
  79.         else:
  80.             #If the text does not match, show an alert
  81.             self.warningLabel["text"]="Hidden phrase challenge failed" # This text will appear if the login is not completed successfully.
  82.             self.warningLabel.grid(row=9, columnspan=2) # Location of the text.    
  83.  
  84.     # Checks if the user is in the file and returns the data in a list.
  85.     def findUserData(self, username):
  86.         try:
  87.             users = open("users.txt", "r") # Opens the local file in reading mode.
  88.             lines = users.readlines() # Reads all the lines of the text and puts them in a list.
  89.             # Checks each line.
  90.             for line in lines:
  91.                 # The user data is separated by ; so it splits the text and checks if the username matches the first field in the line
  92.                 data = line.split(";")
  93.                 if data[0]==username:
  94.                     # If found, returns the data.
  95.                     return data
  96.             return None
  97.         except FileNotFoundError:
  98.             # If the file does not exist, nothing will register.
  99.             return None
  100.  
  101.     # Generates 3 random positions from 0 to 9 to ask the user for the hidden phrase challenge.
  102.     def getPhraseChallenge(self):
  103.         # Creates an empty list
  104.         indexes = []
  105.         #3 times loop
  106.         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.
  107.             # If the number is already in the list, it will generate one that isn't.
  108.             while num in indexes:
  109.                 num = random.randint(0,10)
  110.             # Adds  the number to the list.
  111.             indexes.append(num)
  112.         return indexes # Returns the list
  113.  
  114.     def register(self): # Function for making an account.
  115.         self.clearWarnings() # Clears any previous warnings (if any).
  116.         username = self.usernameEntry.get() # Gets the username input fields.
  117.         password = self.passwordEntry.get() # Gets the password input fields.
  118.         hiddenPhrase = self.hiddenPhraseEntry.get() # Gets the hidden phrase input fields.
  119.         if hiddenPhrase.isalnum() and len(hiddenPhrase) == 10: # Checks if the hidden phrase is 10 characters long and alphanumeric.
  120.             # Checks that the username hasn't already been registered
  121.             if not self.findUserData(username):
  122.                 # Opens the local file in append mode (to write new data at the end of the file)
  123.                 users = open("users.txt", "a+")
  124.                 # Creates the structure of the saved data (username;password;hiddenphrase)
  125.                 data = username + ";" + password + ";" + hiddenPhrase + "\n"
  126.                 # Writes the data to the file and closes it
  127.                 users.write(data)
  128.                 users.close()
  129.                 # Cleans the form and shows a confirmation message
  130.                 self.cleanWidgets()
  131.                 self.warningLabel["text"]="Account setup successful"
  132.                 self.warningLabel.grid(row=7, columnspan=2) # Location of the text.
  133.             else:
  134.                 # Shows an alert for a duplicate username entry
  135.                 self.warningLabel["text"]="Username already exists"
  136.                 self.warningLabel.grid(row=7, columnspan=2) # Location of the text.
  137.         else:
  138.             # Shows an alert stating the hidden phrase constraints.
  139.             self.warningLabel["text"]="The hidden phrase needs to be alphanumeric\n with no spaces and 10 characters long"
  140.             self.warningLabel.grid(row=7, columnspan=2) # Location of the text.
  141.  
  142.     def registerForm(self):
  143.         self.cleanWidgets()
  144.         # Displays the username, password and hidden phrase input fields and labels for the register form.
  145.         self.usernameLabel.grid(row=3, column=0, padx=10) # Location of the text.
  146.         self.usernameEntry.grid(row=3, column=1, padx=10) # Location of the text.
  147.         self.passwordLabel.grid(row=4, column=0, padx=10) # Location of the text.
  148.         self.passwordEntry.grid(row=4, column=1, padx=10) # Location of the text.
  149.         self.hiddenPhraseLabel.grid(row=5, column=0, padx=10) # Location of the text.
  150.         self.hiddenPhraseEntry.grid(row=5, column=1, padx=10) # Location of the text.
  151.         # Displays the register button
  152.         self.doRegisterBtn.grid(row=6, columnspan=2, pady=10) # Location of the text.
  153.  
  154.     def loginForm(self):
  155.         self.cleanWidgets()
  156.         # Displays the username and password inut fields for the login form.
  157.         self.usernameLabel.grid(row=3, column=0, padx=10) # Location of the text.
  158.         self.usernameEntry.grid(row=3, column=1, padx=10) # Location of the text.
  159.         self.passwordLabel.grid(row=4, column=0, padx=10) # Location of the text.
  160.         self.passwordEntry.grid(row=4, column=1, padx=10) # Location of the text.
  161.         # Displays the login button
  162.         self.doLoginBtn.grid(row=6, columnspan=2, pady=10) # Location of the text.
  163.  
  164.     def cleanWidgets(self):
  165.         # Hides all the login/register related items (if present).
  166.         self.usernameLabel.grid_forget()
  167.         self.usernameEntry.grid_forget()
  168.         self.passwordLabel.grid_forget()
  169.         self.passwordEntry.grid_forget()
  170.         self.hiddenPhraseLabel.grid_forget()
  171.         self.hiddenPhraseEntry.grid_forget()
  172.         self.doRegisterBtn.grid_forget()
  173.         self.doLoginBtn.grid_forget()
  174.         self.warningLabel.grid_forget()
  175.         self.confirmPhraseLabel.grid_forget()
  176.         self.confirmPhraseBtn.grid_forget()
  177.  
  178.     def clearWarnings(self):
  179.         # Hides all the warning/messages in the UI (if any).
  180.         self.warningLabel.grid_forget()
  181.         self.confirmPhraseLabel.grid_forget()
  182.         self.confirmPhraseBtn.grid_forget
  183.  
  184. # Creates a root Frame from Tkinter.
  185. root = tk.Tk()
  186. # Sets the frame title.
  187. root.title("WELCOME")
  188. # Creates a new window with the root frame
  189. app = MainWindow(master=root)
  190. # Starts/displays the window.
  191. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement