我一直在研究非常简单的计算器,该计算器具有一些用于计算机科学课程的功能,并且其中的计算器功能是求解二次方程。我试图使其工作,但出现此错误:screenshot of error。我不明白为什么这样做,因为我输入了所有内容。screenshot of input。如果有人可以帮助,那就太好了。
import tkinter as tk
import math
from functools import partial
#defining functions for operations
#Addition
def add_result(label_result, n1, n2):
num1 = (n1.get())
num2 = (n2.get())
result = float(num1)+float(num2)
label_result.config(text="Result is %d" % result)
return
#Multiplication
def mult_result(label_result, n1, n2):
num1 = (n1.get())
num2 = (n2.get())
result = float(num1)*float(num2)
label_result.config(text="Result is %d" % result)
return
#Subtraction
def sub_result(label_result, n1, n2):
num1 = (n1.get())
num2 = (n2.get())
result = float(num1)-float(num2)
label_result.config(text="Result is %d" % result)
return
#Division
def div_result(label_result, n1, n2):
num1 = (n1.get())
num2 = (n2.get())
result = float(num1)/float(num2)
label_result.config(text="Result is %d" % result)
return
#SquareRoot
def sqrt_result(label_result, n1):
num1 = (n1.get())
result = math.sqrt(float(num1))
label_result.config(text="Result is %d" % result)
return
def algebraMode(label_result, n1, n2, n3):
num1 = (n1.get())
num2 = (n2.get())
num3 = (n3.get())
sol1 = (-float(num2) + math.sqrt(float(num2)**2 - 4*float(num1)*float(num3))) / (2 * float(num1))
sol2 = (-float(num2) - math.sqrt(float(num2)**2 - 4*float(num1)*float(num3))) / (2 * float(num1))
label_result.config(text="The solutions are %d" % sol1 + " %d" % sol2)
return
root = tk.Tk()
root.geometry('450x170+100+200')
root.title('Simple Calculator')
number1 = tk.StringVar()
number2 = tk.StringVar()
number3 = tk.StringVar()
labelNum1 = tk.Label(root, text="Enter a number").grid(row=2, column=1)
labelNum2 = tk.Label(root, text="Enter another number").grid(row=3, column=1)
labelNum3 = tk.Label(root, text="Enter another number (Algebra Mode Only)").grid(row=4, column=1)
labelResult = tk.Label(root)
labelResult.grid(row=7, column=2)
entryNum1 = tk.Entry(root, textvariable=number1).grid(row=2, column=2)
entryNum2 = tk.Entry(root, textvariable=number2).grid(row=3, column=2)
entryNum3 = tk.Entry(root, textvariable=number3).grid(row=4, column=2)
add_result = partial(add_result, labelResult, number1, number2)
mult_result = partial(mult_result, labelResult, number1, number2)
sub_result = partial(sub_result, labelResult, number1, number2)
div_result = partial(div_result, labelResult, number1, number2)
sqrt_result = partial(sqrt_result, labelResult, number1, number2)
buttonCal = tk.Button(root, text="Add", command=add_result).grid(row=1, column=0)
buttonCal = tk.Button(root, text="Multiply", comma=mult_result).grid(row=2, column=0)
buttonCal = tk.Button(root, text="Subtract", command=sub_result).grid(row=3, column=0)
buttonCal = tk.Button(root, text="Divide", command=div_result).grid(row=4, column=0)
buttonCal = tk.Button(root, text="Square Root", command=sqrt_result).grid(row=5, column=0)
buttonCal = tk.Button(root, text="Algebra", command=algebraMode).grid(row=6, column=0)
root.mainloop()
最佳答案
在sqrt_result = partial(....)
下面添加以下代码:
algebraMode = partial(algebraMode, labelResult, number1, number2, number3)
然后它应该像您的其他人一样工作。
关于python - 无法获得代数(二次方程式),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51508771/