Unit Converter (GUI, tkinter)

andreasPython Code2 months ago71 Views

import tkinter as tk
from tkinter import ttk

def convert():
    try:
        value = float(entry.get())
        unit_from = from_unit.get()
        unit_to = to_unit.get()

        result = None

        if unit_type.get() == "Temperature":
            if unit_from == "Celsius" and unit_to == "Fahrenheit":
                result = value * 9/5 + 32
            elif unit_from == "Fahrenheit" and unit_to == "Celsius":
                result = (value - 32) * 5/9
            elif unit_from == "Celsius" and unit_to == "Kelvin":
                result = value + 273.15
            elif unit_from == "Kelvin" and unit_to == "Celsius":
                result = value - 273.15
            elif unit_from == "Fahrenheit" and unit_to == "Kelvin":
                result = (value - 32) * 5/9 + 273.15
            elif unit_from == "Kelvin" and unit_to == "Fahrenheit":
                result = (value - 273.15) * 9/5 + 32
            else:
                result = value

        elif unit_type.get() == "Length":
            conversions = {
                "Meters": 1.0,
                "Kilometers": 1000,
                "Feet": 0.3048,
                "Miles": 1609.34
            }
            result = value * conversions[unit_from] / conversions[unit_to]

        elif unit_type.get() == "Weight":
            conversions = {
                "Grams": 1.0,
                "Kilograms": 1000,
                "Pounds": 453.592,
                "Ounces": 28.3495
            }
            result = value * conversions[unit_from] / conversions[unit_to]

        output_label.config(text=f"Result: {round(result, 4)} {unit_to}")
    except Exception as e:
        output_label.config(text=f"Error: {e}")

def update_units(*args):
    units = {
        "Temperature": ["Celsius", "Fahrenheit", "Kelvin"],
        "Length": ["Meters", "Kilometers", "Feet", "Miles"],
        "Weight": ["Grams", "Kilograms", "Pounds", "Ounces"]
    }
    options = units[unit_type.get()]
    from_unit['values'] = options
    to_unit['values'] = options
    from_unit.set(options[0])
    to_unit.set(options[1])

# GUI setup
root = tk.Tk()
root.title("Unit Converter")

tk.Label(root, text="Enter value:").pack(pady=5)
entry = tk.Entry(root)
entry.pack(pady=5)

tk.Label(root, text="Select category:").pack()
unit_type = ttk.Combobox(root, values=["Temperature", "Length", "Weight"])
unit_type.set("Temperature")
unit_type.pack()
unit_type.bind("<<ComboboxSelected>>", update_units)

tk.Label(root, text="From:").pack()
from_unit = ttk.Combobox(root)
from_unit.pack()

tk.Label(root, text="To:").pack()
to_unit = ttk.Combobox(root)
to_unit.pack()

update_units()

tk.Button(root, text="Convert", command=convert).pack(pady=10)
output_label = tk.Label(root, text="")
output_label.pack(pady=5)

root.mainloop()

Features:

  • Converts between:
    • Temperature: Celsius, Fahrenheit, Kelvin
    • Length: Meters, Kilometers, Feet, Miles
    • Weight: Grams, Kilograms, Pounds, Ounces
  • Easy-to-use dropdowns
  • Smart result rounding
  • No external libraries needed

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Previous Post

Next Post

Loading Next Post...
Loading

Signing-in 3 seconds...