0

TkinterでBMI計算機を作りました。ユーザー入力を受け取り、BMI を計算するビットのコーディングに成功しました。私が遭遇している問題の 1 つは、最初のクリックで「あなたは体重不足/標準/肥満です」としか表示されないことです。最初のクリックの後、このラベルは更新されなくなりますが、BMI ラベルは更新されます。

誰かがこれを修正する方法を教えてもらえますか?

from Tkinter import *
import tkMessageBox

class App(object):
    def __init__(self):
        self.root = Tk()
        self.root.wm_title("Question 7")
        self.label = Label (self.root, text= "Enter your weight in pounds.")
        self.label.pack()  
        self.entrytext = StringVar()
        Entry(self.root, textvariable=self.entrytext).pack()

        self.label = Label (self.root, text= "Enter your height in inches.")
        self.label.pack()  
        self.entrytext2 = StringVar()
        Entry(self.root, textvariable=self.entrytext2).pack()

        self.buttontext = StringVar()
        self.buttontext.set("Calculate")
        Button(self.root, textvariable=self.buttontext, command=self.clicked1).pack()

        self.label = Label (self.root, text="")
        self.label.pack()

        self.dec = Label (self.root, text="")
        self.dec.pack()

        self.root.mainloop()


    def clicked1(self):
        w = float(self.entrytext.get())
        h = float(self.entrytext2.get())
        bmi = float((w/(h**2))*703)
        bmi = ("Your BMI is %.2f" %bmi)
        self.label.configure(text=bmi)
        if bmi < 18.5:
            self.dec.configure(text="You are underweight")
        if 18.5 <= bmi < 25:
            self.dec.configure(text="You are normal")
        if 25 <= bmi < 30:
            self.dec.configure(text="You are overweight")
        if 30<= bmi > 30:
            self.dec.configure(text="You are obese")



App()
4

1 に答える 1

0

私のTkinterの経験は...限られていると言ってこれを序文にします:)しかし、以下はあなたが必要とすることをするようです. 主な調整は、ラベル自体の定義にありました。

from Tkinter import *
import tkMessageBox

class App(object):
    def __init__(self):
        self.root = Tk()
        self.root.wm_title("Question 7")

        self.label = Label(self.root, text="Enter your weight in pounds.").pack()
        self.entrytext = StringVar()
        Entry(self.root, textvariable=self.entrytext).pack()

        self.label = Label(self.root, text="Enter your height in inches.").pack()
        self.entrytext2 = StringVar()
        Entry(self.root, textvariable=self.entrytext2).pack()

        self.buttontext = StringVar()
        Button(self.root, textvariable=self.buttontext, command=self.clicked1).pack()
        self.buttontext.set("Calculate")

        # Here we set bmi_num (the actual number) as a StringVar
        # and then pack a label with the textvariable property = bmi_num.
        # This ensure that whenever we change that variable, the label updates
        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 clicked1(self):
        w = float(self.entrytext.get())
        h = float(self.entrytext2.get())
        bmi = float((w/(h**2))*703)
        # Now we just use the below syntax to update the 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()
于 2012-10-29T02:12:05.400 に答える