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 a proper GUI. I have the code bellow. I have made the GUI, and it's working fine. Apart of a little mistake.
The idea is that the user enters a number and the app will return a Fibonacci number that lies at the same position in the sequence as specified by the user. But when I try it out, all that is shown is the number the user put in, not the Fibonacci number.
##!/usr/bin/env python
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()
def fibonacci(n):
a = 0
b = 1
temp = a
a = b
b = temp + b
text_display = fibonacci(self.digits)
self.label = Tk.Label(self.root, text=text_display)
self.label.pack()
self.root.mainloop()
def clicked1(self):
digits = self.digits.get()
self.label.configure(text=digits)
def button_click(self, e):
pass
App()
I know the script to calculate the Fibonacci numbers works because I tested it separately.
Is it something to do with TKinter?
Thanks for any help :)
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:
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:
OK guys, I solved it. Thanks a lot :)
I used the Binet's Formula.
Here is the working code:
PS: The largest number the calculator can compute is the 1474th Fibonacci number.
Thanks a lot for your help :)
I got your mistake, in the function clicked1, you use self.label.configure (text=digits) instead of self.label.configure(text=fibbonacci (digits))