【发布时间】:2018-07-16 03:55:48
【问题描述】:
我的代码出现“赋值前引用的局部变量'答案'”错误。我应该如何解决这个问题?
from Tkinter import *
import Tkinter
def proces():
F=Entry.get(E1)
m=Entry.get(E2)
a=Entry.get(E3)
if F ==0:
answer=float(m)*float(a)
if m ==0:
answer=float(F)/float(a)
if a ==0:
answer=float(F)/float(m)
Entry.insert(E4,0,answer)
print(answer)
#And the rest of the GUI coding goes here.
top = Tkinter.Tk()
L1 = Label(top, text="My calculator",).grid(row=0,column=1)
L2 = Label(top, text="Force",).grid(row=1,column=0)
L3 = Label(top, text="Weight",).grid(row=2,column=0)
L4 = Label(top, text="Accelaration",).grid(row=3,column=0)
L4 = Label(top, text="Solution",).grid(row=4,column=0)
E1 = Entry(top, bd =5)
E1.grid(row=1,column=1)
E2 = Entry(top, bd =5)
E2.grid(row=2,column=1)
E3 = Entry(top, bd =5)
E3.grid(row=3,column=1)
E4 = Entry(top, bd =5)
E4.grid(row=4,column=1)
B=Button(top, text ="Solve",command = proces).grid(row=5,column=1,)
top.mainloop()
【问题讨论】:
-
只要在
IF之上定义answer=0 or some vale,想想如果没有一个条件满足答案的值是多少? -
欢迎来到Stack Overflow!请为您提出的问题提供minimal reproducible example,而不是您当前的代码。
标签: python user-interface tkinter