利用tkinter,设计完成BMI计算器。
时间: 2024-02-22 21:55:46 浏览: 128
好的,以下是一个基本的BMI计算器的代码,使用了tkinter库:
```python
import tkinter as tk
def calculate_bmi():
height = float(height_entry.get()) / 100
weight = float(weight_entry.get())
bmi = weight / (height ** 2)
result_label.config(text="Your BMI is {:.2f}".format(bmi))
root = tk.Tk()
root.title("BMI Calculator")
# Create a label for height
height_label = tk.Label(root, text="Height (cm):")
height_label.pack()
# Create an entry for height
height_entry = tk.Entry(root)
height_entry.pack()
# Create a label for weight
weight_label = tk.Label(root, text="Weight (kg):")
weight_label.pack()
# Create an entry for weight
weight_entry = tk.Entry(root)
weight_entry.pack()
# Create a button to calculate BMI
calculate_button = tk.Button(root, text="Calculate BMI", command=calculate_bmi)
calculate_button.pack()
# Create a label to display the result
result_label = tk.Label(root, text="")
result_label.pack()
root.mainloop()
```
这个计算器会根据输入的身高和体重计算BMI,并显示结果。希望对你有帮助!
阅读全文