This is my first time posting a question so I'm sorry in advanced if I do anything wrong when posting the question.
I am using tkinter and using the same code as an answer that was posted here that used classes to create and show frames:
Switch between two frames in tkinter
I want to be able to create global variables so that other frames can use them when needed. I tried using root = tk.Tk()
and creating the global variable that way, but noticed that when doing that, not only did I not get the entry from the entry box, but it created a new window. I also tried global_var = tk.StringVar()
, not having root = tk.Tk()
, but got an error stating:
Traceback (most recent call last): File "app.py", line 6, in name_var = tk.StringVar() File "/usr/lib/python3.7/tkinter/init.py", line 480, in init Variable.init(self, master, value, name) File "/usr/lib/python3.7/tkinter/init.py", line 317, in init self._root = master._root() AttributeError: 'NoneType' object has no attribute '_root'
Is there a way for me to store that input from the entry box, and use it in another frame?
Source Code
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.frames = {}
for F in (StartPage, PageOne, PageTwo, UserInfo):
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')
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.controller.geometry("800x400")
label = tk.Label(self, text = "Facial Recognition Lock", 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", 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 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)
button1 = tk.Button(self, text = "Go to Start Page",
command = lambda: controller.show_frame("StartPage"))
button1.place(x=300, y = 350)
nameEntry = tk.Entry(self, textvariable = name_var, bd = 5, font=('calibre',10,'normal'))
nameEntry.place(x=220, y=100)
showButton = tk.Button(self, text="Show", command = submit)
showButton.place(x=200, y = 250)
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
Excluded PageTwo Class as it's similar to PageOne