-1

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!

Community
  • 1
  • 1
C. Pugh
  • 103
  • 2
  • 10
  • a basic search of the error message would have given you an answer. Please do a little research before posting questions. Also, you really only need about half a dozen lines of code to reproduce this particular error. – Bryan Oakley Mar 23 '17 at 14:07
  • I did a lot of searching, though due to my obvious lack of programming experience, I unfortunately couldn't see the relation between those answers and my particular case. I would not be lazy enough to request the help of the kind people on the forum if I believed my question had already been answered. This is all a learning experience and I can appreciate why you would write this comment. – C. Pugh Mar 23 '17 at 15:01
  • @BryanOakley And with regards to the code needing only a dozen lines to reproduce the error, again, this is due to my obvious lack of programming experience. Hopefully, with more time and experience I will be able to produce more concise code. – C. Pugh Mar 23 '17 at 15:04

1 Answers1

2

Every variables you made to assign Entry and Button are grided using this format:

self.Initial = tk.Entry(self).grid(row=2, column=1)

which is wrong because tk.Entry(self) returns the Entry object, but tk.Entry(self).grid(row=2, column=1) returns None. So self.Initial is actually None. What you need to do is to split it into two lines like this for every Entry or Button that you're going to retrieve data from:

self.Initial = tk.Entry(self)
self.Initial.grid(row=2, column=1)

You should change every one of those for connivance and to avoid further similar errors(which is what you should've done in the first place)...

self.LastName = tk.Entry(self).grid(row=3, column=1)
# into:
self.LastName = tk.Entry(self)
self.LastName.grid(row=3, column=1)
# and so on....
Taku
  • 31,927
  • 11
  • 74
  • 85