from tkinter import * # Trial - Radio Buttons as an alternative to OptionMenu dropdowns # Purpose: Compare radio buttons vs dropdowns for unit selection. # This was the other main option trialled before choosing dropdowns. # # Conclusion (at the bottom of this file): # Radio buttons work but do NOT scale well when you have many options. # With 4 units and needing TWO selections (from + to), you would need # 8 radio buttons on screen at once, making the GUI cluttered. # OptionMenu dropdowns were chosen instead. UNITS = ["km", "m", "cm", "mm"] class RadioButtonTrial: def __init__(self): self.frame = Frame(padx=10, pady=10) self.frame.grid() self.heading = Label(self.frame, text="Radio Button Trial", font=("Arial", "16", "bold")) self.heading.grid(row=0, columnspan=2) # --- Convert FROM group --- from_label = Label(self.frame, text="Convert FROM:", font=("Arial", "12", "bold")) from_label.grid(row=1, column=0, sticky="w", pady=(10, 2)) # IntVar or StringVar works - StringVar is cleaner for text values self.from_var = StringVar(value="km") for i, unit in enumerate(UNITS): rb = Radiobutton(self.frame, text=unit, variable=self.from_var, value=unit, font=("Arial", "12")) rb.grid(row=2 + i, column=0, sticky="w", padx=20) # --- Convert TO group --- to_label = Label(self.frame, text="Convert TO:", font=("Arial", "12", "bold")) to_label.grid(row=1, column=1, sticky="w", pady=(10, 2), padx=20) self.to_var = StringVar(value="m") for i, unit in enumerate(UNITS): rb = Radiobutton(self.frame, text=unit, variable=self.to_var, value=unit, font=("Arial", "12")) rb.grid(row=2 + i, column=1, sticky="w", padx=40) # --- Button and result --- self.convert_button = Button(self.frame, text="Convert", font=("Arial", "12", "bold"), bg="#009900", fg="#FFFFFF", width=12, command=self.show_selected) self.convert_button.grid(row=7, column=0, columnspan=2, pady=10) self.result_label = Label(self.frame, text="", font=("Arial", "12")) self.result_label.grid(row=8, column=0, columnspan=2) # Comparison note note = ("NOTE: Radio buttons work, but with 4 units and 2 groups\n" "this is already 8 radio buttons. Adding more units would\n" "make this very crowded. Dropdowns scale much better.") Label(self.frame, text=note, font=("Arial", "10"), fg="#CC6600", justify="left").grid(row=9, column=0, columnspan=2, pady=10, padx=5) def show_selected(self): from_unit = self.from_var.get() to_unit = self.to_var.get() if from_unit == to_unit: self.result_label.config( text="Error: please choose two different units", fg="#9C0000" ) else: self.result_label.config( text=f"Converting from {from_unit} to {to_unit}", fg="#004C99" ) # main routine if __name__ == "__main__": root = Tk() root.title("Radio Button Trial") RadioButtonTrial() root.mainloop()