import tkinter as tk

W, H = 300, 420
TOP, BOT = 70, 350
X = 150
T_MIN, T_MAX = -20, 100

root = tk.Tk()
root.title("温度计演示")
cv = tk.Canvas(root, width=W, height=H, bg="#f8f9fa")
cv.pack()

def y_of(t):
    return BOT - (t - T_MIN) / (T_MAX - T_MIN) * (BOT - TOP)

def draw(t):
    cv.delete("all")
    # 外管 + 球泡
    cv.create_rectangle(X-10, TOP, X+10, BOT+20, outline="#666", width=2)
    cv.create_oval(X-24, BOT+10, X+24, BOT+58, outline="#666", width=2)
    # 水银柱
    y = y_of(t)
    cv.create_rectangle(X-7, y, X+7, BOT+30, fill="#d9534f", outline="")
    cv.create_oval(X-20, BOT+14, X+20, BOT+54, fill="#d9534f", outline="")
    # 刻度
    for tt in range(T_MIN, T_MAX+1, 10):
        yy = y_of(tt)
        cv.create_line(X+10, yy, X+18, yy, fill="#555")
        cv.create_text(X+30, yy, text=str(tt), anchor="w", font=("微软雅黑", 9))
    # 文字
    f = t*9/5 + 32
    state = "冰（固态）" if t < 0 else "水（液态）" if t < 100 else "水蒸气（气态）"
    cv.create_text(20, 20, anchor="nw", font=("微软雅黑", 12, "bold"),
                   text=f"{t:.1f} ℃ / {f:.1f} ℉\n状态：{state}")

def on_move(e):
    t = T_MAX - (e.y - TOP) / (BOT - TOP) * (T_MAX - T_MIN)
    draw(max(T_MIN, min(T_MAX, t)))

cv.bind("<B1-Motion>", on_move)
cv.bind("<Button-1>", on_move)
draw(20)
root.mainloop()