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


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())
        
        conv_type = combo_type.get()
        from_unit = combo_from.get()
        to_unit = combo_to.get()
        
        answer = num

        # Temperature
        if conv_type == "Temperature":
            if from_unit == "Celsius" and to_unit == "Fahrenheit":
                answer = num * 9/5 + 32
            elif from_unit == "Fahrenheit" and to_unit == "Celsius":
                answer = (num - 32) * 5/9
            elif from_unit == "Celsius" and to_unit == "Kelvin":
                answer = num + 273.15
            elif from_unit == "Kelvin" and to_unit == "Celsius":
                answer = num - 273.15

        # Length
        elif conv_type == "Length":
            if from_unit == "Meters" and to_unit == "Kilometers":
                answer = num / 1000
            elif from_unit == "Kilometers" and to_unit == "Meters":
                answer = num * 1000
            elif from_unit == "Meters" and to_unit == "Centimeters":
                answer = num * 100
            elif from_unit == "Centimeters" and to_unit == "Meters":
                answer = num / 100

        result_text.set(f"{answer:.4f} {to_unit}")
        
    except:
        result_text.set("Error")


def change_type(*args):
    t = combo_type.get()
    if t == "Temperature":
        units = ["Celsius", "Fahrenheit", "Kelvin"]
    else:
        units = ["Meters", "Kilometers", "Centimeters"]
    
    combo_from['values'] = units
    combo_to['values'] = units
    combo_from.current(0)
    combo_to.current(1)


# Interface
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()