xXVelocityXx

Banking System Project

Mar 5th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.14 KB | None | 0 0
  1. import tkinter as tk
  2. import random
  3.  
  4. class MainWindow(tk.Frame):
  5.  
  6.     def __init__(self, master=None):
  7.         #master constructor
  8.         super().__init__(master)
  9.         self.grid(columnspan=2)
  10.         #call the creation of all UI items used
  11.         self.create_widgets()
  12.  
  13.     def create_widgets(self):
  14.         #Widget creation, all GUI related items defined here
  15.         #Banner
  16.         self.banner = tk.Label(self, text="WELCOME TO THE BANKING SYSTEM")
  17.         self.banner.grid(row=0, columnspan=2, padx=5, pady=10)
  18.         #Buttons
  19.         self.loginBtn = tk.Button(self, text="Login with existing account", command=self.loginForm)
  20.         self.loginBtn.grid(row=1, column=0, padx=5)
  21.         self.createBtn = tk.Button(self, text="Setup an account", command=self.registerForm)
  22.         self.createBtn.grid(row=1, column=1, padx=5)
  23.         self.quitBtn = tk.Button(self, text="QUIT", fg="red", command=root.destroy)
  24.         self.quitBtn.grid(row=2, column=0, columnspan=2, pady=20)
  25.         #user widgets
  26.         self.usernameLabel = tk.Label(self, text="Username:")
  27.         self.usernameEntry = tk.Entry(self)
  28.         self.passwordLabel = tk.Label(self, text="Password:")
  29.         self.passwordEntry = tk.Entry(self, show="*")
  30.         self.hiddenPhraseLabel = tk.Label(self, text="Hidden phrase:")
  31.         self.hiddenPhraseEntry = tk.Entry(self)
  32.         #
  33.         self.doLoginBtn = tk.Button(self, text="Login", command=self.login)
  34.         self.doRegisterBtn = tk.Button(self, text="Register", command=self.register)
  35.         self.warningLabel = tk.Label(self, fg="red")
  36.         self.confirmPhraseLabel = tk.Label(self, fg="blue")
  37.         self.confirmPhraseBtn = tk.Button(self, text="Confirm phrase")
  38.  
  39.     def login(self):
  40.         #clear previous warning messages (if any)
  41.         self.clearWarnings()
  42.         #get input values from the username and password fields
  43.         username = self.usernameEntry.get()
  44.         password = self.passwordEntry.get()
  45.         #check if user exists in the text file
  46.         userData = self.findUserData(username)
  47.         if userData:
  48.             #Check if given password matches the stored password
  49.             if userData[1]==password:
  50.                 #Show the hidden phrase challenge (last step of login)
  51.                 self.hiddenPhraseLabel.grid(row=5, column=0, padx=10)
  52.                 self.hiddenPhraseEntry.grid(row=5, column=1, padx=10)
  53.                 self.doLoginBtn.grid_forget()
  54.                 #Get the random positions to ask the user from the phrase
  55.                 challengeIndexes = self.getPhraseChallenge()
  56.                 challengeText = "Enter the characters number\n" + str(challengeIndexes[0]+1)
  57.                 challengeText += ", " + str(challengeIndexes[1]+1) + " and " + str(challengeIndexes[2]+1)
  58.                 challengeText += " from your hidden phrase.\n (comma separated)"
  59.                 #show the hidden phrase challenge
  60.                 self.confirmPhraseLabel["text"]=challengeText
  61.                 self.confirmPhraseLabel.grid(row=8, column=0)
  62.                 self.confirmPhraseBtn["command"] = lambda: self.confirmPhrase(userData[2], challengeIndexes)
  63.                 self.confirmPhraseBtn.grid(row=8, column=1)
  64.             else:
  65.                 #If the password doesn't match, show an alert
  66.                 self.warningLabel["text"]="The password is incorrect"
  67.                 self.warningLabel.grid(row=7, columnspan=2)
  68.         else:
  69.             #If the user does not exists in the file,  show an alert
  70.             self.warningLabel["text"]="Username not found"
  71.             self.warningLabel.grid(row=7, columnspan=2)
  72.  
  73.     def confirmPhrase(self, hiddenPhrase, indexes):
  74.         #Get the text from user input
  75.         confirmText = self.hiddenPhraseEntry.get()
  76.         #Construct how the user input should look using the random indexes
  77.         challengeText = hiddenPhrase[indexes[0]] + "," + hiddenPhrase[indexes[1]] + "," + hiddenPhrase[indexes[2]]
  78.         if confirmText == challengeText:
  79.             #If the texts match, complete login
  80.             self.cleanWidgets()
  81.             self.warningLabel["text"]="Login Successful!\nWelcome"
  82.             self.warningLabel.grid(row=9, columnspan=2)
  83.         else:
  84.             #If the text does not match, show an alert
  85.             self.warningLabel["text"]="Hidden phrase challenge failed"
  86.             self.warningLabel.grid(row=9, columnspan=2)        
  87.  
  88.     #Check if the user is in the file and return the data in a list
  89.     def findUserData(self, username):
  90.         try:
  91.             #open the local file in reading mode
  92.             users = open("users.txt", "r")
  93.             #read all the lines of the text and put them in a list
  94.             lines = users.readlines()
  95.             #Check each line
  96.             for line in lines:
  97.                 #The user data is separated by ; so split the text and check if the username matches the first field in the line
  98.                 data = line.split(";")
  99.                 if data[0]==username:
  100.                     #If found, return the data
  101.                     return data
  102.             return None
  103.         except FileNotFoundError:
  104.             #if the file does not exists, no user is registered yet
  105.             return None
  106.  
  107.     #generates 3 random positions from 0 to 9 to ask the user for the hidden phrase challenge
  108.     def getPhraseChallenge(self):
  109.         #create an empty list
  110.         indexes = []
  111.         #3 times loop
  112.         for i in range(1,4):
  113.             #generate random number
  114.             num = random.randint(0,10)
  115.             #if the number is already in the list, search for a new one that isn't
  116.             while num in indexes:
  117.                 num = random.randint(0,10)
  118.             #add the number to the list
  119.             indexes.append(num)
  120.         #return the list
  121.         return indexes
  122.  
  123.     def register(self):
  124.         #clear any previous warnings if any
  125.         self.clearWarnings()
  126.         #get the username, password and hidden phrase input fields
  127.         username = self.usernameEntry.get()
  128.         password = self.passwordEntry.get()
  129.         hiddenPhrase = self.hiddenPhraseEntry.get()
  130.         #Check if the hidden phrase is 10 characters long and alphanumeric
  131.         if hiddenPhrase.isalnum() and len(hiddenPhrase) == 10:
  132.             #Check that the username isn't already been registered
  133.             if not self.findUserData(username):
  134.                 #Open the local file in append mode (to write new data at the end of the file)
  135.                 users = open("users.txt", "a+")
  136.                 #creates the structure of the saved data (username;password;hiddenphrase)
  137.                 data = username + ";" + password + ";" + hiddenPhrase + "\n"
  138.                 #write the data to the file and close it
  139.                 users.write(data)
  140.                 users.close()
  141.                 #Clean the form and show a confirmation message
  142.                 self.cleanWidgets()
  143.                 self.warningLabel["text"]="Account setup successful"
  144.                 self.warningLabel.grid(row=7, columnspan=2)
  145.             else:
  146.                 #Show an alert for a duplicate username entry
  147.                 self.warningLabel["text"]="Username already exists"
  148.                 self.warningLabel.grid(row=7, columnspan=2)
  149.         else:
  150.             #Show an alert stating the hidden phrase constraints
  151.             self.warningLabel["text"]="The hidden phrase needs to be alphanumeric\n with no spaces and 10 characters long"
  152.             self.warningLabel.grid(row=7, columnspan=2)
  153.  
  154.     def registerForm(self):
  155.         self.cleanWidgets()
  156.         #Display the username, password and hidden phrase input fields and labels for the register form
  157.         self.usernameLabel.grid(row=3, column=0, padx=10)
  158.         self.usernameEntry.grid(row=3, column=1, padx=10)
  159.         self.passwordLabel.grid(row=4, column=0, padx=10)
  160.         self.passwordEntry.grid(row=4, column=1, padx=10)
  161.         self.hiddenPhraseLabel.grid(row=5, column=0, padx=10)
  162.         self.hiddenPhraseEntry.grid(row=5, column=1, padx=10)
  163.         #display the register button
  164.         self.doRegisterBtn.grid(row=6, columnspan=2, pady=10)
  165.  
  166.     def loginForm(self):
  167.         self.cleanWidgets()
  168.         #Display the username and password inut fields for the login form
  169.         self.usernameLabel.grid(row=3, column=0, padx=10)
  170.         self.usernameEntry.grid(row=3, column=1, padx=10)
  171.         self.passwordLabel.grid(row=4, column=0, padx=10)
  172.         self.passwordEntry.grid(row=4, column=1, padx=10)
  173.         #Display the login button
  174.         self.doLoginBtn.grid(row=6, columnspan=2, pady=10)
  175.  
  176.     def cleanWidgets(self):
  177.         #Hide all the login/register related items (if present)
  178.         self.usernameLabel.grid_forget()
  179.         self.usernameEntry.grid_forget()
  180.         self.passwordLabel.grid_forget()
  181.         self.passwordEntry.grid_forget()
  182.         self.hiddenPhraseLabel.grid_forget()
  183.         self.hiddenPhraseEntry.grid_forget()
  184.         self.doRegisterBtn.grid_forget()
  185.         self.doLoginBtn.grid_forget()
  186.         self.warningLabel.grid_forget()
  187.         self.confirmPhraseLabel.grid_forget()
  188.         self.confirmPhraseBtn.grid_forget()
  189.  
  190.     def clearWarnings(self):
  191.         #hide all the warning/messages in the UI (if any)
  192.         self.warningLabel.grid_forget()
  193.         self.confirmPhraseLabel.grid_forget()
  194.         self.confirmPhraseBtn.grid_forget()
  195.  
  196. #Creates a root Frame from Tkinter
  197. root = tk.Tk()
  198. #Set the frame title
  199. root.title("WELCOME")
  200. #Creates a new window with the root frame
  201. app = MainWindow(master=root)
  202. #start/display the window
  203. app.mainloop()
Add Comment
Please, Sign In to add comment