Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LoginPage(tkinter.Frame,SquadBot):
- def __init__(self,master,controller):
- tkinter.Frame.__init__(self,master)
- # initialising inherited parent class
- self.createImages()
- # creating an array of images for the class to use
- crestlabel = tkinter.Label(self,image=self.images["bigcrest"],background=self.palette["bgblue"])
- crestlabel.place(x=350,y=0,anchor="n")
- # creating crest image in centre of page
- self.unamelabel = tkinter.Label(self,text="Username:",background=self.palette["bgblue"],font="TkDefaultFont 9 bold")
- self.unamelabel.place(x=250,y=375,anchor="nw")
- # creating a label to show where to enter the username to login
- self.usernamevar = tkinter.StringVar()
- self.usernamevar.trace_add("write", lambda x,y,z: self.change_text(var=self.usernamevar))
- self.unameentry = ttk.Entry(self,text = self.usernamevar)
- self.unameentry.place(x=350,y=395,anchor="n",width=200)
- # creating the text box for the user to enter their username
- pwordlabel = ttk.Label(self,text="Password:",background=self.palette["bgblue"],font="TkDefaultFont 9 bold")
- pwordlabel.place(x=250,y=420,anchor="nw")
- # creating a label to show the user where to enter their password
- self.passwordvar = tkinter.StringVar()
- self.passwordvar.trace_add("write", lambda x,y,z: self.change_text(var=self.passwordvar))
- self.pwordentry = ttk.Entry(self,show="•",text=self.passwordvar)
- self.pwordentry.place(x=350,y=440,anchor="n",width=200)
- # creating a text box for the user to enter their password
- # shows • instead of characters when the user types their password in to prevent people seeing their password
- loginbutton = ttk.Button(self,text="Login",command = lambda: self.login(controller))
- loginbutton.place(x=350,y=468,anchor="n")
- # creates a button that executes LoginPage.Login() with SquadBot as an argument
- self.configure(bg=self.palette["bgblue"])
- # setting background of frame to palette["bgblue"], which is the main background colour of the program
- #notbutton = tkinter.Button(self,text="test",command = lambda: self.shownot(controller))
- #notbutton.place(x=700,y=500,anchor="se")
- # <nf> TESTING NOTIFICATIONS
- def shownot(self,parent):
- window = Notification(self,parent)
- # <nf> MORE NOTIFICATION TESTING
- def login(self,parent):
- username = self.unameentry.get()
- password = self.pwordentry.get()
- # retrieves username and password from the boxes,
- # and replaces any semicolons with a comma
- self.unameentry.delete(0,"end")
- self.pwordentry.delete(0,"end")
- # removes text from entry boxes, so that they are clear for when the user logs out
- data = (username,password)
- # creating tuple from entered data
- jsondata = json.dumps(data)
- # creating a JSON string from tuple
- sendingdata = "l" + jsondata
- # adding an "l" to the front - indicates it is a login request when the server parses it
- parent.conn.sendall(sendingdata.encode())
- # sends the string to the server
- print(sendingdata.encode()) # <nf>
- response = parent.conn.recv(1024).decode()
- # receives the response from the server
- if response[0] == "a": # indicates accepted login
- fullresp = response[1:]
- # removes the 'a' from the front of the data so the rest of it can be decoded
- data = fullresp.split(",")
- # creates an array of strings from the data, splitting it at commas
- parent.userlvl.set(int(data[0]))
- # setting userlevel to set certain functions depending on different users
- parent.fullname = data[2]
- # setting the name of the user to allow better human-computer interaction
- print(parent.fullname)
- print("login successful")
- print(parent.getuserlvl()) # <nf>
- # receives the user data from the server
- parent.userid.set(int(data[1]))
- # sets the user id of the user
- requestdata = parent.conn.recv(1024)
- print(requestdata)
- requests = json.loads(requestdata)
- msg = "There have been responses to your requests:\n\n"
- if len(requests) >0:
- for i in requests:
- msg += i[0]+":"+i[1]+"\n"
- notif = Notification(parent,msg)
- parent.welcomemessage.set("Welcome, {name}!".format(name=parent.fullname))
- # creates a welcome message that is displayed upon loggin in
- parent.showPage(MainPage,LoginPage) # displaying main page, from which other pages can be reached
- elif response[0] == "p": # password incorrect
- tkinter.messagebox.showerror("Error logging in","Password incorrect")
- elif response[0] == "u": # user not found
- tkinter.messagebox.showerror("Error logging in","Username or password incorrect")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement