从用户那里获取输入并在TKinter中返回答案

时间:2015-03-15 02:28:20

标签: python tkinter fibonacci

这是我在这里的第一个问题,对于任何错误都很抱歉:S。

我最近选择了python,并且我已经制作了一些非常简单的基于文本的应用程序。现在我尝试使用合适的GUI制作一个。我有下面的代码。我已经制作了GUI,它工作正常。除了一点点错误。

这个想法是用户输入一个数字,应用程序将返回一个Fibonacci数字,该数字位于用户指定的序列中的相同位置。但是当我尝试时,显示的只是用户输入的数字,而不是Fibonacci数字。

 ##!/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()

我知道计算Fibonacci数字的脚本是有效的,因为我是单独测试的。

与TKinter有关吗?

感谢您的帮助:)

3 个答案:

答案 0 :(得分:1)

以下代码用于从Entry中获取用户输入,将其发送到函数,并获取该函数的输出并更新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()

剩下的就是插入/修改你的fibonacci()函数。请注意,您当前的函数,并且您当前的实现没有按照您的需要更新标签。


编辑同样的想法,只是清理了一下:

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

答案 1 :(得分:1)

好的伙计们,我解决了。非常感谢:)

我使用了Binet的公式。

以下是工作代码:

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:计算器可以计算的最大数字是第1474个Fibonacci数。

非常感谢您的帮助:)

答案 2 :(得分:0)

我弄错了,在clicked1函数中,你使用self.label.configure(text = digits)而不是self.label.configure(text = fibbonacci(digits))