Skip to main content
 首页 » 编程设计

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

2024年10月25日8thcjp

这是我的第一个问题,如有任何错误,我们深表歉意: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 获取用户输入,将其发送到函数,然后获取该函数的输出并更新 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()