import tkinter as tk
from tkinter import ttk

def convert_weight():
    # 单位换算基准，全部换算为克
    unit_dict = {
        "克(g)": 1,
        "千克(kg)": 1000,
        "斤": 500,
        "两": 50,
        "吨(t)": 1000000,
        "磅(lb)": 453.59237
    }
    try:
        # 获取输入数值
        num = float(entry_num.get())
        unit_from = combo_from.get()
        unit_to = combo_to.get()
        # 先转克，再转目标单位
        gram = num * unit_dict[unit_from]
        result = gram / unit_dict[unit_to]
        # 显示结果保留4位小数
        label_result.config(text=f"转换结果：{result:.4f} {unit_to}")
    except ValueError:
        label_result.config(text="请输入合法数字！")
    except KeyError:
        label_result.config(text="请选择正确单位！")

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

# 可选单位列表
unit_list = ["克(g)", "千克(kg)", "斤", "两", "吨(t)", "磅(lb)"]

# 输入数值框
tk.Label(root, text="输入数值：", font=("微软雅黑", 11)).place(x=30, y=30)
entry_num = tk.Entry(root, width=15, font=("微软雅黑", 11))
entry_num.place(x=110, y=30)

# 原单位下拉框
tk.Label(root, text="原单位：", font=("微软雅黑", 11)).place(x=30, y=70)
combo_from = ttk.Combobox(root, values=unit_list, width=12, font=("微软雅黑", 11))
combo_from.current(1)  # 默认选中千克
combo_from.place(x=110, y=70)

# 目标单位下拉框
tk.Label(root, text="目标单位：", font=("微软雅黑", 11)).place(x=220, y=70)
combo_to = ttk.Combobox(root, values=unit_list, width=12, font=("微软雅黑", 11))
combo_to.current(2)  # 默认选中斤
combo_to.place(x=295, y=70)

# 转换按钮
btn_convert = tk.Button(root, text="开始转换", command=convert_weight,
                        font=("微软雅黑", 11), bg="#4096ee", fg="white")
btn_convert.place(x=140, y=115, width=130)

# 结果显示标签
label_result = tk.Label(root, text="转换结果将显示在这里", font=("微软雅黑", 12), fg="#d81e06")
label_result.place(x=40, y=160)

root.mainloop()