Skip to content

Instantly share code, notes, and snippets.

@nicholaskajoh
Created January 24, 2017 07:43
Show Gist options
  • Save nicholaskajoh/e1d367c1ac279c0a4170bc4c32ec4588 to your computer and use it in GitHub Desktop.
Save nicholaskajoh/e1d367c1ac279c0a4170bc4c32ec4588 to your computer and use it in GitHub Desktop.
Simple GUI Login System
# reference (https://pythonprogramming.net/change-show-new-frame-tkinter/)
import tkinter as tk
# window
class LoginApp(tk.Tk):
# constructor
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (LoginPage, HomePage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(LoginPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
# frame
class LoginPage(tk.Frame):
correctPassword = "password1234"
# constructor
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
title = tk.Label(self, text="Login")
title.pack(pady=10,padx=10)
errorMsg = tk.StringVar()
errorLabel = tk.Label(self, textvariable=errorMsg)
errorLabel.pack(pady=10,padx=10)
enteredPassword = tk.StringVar()
passwordEntry = tk.Entry(self, show="*", textvariable=enteredPassword)
passwordEntry.pack()
loginBtn = tk.Button(
self, text="Login",
command=lambda: self.validatePassword(enteredPassword.get(), errorMsg)
)
loginBtn.pack()
def validatePassword(self, password, errorMsg):
if password == self.correctPassword:
app.show_frame(HomePage)
else:
errorMsg.set("Invalid password!")
# frame
class HomePage(tk.Frame):
# constructor
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
title = tk.Label(self, text="Home Page (do whatever here!)")
title.pack(pady=10,padx=10)
app = LoginApp()
app.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment