I'm trying to code a numerical integration program in python, however some of the user inputs aren't storing as variables, when using Entry.get(). How do I use Entry.get() correctly?
I'm fairly new to coding and I'm trying to create a program that computes numerical integration. The code for the integration works on it's own, however I'm trying to make the user interface using the tkinter library. I'm getting the following error in the given line:
finalValue = ((a-b)/n)*initialValue
ZeroDivisionError: float division by zero
From this I realised that the user values were not getting stored in the variables, and therefore n, a and b were all returning zero. I verified this by printing the variables after the user inputs. I think I've used Entry.get() incorrectly, but I'm not sure how. I've also had a look at similar problems and solutions, but none of them seem to work.
def integrateNumerical(n, b, a):
def f(x): #Defines the function to be integrated
return eval(numericalFunction)
initialValue = 0 #Sets the initial iterative value
finalValue = 0 #Sets the final iterative value
for i in range(1, n+1):
initialValue = initialValue + f(b+((i-(1/2))*((a-b)/n)))
finalValue = ((a-b)/n)*initialValue
return finalValue
def integrateNumericalWindow():
window8 = Toplevel(window)
window8.title("Numerical Integration")
window8.geometry("400x400")
iterationNumber = IntVar()
upperBound = IntVar()
lowerBound = IntVar()
functionNumerical = StringVar()
Label(window8, text = "").pack()
Label(window8, text = "Number of iterations: ").pack()
iterationNumberEntry = Entry(window8, textvariable = iterationNumber)
iterationNumberEntry.pack()
Label(window8, text = "").pack()
Label(window8, text = "Upper bound: ").pack()
upperBoundEntry = Entry(window8, textvariable = upperBound)
upperBoundEntry.pack()
Label(window8, text = "").pack()
Label(window8, text = "Lower bound: ").pack()
lowerBoundEntry = Entry(window8, textvariable = lowerBound)
lowerBoundEntry.pack()
Label(window8, text = "").pack()
Label(window8, text = "Function: ").pack()
functionNumericalEntry = Entry(window8, textvariable = functionNumerical)
functionNumericalEntry.pack()
global n
global a
global b
global numericalFunction
n = int(Entry.get(iterationNumberEntry))
a = float(Entry.get(upperBoundEntry))
b = float(Entry.get(lowerBoundEntry))
numericalFunction = str(Entry.get(functionNumericalEntry))
Label(window8, text = "").pack()
Button(window8, text = "Integrate", width = 10, height = 1, bd = "0", fg = "#383a39", bg = "#a1dbcd", command = lambda : integrateNumerical(n, b, a)).pack()