从用户那里获取输入并在TKinter中返回答案 [英] Taking input from user and returning an answer in TKinter
问题描述
这是我在这里的第一个问题,对于任何错误,我们深表歉意.
This is my first question here so sorry for any mistakes :S.
我最近选择了python,并制作了一些非常简单的基于文本的应用程序.现在,我尝试使用适当的GUI制作一个.我有下面的代码.我已经制作了GUI,并且工作正常.除了一个小错误.
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.
与TKinter有关吗?
Is it something to do with TKinter?
感谢您的帮助:)
推荐答案
以下代码可用于从Entry中获取用户输入,将其发送给函数,并获取该函数的输出并更新Label的文本:
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()
剩下的就是插入/修改您的fibonacci()函数.请注意,您当前的函数无效起作用,并且您当前的实现未按需要更新标签.
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()
这篇关于从用户那里获取输入并在TKinter中返回答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!