import tkinter as tk
from tkinter import ttk

# 重量换算基准（以克为中间单位）
unit_rate = {
    "克(g)": 1,
    "千克(kg)": 1000,
    "斤": 500,
    "两": 50,
    "吨(t)": 1000000,
    "磅(lb)": 453.59237
}

def convert_weight():
    try:
        # 获取输入数值
        input_num = float(entry_input.get())
        # 获取选中单位
        from_unit = combo_from.get()
        to_unit = combo_to.get()
        # 先转成克，再转目标单位
        gram = input_num * unit_rate[from_unit]
        result = gram / unit_rate[to_unit]
        label_result.config(text=f"转换结果：{result:.4f} {to_unit}")
    except ValueError:
        label_result.config(text="请输入有效数字！")

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

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

# 来源单位
tk.Label(root, text="原单位：", font=("微软雅黑",11)).place(x=30, y=70)
combo_from = ttk.Combobox(root, values=list(unit_rate.keys()), width=12, font=("微软雅黑",10))
combo_from.current(0)
combo_from.place(x=110, y=70)

# 目标单位
tk.Label(root, text="目标单位：", font=("微软雅黑",11)).place(x=220, y=70)
combo_to = ttk.Combobox(root, values=list(unit_rate.keys()), width=12, font=("微软雅黑",10))
combo_to.current(1)
combo_to.place(x=290, y=70)

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

# 结果显示
label_result = tk.Label(root, text="转换结果：", font=("微软雅黑",12), fg="#d83020")
label_result.place(x=60, y=160)

root.mainloop()