[英]I keep reveicing the error “NameError: name 'labelAns' is not defined”,
Basically all I have to do is change the text on labelAns
to a value that I insert by pressing a button, but I keep receiving an error and I don't know how I could fix it. 基本上,我要做的就是将
labelAns
上的文本更改为通过按下按钮插入的值,但是我一直收到错误,而且我不知道该如何解决。
All I need to do now is change the label on command. 我现在要做的就是在命令上更改标签。
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Differentiation Calculator", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Main Menu",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text="Integration",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
self.num = tk.IntVar()
self.entry = tk.Entry(self, textvariable=self.num)
self.button = tk.Button(self, text='Differentiate', command=self.calc)
labelAns = tk.Label(self,text = "0", width = 15)
labelAns.pack(side = 'right')
self.entry.pack(side = 'left')
self.button.pack(side = 'left')
def calc(self):
Jack = (self.entry.get())
x = sp.Symbol('x')
res = (sp.diff(Jack,x))
global labelAns
labelAns(text = res)
print(res)
I think what you need to do is call labelAns
global in the initial declaration of the variable. 我认为您需要做的是在变量的初始声明中调用
labelAns
global。 You should be able to change 你应该能够改变
labelAns = tk.Label(self,text = "0", width = 15)
into 进入
global labelAns = tk.Label(self,text = "0", width = 15)
and it should work then. 然后它应该工作。 Also, you should be able to reference
labelAns
afterwards without using global labelAns
. 另外,您以后应该可以引用
labelAns
,而无需使用global labelAns
。
You're better off doing something like this, so you'll have an easy reference to your label: 最好这样做,这样您就可以轻松参考标签:
class PageOne(tk.Frame):
def __init__(self, parent, controller):
...
self.labelAns = tk.Label(self,text = "0", width = 15)
self.labelAns.pack(side = 'right')
...
def calc(self):
...
self.labelAns['text'] = res
...
Also notice how I changed the text for label self.labelAns
. 还要注意我如何更改标签
self.labelAns
的文本。 I don't think what you have will work--it's not something I've seen before (although it doesn't mean it wouldn't work). 我认为您所拥有的不会起作用-这不是我以前见过的(尽管这并不意味着它不会起作用)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.