为什么找不到变量?

Why does it not find the variable?

所以下面是我的代码,我需要从 Entry 小部件中获取变量 'p',将其设置为新变量名称,然后打印它。出于某种原因,我收到以下错误“NameError: name 'p' is not defined”。我完全不知道如何修复它,这是我最后的选择。请帮助我。

代码:

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="<<< 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()

像这样修改你的代码:

class Population(tk.Frame):

    def __init__(self, parent, controller):
        def EnterP():
            b1 = p.get()
            print (p.get())

我不会尝试给出一个完整的答案(因为你的程序中可能有不止一个错误),但让我告诉你你的问题是什么:

您没有考虑变量范围。每当命名变量时,它只能在代码的特定区域访问。

例如,在您的代码中,p 是在 class Population.__init__ 范围内定义的。 EnterP 定义在此范围 之外 ,因此无法访问 p

每种语言都有自己处理此类范围的方式。以下是您应该阅读的有关此主题的内容:

https://python-textbok.readthedocs.io/en/latest/Variables_and_Scope.html

祝你好运!