from tkinter import * class MetricConverter: def __init__(self): self.valid_conversions = { ("cm", "inches"): lambda x: round(x / 2.54, 2), ("m", "feet"): lambda x: round(x * 3.28084, 2), ("km", "miles"): lambda x: round(x * 0.621371, 2), } self.from_unit = None self.to_unit = None self.window = Tk() self.window.title("Metric Conversion Tool") self.main_frame = Frame(self.window, padx=10, pady=10) self.main_frame.grid() self.heading = Label(self.main_frame, text="Metric Conversion Tool", font=("Arial", 16, "bold")) self.heading.grid(row=0) self.instructions = Label(self.main_frame, text="Enter a value and select FROM and TO units:", wraplength=300, justify="left") self.instructions.grid(row=1) self.input_entry = Entry(self.main_frame, font=("Arial", 14)) self.input_entry.grid(row=2, pady=10) # FROM unit buttons self.from_label = Label(self.main_frame, text="From:") self.from_label.grid(row=3, sticky="w") self.from_frame = Frame(self.main_frame) self.from_frame.grid(row=4, pady=5) self.from_buttons = {} for i, unit in enumerate(set(key[0] for key in self.valid_conversions.keys())): btn = Button(self.from_frame, text=unit, width=10, command=lambda u=unit: self.select_unit("from", u)) btn.grid(row=0, column=i, padx=5) self.from_buttons[unit] = btn # TO unit buttons self.to_label = Label(self.main_frame, text="To:") self.to_label.grid(row=5, sticky="w") self.to_frame = Frame(self.main_frame) self.to_frame.grid(row=6, pady=5) self.to_buttons = {} for i, unit in enumerate(set(key[1] for key in self.valid_conversions.keys())): btn = Button(self.to_frame, text=unit, width=10, command=lambda u=unit: self.select_unit("to", u)) btn.grid(row=0, column=i, padx=5) self.to_buttons[unit] = btn # Convert button self.convert_button = Button(self.main_frame, text="Convert", command=self.convert_value, bg="#004C99", fg="white", font=("Arial", 12, "bold")) self.convert_button.grid(row=7, pady=10) self.result_label = Label(self.main_frame, text="", font=("Arial", 14, "bold")) self.result_label.grid(row=8) self.error_label = Label(self.main_frame, text="", fg="red") self.error_label.grid(row=9) self.window.mainloop() def select_unit(self, unit_type, unit): if unit_type == "from": self.from_unit = unit for u, btn in self.from_buttons.items(): if u == unit: btn.config(relief="sunken", bg="#007ACC", fg="white") else: btn.config(relief="raised", bg="SystemButtonFace", fg="black") elif unit_type == "to": self.to_unit = unit for u, btn in self.to_buttons.items(): if u == unit: btn.config(relief="sunken", bg="#007ACC", fg="white") else: btn.config(relief="raised", bg="SystemButtonFace", fg="black") def convert_value(self): self.error_label.config(text="") self.result_label.config(text="") if self.from_unit is None or self.to_unit is None: self.error_label.config(text="Please select both FROM and TO units.") return if (self.from_unit, self.to_unit) not in self.valid_conversions: self.error_label.config(text=f"Cannot convert from {self.from_unit} to {self.to_unit}.") return try: value = float(self.input_entry.get()) conversion_func = self.valid_conversions[(self.from_unit, self.to_unit)] result = conversion_func(value) self.result_label.config(text=f"Result: {result} {self.to_unit}") except ValueError: self.error_label.config(text="Please enter a valid number.") if __name__ == "__main__": MetricConverter()