Taking input from user and returning an answer in TKinter

偶尔善良 提交于 2019-12-11 03:11:21

问题


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


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



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




回答3:


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



来源:https://stackoverflow.com/questions/29056597/taking-input-from-user-and-returning-an-answer-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!