以下是我的代码,我需要从Entry小部件中获取变量'p',将其设置为新的变量名称,打印它。由于某种原因,我收到以下错误'NameError:name'p'未定义'。我完全不知道如何修复它,这是我的最后一招。请帮帮我。
代码:
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()
答案 0 :(得分:0)
我不会尝试给出完整的答案(因为程序中可能存在更多错误),但让我告诉您问题所在:
您没有考虑变量范围。无论何时命名变量,只能在代码的特定区域中访问它。
例如,在您的代码中,p
在class Population.__init__
范围内定义。 EnterP
在此范围内定义为 之外,因此无法访问p
。
每种语言都有自己的方式来处理这些范围。您应该阅读以下主题:
https://python-textbok.readthedocs.io/en/latest/Variables_and_Scope.html
祝你好运!答案 1 :(得分:-1)
修改你的代码:
class Population(tk.Frame):
def __init__(self, parent, controller):
def EnterP():
b1 = p.get()
print (p.get())