这是我在这里的第一个问题,对不起任何错误: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有关吗?
谢谢你的帮助 :)
以下代码用于从条目中获取用户输入,将其发送到函数,并获取该函数的输出并更新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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句