python - 获取用户输入并在 TKinter 中返回答案

标签 python tkinter fibonacci

这是我的第一个问题,对于任何错误,我们深表歉意:S。

我最近学习了Python,并且制作了一些非常简单的基于文本的应用程序。现在我尝试用合适的 GUI 来制作一个。我有下面的代码。我已经制作了 GUI,并且运行良好。除了一个小错误。

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

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

我知道计算斐波那契数的脚本有效,因为我单独测试了它。

这和TKinter有关系吗?

感谢您的帮助:)

最佳答案

以下代码用于从 Entry 获取用户输入,将其发送到函数,然后获取该函数的输出并更新标签的文本:

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

关于python - 获取用户输入并在 TKinter 中返回答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29056597/

相关文章:

python - 无法理解 Python 方法调用中的求值顺序

javascript - 使用 PySide QtCore.Slot 装饰器的多个参数

python-3.x - (Python) 使用滚动条将框架插入 Canvas

python - 在Python中从剪贴板复制内容时出现tkinter错误

scala - 递归 GO 与 Scala

javascript - 我怎样才能改变我的reduce()来对数字求和,直到得到所要求的数字?

python - Pandas 更新后无效的转义序列

python - 使用 Python 将每周数据转换为每日数据

python - 'tearoff' 属性在 tkinter 菜单中有什么作用?

python-2.7 - 在 while 循环中增加列表索引