Global Variable 'getInfo' is undefined
Question: I'm working on developing a GUI for a project and once I put all of this into
a class, it is returning saying

    
    
        Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
        return self.func(*args)
      File "c:\users\ryan\documents\visual studio 2015\Projects\Group_3_Project\Group_3_Project\Group_3_Project.py", line 30, in <lambda>
        b1 = Button(root, text = 'Submit', command = (lambda e = ents: getInfo(e)))
    NameError: global name 'getInfo' is not defined
    

Here is my code so far:

    
    
    from tkinter import*
    
    
    class GUI:
        fields = 'Last Name', 'First Name', 'Field', 'Phone Number', 'Office number'
    
        def getInfo(entries):
            for entry in entries:
                field = entry[0]
                text = entry[1].get()
                print('%s: "%s"' % (field, text))
    
        def makeForm(root, fields):
            entries = []
            for field in fields:
                row = Frame(root)
                lab = Label(row, width = 15, text = field, anchor = 'w')
                ent = Entry(row)
                row.pack(side = TOP, fill = X, padx = 5, pady = 5)
                lab.pack(side = LEFT)
                ent.pack(side = RIGHT, expand = YES, fill = X)
                entries.append((field, ent))
            return entries 
    
        if __name__ == '__main__':
            root = Tk()
            root.wm_title("HoursWizard")
            ents = makeForm(root, fields)
            root.bind('<Return>', (lambda event, e = ents: getInfo(e)))
            b1 = Button(root, text = 'Submit', command = (lambda e = ents: getInfo(e)))
            b2 = Button(root, text = 'Quit', command = root.quit)
            b1.pack(side = LEFT, padx = 5, pady = 5)
            b2.pack(side = LEFT, padx = 5, pady = 5)
            root.mainloop()
    

I have no idea what is going on and why it isn't working correctly. I'm sure
it is an easy fix and I'm just missing something. Any help is appreciated.
Thanks!


Answer: You should check the [official Python
tutorial](https://docs.python.org/3.4/tutorial/index.html) and look at the
section on classes. Basically, your scoping and namespaces are not what you
think they are. Every class method (unless it's been designated as static) is
first passed the instance itself, usually denoted with `self`. You would then
refer to instance attributes with `self.myattribute`. In `getInfo`, for
example, what you call `entries` isn't entries at all, but rather the instance
of the `GUI` class that has been created.

I highly recommend you look up some tutorials for how to make an OO Tkinter
app. It generally goes like this:

    
    
    class App:
        def __init__(self, parent):
            self.parent = parent
            self.parent.after(5000, self.other_method) # just a demo
            # create buttons, lay out geometry, etc.
        def other_method(self):
            self.do_print()
        def do_print(self):
            print('hello world')
    
    root = Tk()
    app = App(root)
    root.mainloop()
    

