Tkinter - 简单程序不会更新 [英] Tkinter - Simple program won't update

查看:28
本文介绍了Tkinter - 简单程序不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Tkinter 制作了一个 BMI 计算器.我已经成功地对需要用户输入的位进行编码并计算他们的 BMI.我遇到的一个问题是它只在第一次点击时显示你体重不足/正常/肥胖".第一次点击后,此标签不再更新,但 BMI 标签会更新.

I made a BMI calculator with Tkinter. I have successful coded the bits where it takes the user input and calculates their BMI. One problem I'm encountering is that it only states "You are underweight/normal/obese" on the first click. After the first click, this label doesn't update anymore, but the BMI label does.

有人可以告诉我如何解决这个问题吗?

Can someone show me how I can fix this?

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()

推荐答案

我先说我的 Tkinter 经验是...有限的 :) 但是下面的内容似乎可以满足您的需求.主要的调整是定义标签本身.

I'll preface this by saying my Tkinter experience is...limited :) However the below seems to do what you need. The main adjustments were in defining the labels themselves.

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()

这篇关于Tkinter - 简单程序不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆