Skip to content Skip to sidebar Skip to footer

Taking Input From User And Returning An Answer In TKinter

This is my first question here so sorry for any mistakes :S. I have recently picked up python, and I have made some very simple text based application. Now I tried to make one with

Solution 1:

The following code works to take user input from an Entry, send it to a function, and take the output of that function and update the text of the Label:

import Tkinter as Tk

class App(object):
    def __init__(self):
        self.root = Tk.Tk()
        self.root.wm_title("Fibonacci Calculator")
        #self.root.wm_iconbitmap("@icon2.xbm")
        self.label = Tk.Label(self.root, text="Set the digit number you want.")
        self.label.pack()
        self.digits = Tk.StringVar()
        Tk.Entry(self.root, textvariable=self.digits).pack()

        self.buttontext = Tk.StringVar()
        self.buttontext.set("Calculate")
        Tk.Button(self.root,
                  textvariable=self.buttontext,
                  command=self.clicked1).pack()

        self.label = Tk.Label(self.root, text=" ")
        self.label.pack()

        self.root.mainloop()

    def fibonacci(self, idx):
        # Your function here
        # Make sure this function accepts as its only argument the index
        return idx ** 2

    def clicked1(self):
        idx = int(self.digits.get())
        out = self.fibonacci(idx)
        self.label['text'] = out

    def button_click(self, e):
        pass

App()

All that's left is to insert / modify your fibonacci() function. Note your current function doesn't work, and your current implementation didn't update the label as you wanted.


Edit Same idea, just cleaned up a bit:

import Tkinter as Tk

class App(object):
    def __init__(self):
        self.root = Tk.Tk()
        self.root.wm_title("Fibonacci Calculator")
        #self.root.wm_iconbitmap("@icon2.xbm")
        Tk.Label(self.root, text="Set the digit number you want.").pack()

        self.digits = Tk.StringVar()
        Tk.Entry(self.root, textvariable=self.digits).pack()
        Tk.Button(self.root, text="Calculate", command=self.clicked).pack()

        self.result = Tk.Label(self.root, text=" ")
        self.result.pack()

        self.root.mainloop()

    @staticmethod
    def fibonacci(idx):
        # Your function here
        # Make sure this function accepts as its only argument the index
        return idx ** 2

    def clicked(self):
        idx = int(self.digits.get())
        out = App.fibonacci(idx)
        self.result['text'] = out


App()

Solution 2:

OK guys, I solved it. Thanks a lot :)

I used the Binet's Formula.

Here is the working code:

import Tkinter as Tk

class App(object):
    def __init__(self):
        self.root = Tk.Tk()
        self.root.wm_title("Fibonacci Calculator")
        self.root.wm_iconbitmap("@icon2.xbm")
        Tk.Label(self.root, text="Set the digit number you want.").pack()

        self.digits = Tk.StringVar()
        Tk.Entry(self.root, textvariable=self.digits).pack()
        Tk.Button(self.root, text="Calculate", command=self.clicked).pack()

        self.result = Tk.Label(self.root, text=" ")
        self.result.pack()

        self.root.mainloop()

    @staticmethod
    def fibonacci(idx):
         phi = (1 + 5**0.5)/2.0
         return int(round((phi**idx - (1-phi)**idx) / 5**0.5))


    def clicked(self):
        idx = int(self.digits.get())
        out = App.fibonacci(idx)
        self.result['text'] = out


App()

PS: The largest number the calculator can compute is the 1474th Fibonacci number.

Thanks a lot for your help :)


Solution 3:

I got your mistake, in the function clicked1, you use self.label.configure (text=digits) instead of self.label.configure(text=fibbonacci (digits))


Post a Comment for "Taking Input From User And Returning An Answer In TKinter"