Skip to content

Instantly share code, notes, and snippets.

@novel-yet-trivial
Last active December 4, 2017 17:23
Show Gist options
  • Save novel-yet-trivial/3e2863fef2b8cd2afa317ff0c7389d3d to your computer and use it in GitHub Desktop.
Save novel-yet-trivial/3e2863fef2b8cd2afa317ff0c7389d3d to your computer and use it in GitHub Desktop.
"""
An example of how to show one tkinter window after another.
This program will show a password entry dialog. If the ppassword is correct,
it will close the dialog and open the main body of the program.
"""
import tkinter as tk
class MainApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.frame = FirstFrame(self) # set first frame to appear here
self.frame.pack()
def change(self, frame):
self.frame.pack_forget() # delete currrent frame
self.frame = frame(self)
self.frame.pack() # make new frame
class FirstFrame(tk.Frame):
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
master.title("Enter password")
master.geometry("300x200")
self.status = tk.Label(self, fg='red')
self.status.pack()
lbl = tk.Label(self, text='Enter password')
lbl.pack()
self.pwd = tk.Entry(self, show="*")
self.pwd.pack()
self.pwd.focus()
self.pwd.bind('<Return>', self.check)
btn = tk.Button(self, text="Done", command=self.check)
btn.pack()
btn = tk.Button(self, text="Cancel", command=self.quit)
btn.pack()
def check(self, event=None):
if self.pwd.get() == 'password':
self.master.change(SecondFrame) # correct password, switch to the second frame
else:
self.status.config(text="wrong password")
class SecondFrame(tk.Frame):
def __init__(self, master=None, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
master.title("Main application")
master.geometry("600x400")
lbl = tk.Label(self, text='You made it to the main application')
lbl.pack()
if __name__=="__main__":
app=MainApp()
app.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment