我是编程新手,开始使用Python。我正在尝试用5个操作(到目前为止我只做3个)做一个简单的计算器,可以执行PEMDAS。我的代码的问题是它以前可以工作,但是当我不断添加行时,在插入条目时出现错误。
when performing addition + multiplication
MA() function which is working
when performing multiplication only
这是部分代码:
from Tkinter import *
main=Tk()
main.title("PEMDAS Calculator")
Grid.rowconfigure(main, 0, weight=1)
Grid.columnconfigure(main, 0, weight=1)
entry= Entry(main, width=50, bg='#DFDEDB')
label=Label(main)
frame= Frame(main)
secondFrame= Frame(main)
thirdFrame= Frame(main)
fourthFrame= Frame(main)
fifthFrame= Frame(main)
label.grid(row=1)
entry.grid(row=2, column=0)
frame.grid(row=4, column=0)
secondFrame.grid(row=5, column=0)
thirdFrame.grid(row=6, column=0)
fourthFrame.grid(row=7, column=0)
fifthFrame.grid(row=8, column=0)
#Label
intro=Label(label, text="Welcome to Calculator!")
intro.grid(row=0)
def buttons(number):
prior= entry.get()
entry.delete(0,END)
entry.insert(0, str(prior)+str(number))
def addition():
buttons('+')
def multiplication():
buttons('*')
def subtraction():
buttons('-')
def division():
buttons('/')
def equal():
eq=entry.get()
entry.delete(0,END)
if '+' and '*' in eq:
def MA():
while '+' in eq:
addEl=eq.split('+')
toMultiply=[]
numbers=[]
for num in addEl:
if '*' in num:
mulEl=num.split("*")
for element in mulEl:
toMultiply.append(element)
else:
numbers.append(int(num))
def multiply():
prod = 1
for element in toMultiply:
total = prod * int(element)
prod = total
return prod
numbers.append(multiply())
return sum(numbers)
entry.insert(0, MA())
elif '+' and '-' in eq:
el = eq.split('+')
numbers=[]
def addsub():
for num in el:
if '-' in num:
def sub():
subEl=num.split('-')
dif = 0
subEl[0] = -(int(subEl[0]))
for dig in subEl:
dif -= int(dig)
return dif
numbers.append(sub())
else:
numbers.append(int(num))
return sum(numbers)
entry.insert(0, addsub())
elif '+' in eq:
el=eq.split('+')
def add():
total=[]
for num in el:
total.append(int(num))
return total
entry.insert(0, sum(add()))
elif '*' in eq:
def multi():
el = eq.split('*')
prod = 1
for num in el:
total=prod*int(num)
prod=total
return prod
entry.insert(0, multi())
elif '-' in eq:
el=eq.split('-')
def subtract():
dif=0
el[0] = -(int(el[0]))
for num in el:
dif -= int(num)
return dif
entry.insert(0, subtract())
one=Button(frame, text=1, padx=20, pady=20, command=lambda: buttons(1))
two=Button(frame, text=2, padx=20, pady=20, command=lambda: buttons(2))
three=Button(frame, text=3, padx=20, pady=20, command=lambda: buttons(3))
space=Label(frame, text=" ")
add=Button(frame, padx=20, pady=20, text="+", command=addition)
subtract=Button(frame, padx=20, pady=20, text="-", command=subtraction)
one.grid(row=4, column=0)
two.grid(row=4, column=1)
three.grid(row=4, column=2)
space.grid(row=4, column=3)
add.grid(row=4, column=4)
subtract.grid(row=4, column=5)
four=Button(secondFrame, text=4, padx=20, pady=20, command=lambda: buttons(4))
five=Button(secondFrame, text=5, padx=20, pady=20, command=lambda: buttons(5))
six=Button(secondFrame, text=6, padx=20, pady=20, command=lambda: buttons(6))
space=Label(secondFrame, text=" ")
multiply=Button(secondFrame, text="*", padx=21.5, pady=20, command=multiplication)
divide=Button(secondFrame, text="/", padx=19, pady=20, command=division)
four.grid(row=5, column=0)
five.grid(row=5, column=1)
six.grid(row=5, column=2)
space.grid(row=5, column=3)
multiply.grid(row=5, column=4)
divide.grid(row=5, column=5)
seven=Button(thirdFrame, text=7, padx=20, pady=20, command=lambda: buttons(7))
eight=Button(thirdFrame, text=8, padx=20, pady=20, command=lambda: buttons(8))
nine=Button(thirdFrame, text=9, padx=20, pady=20, command=lambda: buttons(9))
space=Label(thirdFrame, text=" ")
sqrt=Button(thirdFrame, text="sqrt", padx=41, pady=20, command=lambda: buttons('sqrt'))
seven.grid(row=6, column=0)
eight.grid(row=6, column=1)
nine.grid(row=6, column=2)
space.grid(row=6, column=3)
sqrt.grid(row=6, column=4)
space1=Label(fourthFrame, text=" ")
zero=Button(fourthFrame, text=0, padx=20, pady=20, command=lambda: buttons(0))
space=Label(fourthFrame, text=" ")
eq=Button(fourthFrame, text="=", padx=47, pady=20, command=equal)
space1.grid(row=7,column=0)
zero.grid(row=7, column=1)
space.grid(row=7, column=3)
eq.grid(row=7, column=5)
main.mainloop()
答案 0 :(得分:0)
如果MA()
返回None
,则会出现此错误。您无法将python值None
插入条目小部件。