我是python的新手。我的问题是:我可以使用一个按钮" Estimate"多次计算我的答案?更具体地说,该项目是将牛顿公式实施到tkinter中。如果输入为0,则计算estimateVar文本框以产生0.如果输入是正数,我应该能够使用我的估计按钮作为循环多次处理该数字。我很容易使用嵌套的while循环获得最终结果。但是,尝试重复使用估计按钮逐步进入算法似乎是不可能的。我尝试了一切。我最接近的是设置我的inputVar来复制我的estimateVar,但这显然会导致逻辑错误,因为输入需要保持不变。此外,我已经尝试设置一个全局变量用于估计,但python将不允许我操纵估计,我的inputVar返回全局值。估计的初始值必须为1.如果任何人有任何输入,将非常感激。这是我的代码
'''
Created on Nov 11, 2015
@author: lz206729
'''
from tkinter import *
class newtonGUI(Frame):
def __init__(self):
#Sets up window
Frame.__init__(self)
self.master.title("Newton's Square")
self.grid(column = 0, row = 0)
#Label and field for number input
self._inputLabel = Label(self, text = "Input a positive number to square: ")
self._inputLabel.grid(row = 0, column = 0)
self._inputVar = DoubleVar()
self._inputEntry = Entry(self, textvariable = self._inputVar)
self._inputEntry.grid(row = 0, column = 1)
#Displays the common square root
self._estimateLabel = Label(self, text = "The estimate is : ")
self._estimateLabel.grid(row = 1, column = 0)
self._estimateVar = DoubleVar()
self._estimateEntry = Entry(self, textvariable = self._estimateVar)
self._estimateEntry.grid(row = 1, column = 1)
#Button that computes the input
self._estimateButton = Button(self, text = "Estimate", command = self._newtonSquare)
self._estimateButton.grid(row = 4, column = 0)
#Button that resets text boxes
self._resetButton = Button(self, text = "Reset", command = self._reset)
self._resetButton.grid(row = 4, column = 1)
def _newtonSquare(self):
tolerance = 0.000001
estimate = 1
x = self._inputVar.get()
if x == 0:
self._estimateVar.set(x / 2)
else:
while True:
estimate = (estimate + x / estimate)/2
difference = abs(x - estimate **2)
if difference <= tolerance:
break
self._estimateVar.set(estimate)
def _reset(self):
self._inputVar.set(0)
self._estimateVar.set(0)
self._estimateButton.config(state='normal')
def main():
newtonGUI().mainloop()
main()
答案 0 :(得分:0)
您的目标和问题尚不清楚,但根据“估算按钮逐步进入算法”,这就是我认为您想要的内容。
from tkinter import *
class newtonGUI(Frame):
def __init__(self, master):
#Set up frame
Frame.__init__(self, master)
self.master.title("Newton's Squareroot")
self.grid(column = 0, row = 0)
#Label and field for number input
self._inputLabel = Label(self, text = "Input a positive number")
self._inputLabel.grid(row = 0, column = 0)
self._inputVar = DoubleVar()
self._inputVar.set(1.0)
self._inputEntry = Entry(self, textvariable = self._inputVar)
self._inputEntry.grid(row = 0, column = 1)
#Display the common square root
self._estimateLabel = Label(self, text = "Square root estimate")
self._estimateLabel.grid(row = 1, column = 0)
self._estimateVar = DoubleVar()
self._estimateVar.set(1.0)
self._estimateEntry = Label(self, textvariable = self._estimateVar)
self._estimateEntry.grid(row = 1, column = 1)
#Button that computes the input
self._estimateButton = Button(self, text = "Estimate", command = self._newtonSquare)
self._estimateButton.grid(row = 4, column = 0)
#Button that resets text boxes
self._resetButton = Button(self, text = "Reset", command = self._reset)
self._resetButton.grid(row = 4, column = 1)
def _newtonSquare(self):
val = self._inputVar.get()
est = self._estimateVar.get()
if val <= 0.0:
self._estimateVar.set(0.0)
else:
self._estimateVar.set((est + val / est) / 2)
def _reset(self):
self._inputVar.set(1.0)
self._estimateVar.set(1.0)
def main():
root = Tk()
newtonGUI(root)
root.mainloop()
main()
答案 1 :(得分:0)
谢谢你, 绝对让我朝着正确的方向前进。我甚至没想过将我的初始值设置为1。起初,代码只执行公共方形值,所以我修改了它。再次感谢。这是最终的计划。
from tkinter import *
class newtonGUI(Frame):
def __init__(self, master):
#Set up frame
Frame.__init__(self, master)
self.master.title("Newton's Squareroot")
self.grid(column = 0, row = 0)
#Label and field for number input
self._inputLabel = Label(self, text = "Input a positive number")
self._inputLabel.grid(row = 0, column = 0)
self._inputVar = DoubleVar()
self._inputVar.set(1.0)
self._inputEntry = Entry(self, textvariable = self._inputVar)
self._inputEntry.grid(row = 0, column = 1)
#Display the common square root
self._estimateLabel = Label(self, text = "Square root estimate")
self._estimateLabel.grid(row = 1, column = 0)
self._estimateVar = DoubleVar()
self._estimateVar.set(1.0)
self._estimateEntry = Label(self, textvariable = self._estimateVar)
self._estimateEntry.grid(row = 1, column = 1)
#Button that computes the input
self._estimateButton = Button(self, text = "Estimate", command = self._newtonSquare)
self._estimateButton.grid(row = 4, column = 0)
#Button that resets text boxes
self._resetButton = Button(self, text = "Reset", command = self._reset)
self._resetButton.grid(row = 4, column = 1)
def _newtonSquare(self):
value = self._inputVar.get()
estimate = self._estimateVar.get()
tolerance = 0.000001
if value <= 0.0:
self._estimateVar.set(0.0)
else:
estimate = (estimate + value / estimate) / 2
difference = abs(value - estimate **2)
if difference <= tolerance:
self._estimateVar.set(estimate)
self._estimateButton.config(state = 'disabled')
else:
self._estimateVar.set(estimate)
def _reset(self):
self._inputVar.set(1.0)
self._estimateVar.set(1.0)
def main():
root = Tk()
newtonGUI(root)
root.mainloop()
main()