Reputation: 45
I have been working on very simple calculator with a few functions for a computer science course, and of the calculators functions is solving quadratic equations. I have tried to make it work but I get this error:screenshot of error. I don't understand why it does this since I input all of it.screenshot of input. If anyone could help that would be great.
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()
Upvotes: 2
Views: 58
Reputation: 3342
Add the follow code below sqrt_result = partial(....)
:
algebraMode = partial(algebraMode, labelResult, number1, number2, number3)
Then it should work as your others do.
Upvotes: 1