Search code examples
tkinterpython-3.4newtons-method

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


I am new to python. My question is: Can I use one button "Estimate" mutiple times to compute my answer? More specifically, the project is to implement Newton's formula into tkinter. If the input is 0 the estimateVar text box is computed to result in 0. If the input is a positive number, i should be able to process the number multiple times using my estimate button as a loop. I have easily been able to get the final result using a nested while loop. However, trying to reuse the estimate button to step into the algorithm incrementally seems impossible. I have tried everything. the closest i have came was setting my inputVar to replicate my estimateVar, but this would obviously result in a logical error since the input needs to remain constant. Also, i have tried setting a global variable for estimate, but python will not allow me to manipulate the estimate and my inputVar returns the global value. The initial value of estimate has to be 1. If anyone has any input it would be greatly appreciated. Here is my code

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

Solution

  • Your goal and problem are not clear, but here is what I think you want based on "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()