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

1 minute read

my github repo

Programming languages used

  • python

Introduction

I am taking chemistry class this semester, and there are some data sheets that contain metrix prefixes, conversion factor and so on. I got an idea how it would be to make a project with this

code

I will use tkinter for using GUI so import tkinter and add frame to make it more tidy.

import

from tkinter import *
from tkinter.messagebox import *

class MyGUI:

    def __init__(self):
        self.mainWindow = Tk()
        self.mainWindow.geometry(f"200x300+100+50")
        self.frame1 = Frame(self.mainWindow, width=180, height=280)
        self.frame1.place(x=10, y=10)

button

And add buttons to open new windows for

  • Metric Converter
  • Conversion Factor
  • Calculate BMI
  • Calculate Daily Basal Metabolic Requirement + Physical Activity Level
self.metricButton = Button(self.frame1, text="Metric Prefixes", font=("Arial", 14), width=15, command=self.MetricConverter)
self.metricButton.place(x=10, y=10)

self.convFacButton = Button(self.frame1, text="Conversion Factor", font=("Arial", 14), width=15, command=self.convFac)
self.convFacButton.place(x=10, y=60)

self.bmiButton = Button(self.frame1, text="BMI", font=("Arial", 14), width=15, command=self.bmi)
self.bmiButton.place(x=10, y=110)
.
.
.

new windows

And create functions to open new windows

def MetricConverter(self):
    self.metricWindow = Tk()
    self.metricWindow.geometry(f"500x800+520+50")


def convFac(self):
    self.convFacWindow = Tk()
    self.convFacWindow.geometry(f"500x800+620+50")
.
.
.
Learned New

I thought this is sort of bad idea to initialize, so I searched to optimize and I got better code.

template for creating windows

class BaseWindow:
    def __init__(self, title, geometry):
        self.window = Toplevel() # create instance for the window
        self.window.title(title)
        self.window.geometry(geometry)
        self.frame = Frame(self.window, width=500, height=800)
        self.frame.pack()

main class myGUI

This is main window

class MyGUI:

    def __init__(self):
        self.mainWindow = Tk()
        self.mainWindow.geometry("600x300+720+50")
        self.create_buttons()
        mainloop()


create buttons

create a list of tuples containing button text and the corresponding command to be executed when the button is clicked.

def create_buttons(self):
    # if button has text "Metric Prefixes" is clicked, call self.metric_prefixes
    buttons_info = [
        ("Metric Prefixes", self.metric_prefixes),
        ("Conversion Factor", self.conv_fac),
        ("BMI", self.bmi),
        ("DBMR", self.dbmr),
    ]
    # i has tuple(text, command)
    for i, (text, command) in enumerate(buttons_info):
        button = Button(self.mainWindow, text=text, font=("Arial", 14), width=15, command=command)
        button.place(x=10, y=10 + i * 50)

create functions

create functions to open windows

def metric_prefixes(self):
    MetricConverterWindow()

def conv_fac(self):
    WeightConverterWindow()

def bmi(self):
    BMIWindow()
.
.
.

create classes

create each class to do their roles

super() is a built-in function used in Python, which is used to access methods or properties of the parent class (super class).

class MetricConverterWindow(BaseWindow):

    def __init__(self):
        super().__init__("Metric Prefixes", "500x800+520+50")

class WeightConverterWindow(BaseWindow):

    def __init__(self):
        super().__init__("Conversion Factor", "500x800+620+50")
.
.
.

and you need to call myGUI()

my_gui = myGUI()