I found an answer relating to how I can access data from other pages, which is creating a dictionary in the SampleApp class, and then use the controller to let every page access that. However, my question is how can I have it so that if there are multiple users, how can I display their username and passcode in another frame? Or would it use the dictionary I created in the SampleApp class, accessing the data from the page I want to access it from?
Here is my code:
import tkinter as tk
from tkinter import font as tkfont
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title_font = tkfont.Font(family = 'Garamond', size = 18, weight = "bold", slant = "italic")
#container we'll stack frames on top of each other, then the one we want to
#visible will be raised above the others
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.shared_data = {"username": tk.StringVar(),
"passcode": tk.StringVar()}
self.frames = {}
for F in (StartPage, PageOne, PageTwo, UserInfo, UserPage):
page_name = F.__name__
frame = F(parent = container, controller = self)
self.frames[page_name] = frame
#put all of the pages in the same location;
#the one on the top of the stacking order
#will be the one that is visible
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
frame.configure(background = 'teal')
#starting frame
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text = "Sample App", font = controller.title_font)
label.pack(side = "top")
button1 = tk.Button(self, text = "Go to Page One",
command = lambda: controller.show_frame("PageOne"))
button2 = tk.Button(self, text = "Go to Page Two", bg = "green", fg = "white", width = 10,
height = 4, command = lambda: controller.show_frame("PageTwo"))
Exitbutton = tk.Button(self, text="Exit", width = 8, height = 3, command = self.Exit)
button1.place(x = 200, y = 200)
button2.place(x = 600, y = 200)
Exitbutton.place(x = 350, y = 330)
def Exit(self):
self.controller.destroy()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text = "This is Page One", font = controller.title_font)
label.pack(side = "top")
button1 = tk.Button(self, text = "Go to Start Page",
command = lambda: controller.show_frame("StartPage"))
button2 = tk.Button(self, text = "Go to Page Two",
command = lambda: controller.show_frame("PageTwo"))
button3 = tk.Button(self, text = "User Info",
command = lambda: controller.show_frame("UserInfo"))
button1.place(x = 200, y = 200)
button2.place(x=400, y = 200)
button3.place(x=300, y =340)
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text = "This is Page Two", font = controller.title_font)
label.pack(side = "top", fill = "x", pady = 10)
button1 = tk.Button(self, text = "Go to Start Page",
command = lambda: controller.show_frame("StartPage"))
button2 = tk.Button(self, text = "Go to Page One",
command = lambda: controller.show_frame("PageOne"))
button1.pack()
button2.pack()
class UserInfo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label1 = tk.Label(self, text = "User Info", font = controller.title_font)
label1.pack(side = "top")
label2 = tk.Label(self, text = "User's Name", font = "Arial 13")
label2.place(x=100, y=100)
nameEntry = tk.Entry(self, textvariable=self.controller.shared_data["username"])
nameEntry.place(x=250, y=100)
enterButton = tk.Button(self, text = "Enter",
command = lambda: controller.show_frame("UserPage"))
enterButton.place(x = 100, y = 350)
button1 = tk.Button(self, text = "Go to Start Page",
command = lambda: controller.show_frame("StartPage"))
button1.place(x=300, y = 350)
class UserPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label1 = tk.Label(self, text = "User Page", font = controller.title_font)
label1.pack(side = "top")
button1 = tk.Button(self, text = "Page One",
command = lambda: controller.show_frame("PageOne"))
button1.pack()
if __name__ == "__main__":
app = SampleApp()
app.title("Sample App")
app.geometry("800x500")
app.mainloop()