Taking input from user and returning an answer in

2019-07-21 00:40发布

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

3条回答
虎瘦雄心在
2楼-- · 2019-07-21 00:48

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()
查看更多
地球回转人心会变
3楼-- · 2019-07-21 00:55

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

查看更多
戒情不戒烟
4楼-- · 2019-07-21 01:13

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

查看更多
登录 后发表回答