Entry.get()不返回用户输入的值

时间:2019-02-18 17:56:03

标签: python tkinter

我正在尝试使用python编写数字积分程序,但是使用Entry.get()时,某些用户输入未存储为变量。如何正确使用Entry.get()

我对编码还很陌生,我正在尝试创建一个计算数值积分的程序。集成的代码可以单独使用,但是我试图使用tkinter库制作用户界面。我在给定的行中收到以下错误:

finalValue = ((a-b)/n)*initialValue

ZeroDivisionError: float division by zero

由此,我意识到用户值没有存储在变量中,因此n,a和b都返回零。我通过在用户输入后打印变量来验证这一点。我认为我没有正确使用Entry.get(),但不确定如何使用。我也看过类似的问题和解决方案,但似乎都没有用。


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

2 个答案:

答案 0 :(得分:1)

get()是Entry创建的对象的方法。您可以按以下方法将其称为对象的方法。

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

您的示例尚不完整,因此我无法对其进行测试。您需要添加

window8.mainloop()

这4个获取可能需要包含在IntegratedNumerical中。然后它们将在单击按钮时运行。

HTH

编辑:阅读评论后的进一步说明

我已尝试根据您在上面所做的工作使其工作。我不确定它能满足您的要求。

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

我希望这可以弄清楚我的意思。

答案 1 :(得分:0)

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

用法:

import tkinter as tk
integrateNumericalWindow()