Reputation: 25
Okay so I have been working on this piece of coursework and I am trying to implement a login check before allowing a user to edit their details. However, for some reason, I can't use the get() method to get the data from the entry boxes in the popup. It won't recognise the entry boxes as they aren't defined in the checkLogin function. I'm not sure how to reference the entries properly because they are in a popup. Here's what I have so far:
import tkinter as tk
def popupmsg(window,msg):
popup = tk.Tk()
popup.wm_title("Alert")
messageLabel = tk.Label(popup, text=msg, font=MEDIUM_FONT)
messageLabel.grid(row=1, sticky="nsew")
okayButton = tk.Button(popup, text="Okay", command = popup.destroy)
okayButton.grid(row=2, sticky="nsew")
popup.mainloop()
def checkLogin(window):
username = usernameEntry.get()
password = passwordEntry.get()
if username == "USERNAME" and password == "PASSWORD":
popupmsg(window, "Login Successful")
usernameEntry.delete(0, "end") #clears username from login
passwordEntry.delete(0, "end") #clears password from login
else:
popupmsg(window, "Invalid Login") #presents error message if login is incorrect
def popupquery(window):
popup = tk.Tk()
popup.wm_title("Login Check")
messageLabel = tk.Label(popup, text="Enter your username and password to edit details.", font=MEDIUM_FONT)
messageLabel.grid(row=1, sticky="nsew")
usernameLabel = tk.Label(popup, text="Username: ", font=MEDIUM_FONT)
usernameLabel.grid(row=2, sticky="nsew")
usernameVar = tk.StringVar(popup)
usernameEntry = tk.Entry(popup, textvariable=usernameVar)
usernameEntry.grid(row=3,column=0)
passwordLabel = tk.Label(popup, text="Password:", font=MEDIUM_FONT)
passwordLabel.grid(row=4, sticky="nsew")
passwordVar = tk.StringVar(popup)
passwordEntry = tk.Entry(popup, textvariable=passwordVar)
passwordEntry.grid(row=5,column=0)
okayButton = tk.Button(popup, text="Okay", command = lambda: checkLogin(window))
okayButton.grid(row=6, sticky="nsew")
popup.mainloop()
MEDIUM_FONT = ("Berlin Sans FB", 12)
LARGE_FONT = ("Berlin Sans FB", 16)
window = tk.Tk()
titleLabel = tk.Label(window, text="View Stylist", font=LARGE_FONT, bg="#FFC0CB")
titleLabel.grid(columnspan = 4)
#searches record using entered data
editButton = tk.Button(window, text="Edit Personal Profile",
command=lambda:popupquery(window))
editButton.grid(row=2, column=2, sticky="ew")
window.mainloop()
Upvotes: 0
Views: 60
Reputation: 15226
Sooner or later you will need to learn to write in a class using the OOP method.
I have restructured your code to be written in a class to illustrate how using a class attribute and class methods is cleaner and easier to deal with.
I have removed the unnecessary variable names for widgets that don't really need them here. You really only need to assign a widget to a variable name if you plan on editing/updating this widget later. If the widget is static and wont need to be changed then you can forgo the variable name.
Now that everything has been moved into a class we do not need to use global
and we are able to clean up the code a bit. It makes it easier to read and update.
When writing up a tkinter GUI you really only need 1 instance of Tk()
. Instead of using Tk()
to open new windows you should instead use Toplevel()
as this is what it was designed for.
Another simple clean up was removing the lambda
expressions as in this class structure we don't need to use lambda here. There may be times where you will need/want to use lambda in a class but in this case it is not needed.
One last change. Even though you can use textvariable
here there is no real reason to use it. Its not helping or hurting you in this case so you can just leave the textvariable
argument out and simple use get()
on the entry fields.
Read over the below code and let me know if you have any questions.
import tkinter as tk
class App(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.MEDIUM_FONT = ("Berlin Sans FB", 12)
self.LARGE_FONT = ("Berlin Sans FB", 16)
self.root = parent
tk.Label(self, text="View Stylist", font=self.LARGE_FONT, bg="#FFC0CB").grid(columnspan = 4)
tk.Button(self, text="Edit Personal Profile", command=self.popupquery).grid(row=2, column=2, sticky="ew")
def popupmsg(self, msg):
popup = tk.Toplevel(self.root)
popup.title("Alert")
tk.Label(popup, text=msg, font=self.MEDIUM_FONT).grid(row=1, sticky="nsew")
tk.Button(popup, text="Okay", command = popup.destroy).grid(row=2, sticky="nsew")
def checkLogin(self):
if self.usernameEntry.get() == "USERNAME" and self.passwordEntry.get() == "PASSWORD":
self.popupmsg("Login Successful")
self.usernameEntry.delete(0, "end")
self.passwordEntry.delete(0, "end")
else:
self.popupmsg("Invalid Login")
def popupquery(self):
popup = tk.Toplevel(self.root)
popup.title("Login Check")
tk.Label(popup, text="Enter your username and password to edit details.", font=self.MEDIUM_FONT).grid(row=1, sticky="nsew")
tk.Label(popup, text="Username: ", font=self.MEDIUM_FONT).grid(row=2, sticky="nsew")
tk.Label(popup, text="Password:", font=self.MEDIUM_FONT).grid(row=4, sticky="nsew")
self.usernameEntry = tk.Entry(popup)
self.usernameEntry.grid(row=3,column=0)
self.passwordEntry = tk.Entry(popup)
self.passwordEntry.grid(row=5,column=0)
tk.Button(popup, text="Okay", command = self.checkLogin).grid(row=6, sticky="nsew")
if __name__ == "__main__":
root = tk.Tk()
App(root).pack()
tk.mainloop()
Upvotes: 0
Reputation: 1474
Declare it as a global variable
def popupquery(window):
global usernameEntry, passwordEntry
------------
then this function too
def checkLogin(window):
global usernameEntry, passwordEntry
-------------
You should use Toplevel
rather to call Tk
and mainloop
morethan once
Your full code
import tkinter as tk
def popupmsg(window,msg):
popup = tk.Toplevel()
popup.wm_title("Alert")
messageLabel = tk.Label(popup, text=msg, font=MEDIUM_FONT)
messageLabel.grid(row=1, sticky="nsew")
okayButton = tk.Button(popup, text="Okay", command = popup.destroy)
okayButton.grid(row=2, sticky="nsew")
def checkLogin(window):
global usernameEntry, passwordEntry
username = usernameEntry.get()
password = passwordEntry.get()
if username == "USERNAME" and password == "PASSWORD":
popupmsg(window, "Login Successful")
usernameEntry.delete(0, "end") #clears username from login
passwordEntry.delete(0, "end") #clears password from login
else:
popupmsg(window, "Invalid Login") #presents error message if login is incorrect
def popupquery(window):
global usernameEntry, passwordEntry
popup = tk.Toplevel()
popup.wm_title("Login Check")
messageLabel = tk.Label(popup, text="Enter your username and password to edit details.", font=MEDIUM_FONT)
messageLabel.grid(row=1, sticky="nsew")
usernameLabel = tk.Label(popup, text="Username: ", font=MEDIUM_FONT)
usernameLabel.grid(row=2, sticky="nsew")
usernameVar = tk.StringVar(popup)
usernameEntry = tk.Entry(popup, textvariable=usernameVar)
usernameEntry.grid(row=3,column=0)
passwordLabel = tk.Label(popup, text="Password:", font=MEDIUM_FONT)
passwordLabel.grid(row=4, sticky="nsew")
passwordVar = tk.StringVar(popup)
passwordEntry = tk.Entry(popup, textvariable=passwordVar)
passwordEntry.grid(row=5,column=0)
okayButton = tk.Button(popup, text="Okay", command = lambda: checkLogin(window))
okayButton.grid(row=6, sticky="nsew")
MEDIUM_FONT = ("Berlin Sans FB", 12)
LARGE_FONT = ("Berlin Sans FB", 16)
window = tk.Tk()
titleLabel = tk.Label(window, text="View Stylist", font=LARGE_FONT,
bg="#FFC0CB")
titleLabel.grid(columnspan = 4)
#searches record using entered data
editButton = tk.Button(window, text="Edit Personal Profile",
command=lambda:popupquery(window))
editButton.grid(row=2, column=2, sticky="ew")
window.mainloop()
Upvotes: 1