如何给美化这段程序,如加入背景图等import tkinter as tk import numpy as np def change_label(): button.destroy() label.config(text="请输入您的身高体重以及目标体重:") height_label.place(relx=0.5, rely=0.4, anchor="center") height_entry.place(relx=0.5, rely=0.45, anchor="center") weight_label.place(relx=0.5, rely=0.5, anchor="center") weight_entry.place(relx=0.5, rely=0.55, anchor="center") target_label.place(relx=0.5, rely=0.6, anchor="center") target_entry.place(relx=0.5, rely=0.65, anchor="center") submit_button.place(relx=0.5, rely=0.8, anchor="center")
时间: 2024-02-19 15:59:28 浏览: 58
可以使用tkinter中的Canvas组件添加背景图或者自定义样式。以下是一个实现的示例代码:
```python
import tkinter as tk
import numpy as np
# 创建窗口
root = tk.Tk()
root.title("健康管理")
# 设置窗口背景图
bg_image = tk.PhotoImage(file="bg.png")
canvas = tk.Canvas(root, width=600, height=400)
canvas.create_image(0, 0, image=bg_image, anchor="nw")
canvas.pack()
# 创建标签和输入框
label = tk.Label(canvas, text="欢迎使用健康管理系统", font=("Arial", 20))
label.place(relx=0.5, rely=0.2, anchor="center")
height_label = tk.Label(canvas, text="身高(cm):", font=("Arial", 16))
height_label.place(relx=0.3, rely=0.4, anchor="center")
height_entry = tk.Entry(canvas, font=("Arial", 16))
height_entry.place(relx=0.5, rely=0.4, anchor="center")
weight_label = tk.Label(canvas, text="体重(kg):", font=("Arial", 16))
weight_label.place(relx=0.3, rely=0.5, anchor="center")
weight_entry = tk.Entry(canvas, font=("Arial", 16))
weight_entry.place(relx=0.5, rely=0.5, anchor="center")
target_label = tk.Label(canvas, text="目标体重(kg):", font=("Arial", 16))
target_label.place(relx=0.3, rely=0.6, anchor="center")
target_entry = tk.Entry(canvas, font=("Arial", 16))
target_entry.place(relx=0.5, rely=0.6, anchor="center")
# 创建按钮
def calculate_bmi():
height = float(height_entry.get()) / 100
weight = float(weight_entry.get())
target_weight = float(target_entry.get())
bmi = weight / (height ** 2)
result = "您的BMI指数为:{:.2f}\n".format(bmi)
if bmi < 18.5:
result += "您的体重过轻,建议增加营养摄入。"
elif bmi < 24:
result += "您的体重正常,请保持。"
elif bmi < 28:
result += "您的体重超重,建议减少饮食摄入。"
else:
result += "您的体重肥胖,建议立即减肥。"
result_label.config(text=result)
submit_button = tk.Button(canvas, text="计算BMI", font=("Arial", 16), command=calculate_bmi)
submit_button.place(relx=0.5, rely=0.8, anchor="center")
# 创建结果标签
result_label = tk.Label(canvas, font=("Arial", 16), justify="left")
result_label.place(relx=0.5, rely=0.9, anchor="center")
# 运行窗口
root.mainloop()
```
这个示例代码使用了Canvas组件作为窗口的容器,并在其中添加了一个背景图。同时,还对标签和输入框的位置进行了微调,让整个界面看起来更加美观。
阅读全文