from tkinter import *
from tkinter import ttk
# Создаём окно
root = Tk()
root.title("Unit Converter")
root.geometry("480x400")
# Переменная для результата
result_text = StringVar()
def convert():
try:
num = float(entry.get()) # число которое ввёл пользователь
тип = combo_type.get()
из = combo_from.get()
в = combo_to.get()
итог = num # сначала результат = введённое число
# === Температура ===
if тип == "Temperature":
if из == "Celsius" and в == "Fahrenheit":
итог = num * 9/5 + 32
elif из == "Fahrenheit" and в == "Celsius":
итог = (num - 32) * 5/9
elif из == "Celsius" and в == "Kelvin":
итог = num + 273.15
elif из == "Kelvin" and в == "Celsius":
итог = num - 273.15
# === Длина ===
elif тип == "Length":
if из == "Meters" and в == "Kilometers":
итог = num / 1000
elif из == "Kilometers" and в == "Meters":
итог = num * 1000
elif из == "Meters" and в == "Centimeters":
итог = num * 100
elif из == "Centimeters" and в == "Meters":
итог = num / 100
# Показываем результат
result_text.set(f"{итог:.4f} {в}")
except:
result_text.set("Ошибка")
def change_type(*args):
тип = combo_type.get()
if тип == "Temperature":
единицы = ["Celsius", "Fahrenheit", "Kelvin"]
else:
единицы = ["Meters", "Kilometers", "Centimeters"]
combo_from['values'] = единицы
combo_to['values'] = единицы
combo_from.current(0)
combo_to.current(1)
# ===================== Интерфейс =====================
Label(root, text="Unit Converter", font=("Arial", 16, "bold")).pack(pady=15)
Label(root, text="Type:").pack(anchor=W, padx=30)
combo_type = ttk.Combobox(root, values=["Temperature", "Length"], state="readonly")
combo_type.current(0)
combo_type.pack(padx=30, fill=X)
combo_type.bind("<<ComboboxSelected>>", change_type)
Label(root, text="Value:").pack(anchor=W, padx=30, pady=(10,0))
entry = Entry(root, font=("Arial", 12))
entry.pack(padx=30, fill=X)
Label(root, text="From:").pack(anchor=W, padx=30, pady=(10,0))
combo_from = ttk.Combobox(root, state="readonly")
combo_from.pack(padx=30, fill=X)
Label(root, text="To:").pack(anchor=W, padx=30, pady=(10,0))
combo_to = ttk.Combobox(root, state="readonly")
combo_to.pack(padx=30, fill=X)
Button(root, text="Convert", font=("Arial", 12, "bold"),
bg="#4CAF50", fg="white", height=2, command=convert).pack(pady=20)
Label(root, text="Result:", font=("Arial", 11)).pack(anchor=W, padx=30)
Label(root, textvariable=result_text, font=("Arial", 14, "bold"), fg="blue").pack(pady=8)
# Запуск программы
change_type()
root.mainloop()