Загрузка данных


from tkinter import *
from tkinter import ttk

root = Tk()
root.title("Конвертер Единиц")
root.geometry("480x400")

result_text = StringVar()

def convert():
    try:
        num = float(entry.get())
        
        conv_type = combo_type.get()
        from_unit = combo_from.get()
        to_unit = combo_to.get()
        
        answer = num

        # Температура
        if conv_type == "Температура":
            if from_unit == "Цельсий" and to_unit == "Фаренгейт":
                answer = num * 9/5 + 32
            elif from_unit == "Фаренгейт" and to_unit == "Цельсий":
                answer = (num - 32) * 5/9
            elif from_unit == "Цельсий" and to_unit == "Кельвин":
                answer = num + 273.15
            elif from_unit == "Кельвин" and to_unit == "Цельсий":
                answer = num - 273.15

        # Длина
        elif conv_type == "Длина":
            if from_unit == "Метры" and to_unit == "Километры":
                answer = num / 1000
            elif from_unit == "Километры" and to_unit == "Метры":
                answer = num * 1000
            elif from_unit == "Метры" and to_unit == "Сантиметры":
                answer = num * 100
            elif from_unit == "Сантиметры" and to_unit == "Метры":
                answer = num / 100

        result_text.set(f"{answer:.4f} {to_unit}")
        
    except:
        result_text.set("Ошибка")


def change_type(*args):
    t = combo_type.get()
    if t == "Температура":
        units = ["Цельсий", "Фаренгейт", "Кельвин"]
    else:
        units = ["Метры", "Километры", "Сантиметры"]
    
    combo_from['values'] = units
    combo_to['values'] = units
    combo_from.current(0)
    combo_to.current(1)


# ===================== Интерфейс =====================

Label(root, text="Конвертер Единиц", font=("Arial", 16, "bold")).pack(pady=15)

Label(root, text="Тип:").pack(anchor=W, padx=30)
combo_type = ttk.Combobox(root, values=["Температура", "Длина"], state="readonly")
combo_type.current(0)
combo_type.pack(padx=30, fill=X)
combo_type.bind("<<ComboboxSelected>>", change_type)

Label(root, text="Значение:").pack(anchor=W, padx=30, pady=(10,0))
entry = Entry(root, font=("Arial", 12))
entry.pack(padx=30, fill=X)

Label(root, text="Из:").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="В:").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="Конвертировать", font=("Arial", 12, "bold"), 
       bg="#4CAF50", fg="white", height=2, command=convert).pack(pady=20)

Label(root, text="Результат:", 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()