I am in the process of creating a UI using tkinter for a program I have written in Python2.7. I have modified code I found here: Switch between two frames in tkinter
The way I was hoping the code would work is that when the "Apply" button is pressed, the method varset1
or varset2
(depending on the current page) would be called and this should save the user inputs to variables using the .get()
command.
However, when I attempt to save the user inputted entries (Initial, LastName, HouseNo and PostCode) to variables, this error message is returned:
AttributeError: 'NoneType' object has no attribute 'get'
Here is a simplified version of my code, it is still a bit long, though this is as concise as I could make it in order to reproduce the error:
import Tkinter as tk
class Setup(tk.Tk):
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 (page1, page2):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("page1")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class page1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
tk.Label(self, text="First Initial: ").grid(row=2, sticky=tk.E)
self.Initial = tk.Entry(self).grid(row=2, column=1)
tk.Label(self, text="Last Name: ").grid(row=3, sticky=tk.E)
self.LastName = tk.Entry(self).grid(row=3, column=1)
Quit = tk.Button(self, text="Exit",
command= self.quit).grid(row=13, column=0, sticky="sw")
Next = tk.Button(self, text="OK",
command=lambda: controller.show_frame("page2")).grid(
row=13, column=1, sticky=tk.SE)
Apply = tk.Button(self, text="Apply",
command = self.varset1).grid(row=13, column=1,
sticky=tk.SW)
def varset1(self):
initial = self.Initial.get()
lastname = self.LastName.get()
class page2(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
tk.Label(self, text="House Number: ").grid(row=2, sticky=tk.E)
self.HouseNo = tk.Entry(self).grid(row=2, column=1)
tk.Label(self, text="Post Code: ").grid(row=3, sticky=tk.E)
self.PostCode = tk.Entry(self).grid(row=3, column=1)
Quit = tk.Button(self, text="Exit",
command= self.quit).grid(row=13, column=0, sticky="sw")
Back = tk.Button(self, text="Back",
command=lambda: controller.show_frame("page1")).grid(
row=13, column=1, sticky=tk.SE)
Apply = tk.Button(self, text="Apply",
command = self.varset2).grid(row=13, column=1,
sticky=tk.SW)
def varset2(self):
houseno = self.HouseNo.get()
postcode = self.PostCode.get()
if __name__ == "__main__":
app = Setup()
app.mainloop()
I really appreciate any help!