使用Tkinter的Python BMI程序

使用Tkinter的Python BMI程序,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,我刚开始使用Tkinter,到目前为止还没有太多问题。我遇到的主要问题是,我应该在程序中使用tk__init____;(self)),它可以很好地计算所有内容。然而,当我运行程序时,我会得到两个弹出框,而不仅仅是一个。我还试图拉伸主框,使其完全显示标题。这是我的密码: from Tkinter import * class App(Tk): def __init__(self): self.root = Tk() self.root.title("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()
从Tkinter导入*
类应用程序(Tk):
定义初始化(自):
self.root=Tk()
self.root.title(“BMI计算器”)
Tk.\uuuuuu初始(自我)
#在窗口中设置“重量”、“高度”的标签和输入字段
#英尺和高度(英寸)
self.label=label(self.root,text=“以磅为单位输入体重。”).pack()
self.lbs=StringVar()
条目(self.root,textvariable=self.lbs).pack()
self.label=label(self.root,text=“以英尺为单位输入身高。”).pack()
self.feet=StringVar()
条目(self.root,textvariable=self.feet).pack()
self.label=label(self.root,text=“以英寸为单位输入您的高度。”).pack()
self.inches=StringVar()
条目(self.root,textvariable=self.inches).pack()
#设置按钮和标签以单击并计算BMI
self.buttontext=StringVar()
按钮(self.root,textvariable=self.buttontext,command=self.calculate).pack()
self.buttontext.set(“计算”)
#将bmi_num设置为StringVar,以便在更改时,标签将
#更新
self.bmi_num=StringVar()
标签(self.root,textvariable=self.bmi_num).pack()
#这里也一样
self.bmi_text=StringVar()
标签(self.root,textvariable=self.bmi_text).pack()
self.root.mainloop()
def计算(自我):
#检索计算体重指数所需的所有信息
重量=浮动(self.lbs.get())
feet=float(self.feet.get())
英寸=浮动(self.inches.get())
高度=(英尺*12)+英寸
体重指数=漂浮物((体重*703)/(身高**2))
#更新状态标签
self.bmi_num.set(“您的bmi为%.2f”%bmi)
如果体重指数<18.5:
self.bmi_text.set(“你体重不足”)

如果18.5看起来您在这里混淆了继承和封装。您的应用程序类继承自Tk,这意味着它是一个Tk。然而,您也将Tk对象封装在应用程序中,并将其分配给self.root。所以你得到了App,它是Tk的一个子类,封装了App.root,它也是Tk


实际上,您正在self.root Tk实例上构建GUI,但随后您还调用了
Tk.\uuu init\uuu(self)
,这将初始化一个空白Tk对象(因此是第二个窗口)。你需要选择一种或另一种方法;要么(1)不从Tk继承,不调用
Tk.\uuuuu init\uuuuuuu(self)
,只使用self.root,要么(2)根本不创建
self.root
对象,调用
Tk.\uuuuuu init\uuuuuuuuuself)
,只要在当前使用的任何地方使用
self.root
就可以了,这里似乎混淆了继承和封装。您的应用程序类继承自Tk,这意味着它是一个Tk。然而,您也将Tk对象封装在应用程序中,并将其分配给self.root。所以你得到了App,它是Tk的一个子类,封装了App.root,它也是Tk


实际上,您正在self.root Tk实例上构建GUI,但随后您还调用了
Tk.\uuu init\uuu(self)
,这将初始化一个空白Tk对象(因此是第二个窗口)。你需要选择一种或另一种方法;要么(1)不从Tk继承,不调用
Tk.\uuuuu init\uuuuuuu(self)
,只使用self.root,要么(2)根本不创建
self.root
对象,调用
Tk.\uuuuuu init\uuuuuuuuuself)
,只要在当前使用的任何地方使用
self.root
就可以了,这里似乎混淆了继承和封装。您的应用程序类继承自Tk,这意味着它是一个Tk。然而,您也将Tk对象封装在应用程序中,并将其分配给self.root。所以你得到了App,它是Tk的一个子类,封装了App.root,它也是Tk


实际上,您正在self.root Tk实例上构建GUI,但随后您还调用了
Tk.\uuu init\uuu(self)
,这将初始化一个空白Tk对象(因此是第二个窗口)。你需要选择一种或另一种方法;要么(1)不从Tk继承,不调用
Tk.\uuuuu init\uuuuuuu(self)
,只使用self.root,要么(2)根本不创建
self.root
对象,调用
Tk.\uuuuuu init\uuuuuuuuuself)
,只要在当前使用的任何地方使用
self.root
就可以了,这里似乎混淆了继承和封装。您的应用程序类继承自Tk,这意味着它是一个Tk。然而,您也将Tk对象封装在应用程序中,并将其分配给self.root。所以你得到了App,它是Tk的一个子类,封装了App.root,它也是Tk


实际上,您正在self.root Tk实例上构建GUI,但随后您还调用了
Tk.\uuu init\uuu(self)
,这将初始化一个空白Tk对象(因此是第二个窗口)。你需要选择一种或另一种方法;要么(1)不从Tk继承,不调用
Tk.\uuuuu init\uuuuuuu(self)
,只使用self.root,要么(2)根本不创建
self.root
对象,调用
Tk.\uuuuuu init\uuuuuuuuuself)
,只需在您当前使用的任何地方使用
self.root

请注意,这似乎是我所教班级的家庭作业。如果你想用这个例子作为自己的例子,请注意两件事

  • 代码有一些问题,所以它不是最好的复制方式
  • 助教和我都知道这段代码是在线的,所以你可能会被抓获

    • 请注意,这似乎是