我试图用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()
用法:
get()是由Entry创建的对象的方法。您可以如下所示调用它作为对象的方法
你的例子不完整,所以我无法测试。你需要包括
4个get可能需要包含在积算中。然后单击按钮时它们将运行
HTH公司
编辑:阅读评论后作进一步解释
我试着让它在你上面所做的基础上工作。我不确定它是否符合你的要求
我希望这能澄清我的意思
相关问题 更多 >
编程相关推荐