from tkinter import * import all_constants as c class Converter(): def __init__(self): self.dist_frame = Frame(padx=10, pady=10) self.dist_frame.grid() self.dist_heading = Label(self.dist_frame, text="Distance Converter", font=("Arial", "16", "bold")) self.dist_heading.grid(row=0, columnspan=2) instructions = ("Please enter a distance below, choose the units " "to convert from and to, then press Convert.") self.dist_instructions = Label(self.dist_frame, text=instructions, wraplength=250, width=40, justify="left") self.dist_instructions.grid(row=1, columnspan=2) # Input row: entry + from-unit dropdown self.input_subframe = Frame(self.dist_frame) self.input_subframe.grid(row=2, pady=10) self.dist_entry = Entry(self.input_subframe, font=("Arial", "14"), width=14) self.dist_entry.grid(row=0, column=0, padx=(0, 10)) self.from_unit_var = StringVar(value=c.UNITS[0]) self.from_unit_menu = OptionMenu(self.input_subframe, self.from_unit_var, *c.UNITS) self.from_unit_menu.config(font=("Arial", "12"), width=5) self.from_unit_menu.grid(row=0, column=1) # "to" label + to-unit dropdown self.to_subframe = Frame(self.dist_frame) self.to_subframe.grid(row=3, pady=(0, 10)) to_label = Label(self.to_subframe, text="Convert to:", font=("Arial", "12")) to_label.grid(row=0, column=0, padx=(0, 10)) self.to_unit_var = StringVar(value=c.UNITS[1]) self.to_unit_menu = OptionMenu(self.to_subframe, self.to_unit_var, *c.UNITS) self.to_unit_menu.config(font=("Arial", "12"), width=5) self.to_unit_menu.grid(row=0, column=1) error = "Please enter a number" self.answer_error = Label(self.dist_frame, text=error, fg="#004C99", font=("Arial", "14", "bold")) self.answer_error.grid(row=4) # Conversion, help and history / export buttons self.button_frame = Frame(self.dist_frame) self.button_frame.grid(row=5) # button list (button text | bg colour | command | row | column) button_details_list = [ ["Convert", "#009900", lambda: self.check_distance(), 0, 0], ["Clear", "#666666", "", 0, 1], ["Help / Info", "#CC6600", "", 1, 0], ["History / Export", "#004C99", "", 1, 1] ] # List to hold buttons once they have been made self.button_ref_list = [] for item in button_details_list: self.make_button = Button(self.button_frame, text=item[0], bg=item[1], fg="#FFFFFF", font=("Arial", "12", "bold"), width=12, command=item[2]) self.make_button.grid(row=item[3], column=item[4], padx=5, pady=5) self.button_ref_list.append(self.make_button) # retrieve 'history / export' button and disable it at the start self.to_history_button = self.button_ref_list[3].config(state=DISABLED) def check_distance(self): print("From unit:", self.from_unit_var.get()) print("To unit: ", self.to_unit_var.get()) # Retrieve distance to be converted to_convert = self.dist_entry.get() print("To convert:", to_convert) # Reset label and entry box (if we had an error) self.answer_error.config(fg="#004C99") self.dist_entry.config(bg="#FFFFFF") error = "" try: to_convert = float(to_convert) if to_convert >= 0: self.convert(to_convert) else: error = "Please enter a number greater than or equal to 0" except ValueError: error = "Please enter a number" if error != "": self.answer_error.config(text=error, fg="#9C0000") self.dist_entry.config(bg="#F4CCCC") self.dist_entry.delete(0, END) def convert(self, to_convert): from_unit = self.from_unit_var.get() to_unit = self.to_unit_var.get() if from_unit == to_unit: self.answer_error.config(text="Please choose two different units", fg="#9C0000") else: self.answer_error.config( text=f"Converting {to_convert} {from_unit} to {to_unit}" ) # main routine if __name__ == "__main__": root = Tk() root.title("Distance Converter") Converter() root.mainloop()