Introduction
Unit conversion is a common requirement in various domains such as science, engineering, and everyday life. This program provides an interactive application for converting between different units of length, weight, and volume.
Objective
The objective of this program is to enable users to convert units interactively, making the process quick and accurate using Python.
Python Code
import tkinter as tk from tkinter import messagebox class UnitConverter: def __init__(self, root): self.root = root self.root.title("Unit Conversion Application") self.conversion_options = { "Length": {"meters to kilometers": 0.001, "kilometers to meters": 1000}, "Weight": {"grams to kilograms": 0.001, "kilograms to grams": 1000}, "Volume": {"milliliters to liters": 0.001, "liters to milliliters": 1000} } self.category_label = tk.Label(root, text="Select Category:") self.category_label.pack() self.category_var = tk.StringVar(value="Length") self.category_menu = tk.OptionMenu(root, self.category_var, *self.conversion_options.keys(), command=self.update_conversion_menu) self.category_menu.pack() self.conversion_label = tk.Label(root, text="Select Conversion:") self.conversion_label.pack() self.conversion_var = tk.StringVar(value="meters to kilometers") self.conversion_menu = tk.OptionMenu(root, self.conversion_var, *self.conversion_options["Length"].keys()) self.conversion_menu.pack() self.value_entry = tk.Entry(root, width=30) self.value_entry.pack(pady=10) self.convert_button = tk.Button(root, text="Convert", command=self.convert) self.convert_button.pack() self.result_label = tk.Label(root, text="Result: ") self.result_label.pack(pady=10) def update_conversion_menu(self, category): menu = self.conversion_menu['menu'] menu.delete(0, 'end') for conversion in self.conversion_options[category].keys(): menu.add_command(label=conversion, command=lambda value=conversion: self.conversion_var.set(value)) def convert(self): try: value = float(self.value_entry.get()) conversion_factor = self.conversion_options[self.category_var.get()][self.conversion_var.get()] result = value * conversion_factor self.result_label.config(text=f"Result: {result:.2f}") except ValueError: messagebox.showerror("Input Error", "Please enter a valid number.") if __name__ == "__main__": root = tk.Tk() app = UnitConverter(root) root.mainloop()
Program Explanation
This Python program uses the Tkinter library to create an interactive unit conversion application. Here’s a breakdown of the program structure:
UnitConverter
: The main application class that initializes the GUI elements and handles unit conversion.update_conversion_menu
: Updates the conversion options dynamically based on the selected category.convert
: Performs the conversion and displays the result based on user input and selected options.
Steps to Run the Program
- Ensure Python is installed on your system.
- Copy the code into a file named
unit_converter.py
. - Open a terminal or command prompt and navigate to the directory containing the file.
- Run the script using the command:
python unit_converter.py
. - Select a category, choose a conversion type, enter a value, and click “Convert” to see the result.