牛顿公式使用tkinter/using一个按钮进行多次计算

Newton's square formula using tkinter/using a button for computations multiple times

我是 python 的新手。我的问题是:我可以多次使用一个按钮 "Estimate" 来计算我的答案吗?更具体地说,该项目是将牛顿公式实施到 tkinter 中。如果输入为 0,则计算 estimateVar 文本框的结果为 0。如果输入为正数,我应该能够使用我的估计按钮作为循环多次处理该数字。我很容易使用嵌套的 while 循环获得最终结果。然而,尝试重新使用估算按钮来逐步进入算法似乎是不可能的。我已经尝试了一切。我最接近的是设置我的 inputVar 来复制我的 estimateVar,但这显然会导致逻辑错误,因为输入需要保持不变。另外,我已经尝试为估计设置一个全局变量,但是 python 不允许我操纵估计和我的 inputVar returns 全局值。估计的初始值必须为 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()

你的目标和问题不明确,但根据"estimate button to step into the algorithm incrementally"我认为你想要的是。

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。起初代码只执行公共平方值,所以我修改了它。再次感谢。这是最后的程序。

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