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, I am going to create Metric Converter class

metric prefixes labels

I will use for loop to set the label.

class MetricConverterWindow(BaseWindow):
    def __init__(self):
        super().__init__("Metric Converter", "270x500+720+50")
        self.frame = Frame(self.window, width=250, height=480)
        self.frame.place(x=10, y=10)

        label_info = [("tera"), ("giga"), ("mega") ... ]

        for i, (text) in enumerate(label_info):
            label = Label(self.frame, text=text, font=14)
            label.place(x=10, y=10 + i * 25)
            setattr(self, f"label{i+1}", label)

The setattr function is used to dynamically set attributes of an object in Python. For example, setattr(self, “label1”, label) dynamically adds the attribute label1 to the self object and sets its value to the contents of the label variable.

Using this method, set symbol, factor labels.

symbol_info = [("T"), ("G"), ("M") ...]

        for i, (symbol) in enumerate(symbol_info):
            label = Label(self.frame, text=symbol, font=14)
            label.place(x=80, y=35 + i * 25)
            setattr(self, f"label{i+1}", label)


        factor_info = [("1 000 000 000 000"), ("1 000 000 000"), ("1 000 000") ...]

        for i, (factor) in enumerate(factor_info):
            label = Label(self.frame, text=factor, font=14)
            label.place(x=170, y=35 + i * 25)
            setattr(self, f"label{i+1}", label)

to add entry box between deca and deci, modify for loop a little.

for i, (text) in enumerate(text_info):
    label = Label(self.frame, text=text, font=14)
    if i >= 6:
        label.place(x=10, y=60 + i * 25)
    else:
        label.place(x=10, y=35 + i * 25)
    setattr(self, f"label{i+1}", label)

inputs

Now I am going to create input structures. I will use combo box to choose which factor to convert. We need to import ttk from tkinter

from tkinter import ttk

and this is input structure

self.entry = Entry(self.frame, width=8)
self.entry.place(x=10, y=195)
self.convertTo = Label(self.frame, text="Convert to", font=14)
self.convertTo.place(x=100, y=195)

# symbol info is [("T"), ("G"), ("M") ...]
self.factorValues = ttk.Combobox(self.frame, values=symbol_info, width=3)

self.factorValues.place(x=180, y=193)
self.convertBtn = Button(self.frame, text="Convert", font=("Arial", 14), width=7, command=self.do_this)
self.convertBtn.place(x=110, y=420)

function

I used elif statement to multiply by the combo box value, but it seems really bad code.

if factorVal == "T":
    outputVal = inputVal * 1000000000000
elif factorVal == "G":
    outputVal = inputVal * 1000000000
elif factorVal == "M":
    outputVal = inputVal * 1000000
    .
    .
    .

So I searched and find the optimized code.

FACTOR_MULTIPLIERS = {"T": 1e12, "G": 1e9, "M": 1e6, "K": 1e3 ... }

if factor_val in FACTOR_MULTIPLIERS:
    output_val = input_val * FACTOR_MULTIPLIERS[factor_val]

And I will add exception because if there is no input value, it gives an error. This is my final code.

def do_this(self):
    FACTOR_MULTIPLIERS = {"T": 1e12, "G": 1e9, "M": 1e6, "k": 1000, "h": 100, "da": 10,
                            "d": 0.1, "c": 0.01, "m":0.001, "µ": 1e-6, "n": 1e-9,
                            "p": 1e-12}
    try:
        # get values
        input_val = float(self.entry.get())
        factor_val = self.factor_values.get()
        output_val = 0

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

    else:
        if factor_val in FACTOR_MULTIPLIERS:
            output_val = input_val * FACTOR_MULTIPLIERS[factor_val]
            showinfo("Result", str(output_val) + factor_val)
        else:
            # if combo box is not selected
            showerror("Error", "Please select the factor")