STAY INFORMED
following content serves as a personal note and may lack complete accuracy or certainty.

Minimal-Mistakes instruction
Useful vscode Shortcut Keys
Unix Commands
npm Commands
Vim Commands
Git Note
Useful Figma Shortcut Keys

2 minute read

Now time to create conversion factor class. I will skip the code for creating window. It just needs 3 inputs(value, convert from symbol, convert to symbol) and 1 button to get the result.

inputs

symbol_info = [("tsp"), ("tbsp"), ("cup"), ("mL"), ("oz"), ("°C"), ("°F") ... ]
self.convert_from = ttk.Combobox(self.frame, values=symbol_info, width=5)
self.convert_from.place(x=90, y=50)
self.convert_to = ttk.Combobox(self.frame, values=symbol_info, width=5)
self.convert_to.place(x=240, y=50)

self.label = Label(self.frame, text="convert to", font=14)
self.label.place(x=170, y=50)

self.convertBtn = Button(self.frame, text="Convert", font=14, command=self.do_this)
self.convertBtn.place(x=120, y=110)

functions

I think it will need a lot of jobs since there are many different symbol_info. First, create exception

def do_this(self):
    try:
        input_val = self.entry.get()
        from_val = self.convert_from.get()
        to_val = self.convert_to.get()

    except:
        showerror("Error", "Please type value in entry box")

    else:
        showinfo("Result", input_val + from_val + to_val)

and create calculate function using elif It will be big job, so I will test to check whether it is working.

def do_this(self):
    try:
        input_val = float(self.entry.get())
        from_val = self.convert_from.get()
        to_val = self.convert_to.get()
        output_val = 0

    except:
        showerror("Error", "Please type value in entry box")

    else:
        if from_val == "kg" and to_val == "g":
            output_val = round(input_val * 1000, 2)
            showinfo("Result", str(output_val) + f" {to_val}")

        else:
            showerror("Error", "Invalid Conversion")

img It works. I figured out it can be also optimized using dictionary. But if the from_val and to_val is same, It will give error because there is no key in dictionary for the same value, so I will add some code to handle it.

CONVERSION_FACTORS = {
    ("kg", "g"): 1000,
    ("g", "kg"): 1 / 1000,
    ("kg", "lb"): 1 / 0.454,
    .
    .
    .
    }
if from_val == to_val:
    showerror("Error", "You have chosen same values to convert")
elif (from_val, to_val) in CONVERSION_FACTORS:
    conversion_factor = CONVERSION_FACTORS[(from_val, to_val)]
    output_val = round(input_val * conversion_factor, 2)
    showinfo("Result", f"{output_val} {to_val}")
else:
    showinfo("Error", f"Conversion from {from_val} to {to_val} not supported")

and converting temperature format is a bit complicated, so I will add some elif to calcualte it.

elif from_val == "°C" and to_val == "°F":
    output_val = round((input_val * (9/5)) + 32)
    showinfo("Result", f"{output_val} {to_val}")

elif from_val == "°F" and to_val == "°C":
    output_val = round((input_val - 32) * (5/9))
    showinfo("Result", f"{output_val} {to_val}")
Learned New

I found ‘pint’ which is conversion library in python. you need to install pint to use the library

pip install pint

and import

from pint import UnitRegistry

and declare two variables

ureg = UnitRegistry() # creates an instance of the UnitRegistry class provided by the pint library.
Q_ = ureg.Quantity # creates an alias Q_ for the Quantity class within the ureg (UnitRegistry) instance.

you can code like this

CONVERSION_FACTORS = {
"kg": ureg.kilogram,
"g": ureg.gram,
"lb": ureg.pound,
"tsp": ureg.teaspoon,
.
.
.
}
try:
    input_quantity = Q_(input_val, from_unit)
    output_quantity = round(input_quantity.to(to_unit), 1)
    showinfo("Result", f"{output_quantity.magnitude} {to_unit}")
except:
    showerror("Error", f"Conversion from {from_unit} to {to_unit} not supported")

more info