Why does it not find the variable?
<p>So below is my code, and I need to get the variable 'p' from the Entry widget, set it as a new variable name, the print it. For some reason, I get the following error 'NameError: name 'p' is not defined'. I have absolutely no idea how to fix it and this is my last resort. Please help me.</p>

<p>Code:</p>

<pre><code>import tkinter as tk   # python3
#import Tkinter as tk   # python

self = tk
TITLE_FONT = ("Helvetica", 18, "bold")

#-------------------FUNCTIONS-------------------#
def EnterP():
    b1 = p.get()
    print (p.get())

def EnterS(*self):
    print (self.s.get())

def EnterB(*args):
    print (b.get())

def EnterN(*args):
    print (n.get())
#-----------------------------------------------#

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want 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 (Home, Population, Quit):
            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("Home")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class Home(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Home Page", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Population",
                        command=lambda: controller.show_frame("Population"))
        button5 = tk.Button(self, text = "Quit",
                        command=lambda: controller.show_frame("Quit"))
        button1.pack()
        button5.pack()


class Population(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Enter Generation 0 Values",     font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        #Population Number
        w = tk.Label(self, text="Enter the value for the Population")
        w.pack()

        p = tk.Entry(self)
        p.pack()

        pb = tk.Button(self, text="OK", command = EnterP)
        pb.pack()

        #Survival Rates
        w = tk.Label(self, text="Enter the value of Survival Rates")
        w.pack()

        s = tk.Entry(self)
        s.pack()

        sb = tk.Button(self, text="OK", command = EnterS)
        sb.pack()

        #Birth Rates
        w = tk.Label(self, text="ENter the value for the Birth Rate")
        w.pack()

        b = tk.Entry(self)
        b.pack()

        bb = tk.Button(self, text="OK", command = EnterB)
        bb.pack()

        #Number of New Generations To Model
        w = tk.Label(self, text="Enter the number of New Generatiions")
        w.pack()

        n = tk.Entry(self)
        n.pack()

        nb = tk.Button(self, text="OK", command = EnterN)
        nb.pack()


        button = tk.Button(self, text="&lt;&lt;&lt; BACK",
                       command=lambda: controller.show_frame("Home"))
        button.pack()



class Quit(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="Are you sure you want to quit?", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)


        yes = tk.Button(self, text="Yes")
        yes.pack()

        no = tk.Button(self, text = "No",
                   command = lambda: controller.show_frame("Home"))
        no.pack()



if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
</code></pre>

<p>I won't try to give a complete answer (for there might be more that one error in your program), but let me tell you what your problem is:</p>

<p>You didn't take into account <strong>variable scopes</strong>. Whenever naming a variable, it is only accessible in a specific region of your code.</p>

<p>In your code, for instance, <code>p</code> is defined inside <code>class Population.__init__</code> scope. <code>EnterP</code> is defined <strong>outside</strong> of this scope, hence cannot access <code>p</code>.</p>

<p>Each language has its own way to handle such scopes. Here is something you should read on this topic:</p>

<p><a href="https://python-textbok.readthedocs.io/en/latest/Variables_and_Scope.html" rel="nofollow">https://python-textbok.readthedocs.io/en/latest/Variables_and_Scope.html</a></p>

<p>Good luck!</p>

python variables tkinter nameerror