import tkinter as tk
from tkinter import ttk

# 重量换算基准：全部换算成克
unit_data = {
    "吨": 1000000,
    "千克(kg)": 1000,
    "克(g)": 1,
    "斤": 500,
    "磅(lb)": 453.592
}


def convert_weight():
    """执行重量换算"""
    try:
        num = float(input_entry.get())
        from_unit = combo_from.get()
        to_unit = combo_to.get()
        # 转为克
        gram = num * unit_data[from_unit]
        result = gram / unit_data[to_unit]
        output_label.config(text=f"换算结果：{result:.4f} {to_unit}")
    except ValueError:
        output_label.config(text="请输入合法数字！")


# 创建主窗口
root = tk.Tk()
root.title("重量单位转换器")
root.geometry("420x260")
root.resizable(False, False)

# 输入组件
tk.Label(root, text="请输入数值：", font=("微软雅黑",12)).pack(pady=6)
input_entry = tk.Entry(root, font=("微软雅黑",12), width=25)
input_entry.pack(pady=4)

# 下拉选择框
frame_select = tk.Frame(root)
frame_select.pack(pady=8)

combo_from = ttk.Combobox(frame_select, values=list(unit_data.keys()), font=("微软雅黑",11), width=12)
combo_from.current(1)
combo_from.grid(row=0, column=0, padx=8)

tk.Label(frame_select, text="→", font=("微软雅黑",14)).grid(row=0, column=1)

combo_to = ttk.Combobox(frame_select, values=list(unit_data.keys()), font=("微软雅黑",11), width=12)
combo_to.current(2)
combo_to.grid(row=0, column=2, padx=8)

# 转换按钮
btn = tk.Button(root, text="开始转换", command=convert_weight,
                 font=("微软雅黑",12), bg="#409EFF", fg="white")
btn.pack(pady=8)

# 结果展示
output_label = tk.Label(root, text="换算结果：", font=("微软雅黑",12), fg="#E6A23C")
output_label.pack(pady=6)

root.mainloop()