使用Tkinter的Python BMI程序
我刚开始使用Tkinter,到目前为止没遇到太多问题。现在我主要遇到的麻烦是,我在程序中应该使用tk__init__(self),我确实用了,它能正常计算所有内容。不过,当我运行程序时,出现了两个弹出框,而不是一个。我还想把主框拉伸一下,这样标题就能完整显示。以下是我的代码:
from Tkinter import *
class App(Tk):
def __init__(self):
self.root = Tk()
self.root.title("BMI Calculator")
Tk.__init__(self)
# Sets a label and entry field into the window for weight, height in
# feet, and height in inches
self.label = Label(self.root, text="Enter your weight in pounds.").pack()
self.lbs = StringVar()
Entry(self.root, textvariable=self.lbs).pack()
self.label = Label(self.root, text="Enter your height in feet.").pack()
self.feet = StringVar()
Entry(self.root, textvariable=self.feet).pack()
self.label = Label(self.root, text="Enter your height in inches.").pack()
self.inches = StringVar()
Entry(self.root, textvariable=self.inches).pack()
# Sets a button and label to click and calculate BMI
self.buttontext = StringVar()
Button(self.root, textvariable=self.buttontext, command=self.calculate).pack()
self.buttontext.set("Calculate")
# Sets bmi_num to a StringVar so that when it is changed, the label will
# update
self.bmi_num = StringVar()
Label(self.root, textvariable=self.bmi_num).pack()
# Same thing here
self.bmi_text = StringVar()
Label(self.root, textvariable=self.bmi_text).pack()
self.root.mainloop()
def calculate(self):
# Retrieves all necessary information to calculate BMI
weight = float(self.lbs.get())
feet = float(self.feet.get())
inches = float(self.inches.get())
height = (feet*12)+inches
bmi = float((weight*703)/(height**2))
# Updates the status label
self.bmi_num.set("Your BMI is %.2f" % bmi)
if bmi < 18.5:
self.bmi_text.set("You are underweight")
if 18.5 <= bmi < 25:
self.bmi_text.set("You are normal")
if 25 <= bmi < 30:
self.bmi_text.set("You are overweight")
if 30<= bmi > 30:
self.bmi_text.set("You are obese")
App()
如果有更好的写法或者解决这些问题的建议,那就太好了。谢谢!
2 个回答
0
请注意,这看起来是我教的一门课的作业。如果你想把这个例子当成自己的,请注意两点。
- 这个代码有一些问题,所以复制它并不是个好主意。
- 我的助教和我都很清楚这个代码在网上,所以你很可能会被发现。
3
看起来你在这里把继承和封装搞混了。你的 App 类是从 Tk 继承而来的,这意味着它就是一个 Tk 对象。不过,你在 App 里面又封装了一个 Tk 对象,并把它赋值给了 self.root。所以你有一个 App,它是 Tk 的子类,同时又封装了一个 Tk 对象在 App.root 里面。
实际上,你是在 self.root 这个 Tk 实例上构建你的图形界面,但你又调用了 Tk.__init__(self)
,这会初始化一个空的 Tk 对象(所以会出现第二个窗口)。你需要选择一种方式来处理:要么 (1) 不从 Tk 继承,不调用 Tk.__init__(self)
,只使用 self.root,或者 (2) 完全不创建 self.root
对象,调用 Tk.__init__(self)
,然后在你现在使用 self.root
的地方直接使用 self
。