user11080418
user11080418

Reputation:

Entry.get() doesn't return user inputted values

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()

Upvotes: 0

Views: 67

Answers (2)

Tls Chris
Tls Chris

Reputation: 3824

get() is a method of the object created by Entry. You call it as below as a method of the object.

n = int(iterationNumberEntry.get())
a = float(upperBoundEntry.get())
b = float(lowerBoundEntry.get())
numericalFunction = str(functionNumericalEntry.get())

Your example isn't complete so I've not been able to test this. You need to include

window8.mainloop()

The 4 gets probably need to be included in integrateNumerical. then they will run when the button is clicked.

HTH

Edit: Further explanation after reading the comment

I've tried to make it work based on what you had done above. I'm not certain it does what you want.

from tkinter import *

def integrateNumericalWindow():

    window8 = Tk()
    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()

    Label(window8, text = "").pack()

    Label(window8, text="Result :").pack()
    result=Label(window8, text="None")
    result.pack()

    # I've moved the command function to here so it can 'see' all the variables it needs
    def integrateNumerical():
        # The 4 Entry widgets are read in this function when the button is clicked.
        n = int(iterationNumberEntry.get())
        a = float(upperBoundEntry.get())
        b = float(lowerBoundEntry.get())
        numericalFunction = str(functionNumericalEntry.get())

        def f(x): #Defines the function to be integrated
            return eval(numericalFunction)

        print(n, a, b, numericalFunction, f(3.0) )

        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

        result.configure(text=str(finalValue)) # Set a label with the result instead of returning a result.
        # return finalValue

    Button(window8, text = "Integrate", width = 10, height = 1, bd = "0", fg = "#383a39", 
    bg = "#a1dbcd", command=integrateNumerical).pack()

    window8.mainloop()

integrateNumericalWindow()

i hope this clarifies what I meant.

Upvotes: 1

AbdulaziAlswaji
AbdulaziAlswaji

Reputation: 31

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 = tk.Toplevel()
    window8.title("Numerical Integration")
    window8.geometry("400x400")

    iterationNumber = tk.IntVar()
    upperBound = tk.IntVar()
    lowerBound = tk.IntVar()
    functionNumerical = tk.StringVar()

    tk.Label(window8, text = "").pack()
    tk.Label(window8, text = "Number of iterations: ").pack()
    iterationNumberEntry = tk.Entry(window8, textvariable = iterationNumber)
    iterationNumberEntry.pack()

    tk.Label(window8, text = "").pack()
    tk.Label(window8, text = "Upper bound: ").pack()
    upperBoundEntry = tk.Entry(window8, textvariable = upperBound)
    upperBoundEntry.pack()

    tk.Label(window8, text = "").pack()
    tk.Label(window8, text = "Lower bound: ").pack()
    lowerBoundEntry = tk.Entry(window8, textvariable = lowerBound)
    lowerBoundEntry.pack()

    tk.Label(window8, text = "").pack()
    tk.Label(window8, text = "Function: ").pack()
    functionNumericalEntry = tk.Entry(window8, textvariable = functionNumerical)
    functionNumericalEntry.pack()

    global n
    global a
    global b
    global numericalFunction

    n = int(tk.Entry.get(iterationNumberEntry))
    a = float(tk.Entry.get(upperBoundEntry))
    b = float(tk.Entry.get(lowerBoundEntry))
    numericalFunction = str(tk.Entry.get(functionNumericalEntry))

    tk.Label(window8, text = "").pack()
    tk.Button(window8, text = "Integrate", width = 10, height = 1, bd = "0", fg = "#383a39", bg = "#a1dbcd", command = lambda : integrateNumerical(n, b, a)).pack()
    window8.mainloop()

Usage:

import tkinter as tk
integrateNumericalWindow()

Upvotes: 0

Related Questions