Advertisement
Guest User

Untitled

a guest
Mar 29th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.33 KB | None | 0 0
  1. class LoginPage(tkinter.Frame,SquadBot):
  2.     def __init__(self,master,controller):
  3.         tkinter.Frame.__init__(self,master)
  4.         # initialising inherited parent class
  5.         self.createImages()
  6.         # creating an array of images for the class to use
  7.        
  8.         crestlabel = tkinter.Label(self,image=self.images["bigcrest"],background=self.palette["bgblue"])
  9.         crestlabel.place(x=350,y=0,anchor="n")
  10.         # creating crest image in centre of page
  11.        
  12.         self.unamelabel = tkinter.Label(self,text="Username:",background=self.palette["bgblue"],font="TkDefaultFont 9 bold")
  13.         self.unamelabel.place(x=250,y=375,anchor="nw")
  14.         # creating a label to show where to enter the username to login
  15.         self.usernamevar = tkinter.StringVar()
  16.         self.usernamevar.trace_add("write", lambda x,y,z: self.change_text(var=self.usernamevar))
  17.         self.unameentry = ttk.Entry(self,text = self.usernamevar)
  18.         self.unameentry.place(x=350,y=395,anchor="n",width=200)
  19.         # creating the text box for the user to enter their username
  20.        
  21.         pwordlabel = ttk.Label(self,text="Password:",background=self.palette["bgblue"],font="TkDefaultFont 9 bold")
  22.         pwordlabel.place(x=250,y=420,anchor="nw")
  23.         # creating a label to show the user where to enter their password
  24.         self.passwordvar = tkinter.StringVar()
  25.         self.passwordvar.trace_add("write", lambda x,y,z: self.change_text(var=self.passwordvar))
  26.         self.pwordentry = ttk.Entry(self,show="•",text=self.passwordvar)  
  27.         self.pwordentry.place(x=350,y=440,anchor="n",width=200)
  28.         # creating a text box for the user to enter their password
  29.         # shows • instead of characters when the user types their password in to prevent people seeing their password
  30.        
  31.         loginbutton = ttk.Button(self,text="Login",command = lambda: self.login(controller))
  32.         loginbutton.place(x=350,y=468,anchor="n")
  33.         # creates a button that executes LoginPage.Login() with SquadBot as an argument
  34.        
  35.         self.configure(bg=self.palette["bgblue"])
  36.         # setting background of frame to palette["bgblue"], which is the main background colour of the program
  37.        
  38.         #notbutton = tkinter.Button(self,text="test",command = lambda: self.shownot(controller))
  39.         #notbutton.place(x=700,y=500,anchor="se")
  40.         # <nf> TESTING NOTIFICATIONS
  41.        
  42.     def shownot(self,parent):
  43.         window = Notification(self,parent)
  44.         # <nf>  MORE NOTIFICATION TESTING
  45.        
  46.     def login(self,parent):
  47.         username = self.unameentry.get()
  48.         password = self.pwordentry.get()
  49.         # retrieves username and password from the boxes,
  50.         # and replaces any semicolons with a comma
  51.        
  52.         self.unameentry.delete(0,"end")
  53.         self.pwordentry.delete(0,"end")
  54.         # removes text from entry boxes, so that they are clear for when the user logs out
  55.        
  56.         data = (username,password)
  57.         # creating tuple from entered data
  58.        
  59.         jsondata = json.dumps(data)
  60.         # creating a JSON string from tuple
  61.        
  62.         sendingdata = "l" + jsondata
  63.         # adding an "l" to the front - indicates it is a login request when the server parses it
  64.        
  65.         parent.conn.sendall(sendingdata.encode())
  66.         # sends the string to the server
  67.        
  68.         print(sendingdata.encode()) # <nf>
  69.        
  70.         response = parent.conn.recv(1024).decode()
  71.         # receives the response from the server
  72.        
  73.         if response[0] == "a":  # indicates accepted login
  74.             fullresp = response[1:]
  75.             # removes the 'a' from the front of the data so the rest of it can be decoded
  76.            
  77.             data = fullresp.split(",")
  78.             # creates an array of strings from the data, splitting it at commas
  79.            
  80.             parent.userlvl.set(int(data[0]))
  81.             # setting userlevel to set certain functions depending on different users
  82.            
  83.             parent.fullname = data[2]
  84.             # setting the name of the user to allow better human-computer interaction
  85.            
  86.             print(parent.fullname)
  87.             print("login successful")
  88.             print(parent.getuserlvl())  # <nf>
  89.             # receives the user data from the server
  90.    
  91.            
  92.             parent.userid.set(int(data[1]))
  93.             # sets the user id of the user
  94.             requestdata = parent.conn.recv(1024)
  95.             print(requestdata)
  96.             requests = json.loads(requestdata)
  97.             msg = "There have been responses to your requests:\n\n"
  98.             if len(requests) >0:
  99.                 for i in requests:
  100.                     msg += i[0]+":"+i[1]+"\n"
  101.                 notif = Notification(parent,msg)
  102.            
  103.             parent.welcomemessage.set("Welcome, {name}!".format(name=parent.fullname))
  104.             # creates a welcome message that is displayed upon loggin in
  105.             parent.showPage(MainPage,LoginPage)   # displaying main page, from which other pages can be reached
  106.            
  107.         elif response[0] == "p":    # password incorrect
  108.             tkinter.messagebox.showerror("Error logging in","Password incorrect")
  109.         elif response[0] == "u":    # user not found
  110.             tkinter.messagebox.showerror("Error logging in","Username or password incorrect")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement