Above, we have my first Python GUI application. As you can tell, it is an app that helps you track your fitness goals. It is a completely beginner level app that I developed in one day, but i learned the core concepts of object oriented programming and processing data while building this app in the same way I learned HTML and CSS by developing this website (See About). If you'd like to make this app yourself, the source code is below.
Python code:
import tkinter as tk
from tkinter import ttk
LARGEFONT = ('Courier', 35)
#Window Initialization
class tkinterApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.config(background='#333333')
container = tk.Frame(self, bg='#333333')
container.pack(fill="both", expand=True)
self.center_window(700, 700)
tkinterApp.geometry(self, '700x700')
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, Page1, Page2):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky='nsew')
self.show_frame(StartPage)
def center_window(self, width, height):
# Get the screen width and height
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
# Calculate position x and y
position_top = int(screen_height / 2 - height / 2)
position_right = int(screen_width / 2 - width / 2)
# Set the dimensions of the window and its position
self.geometry(f'{width}x{height}+{position_right}+{position_top}')
#Show the Frame
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
#Start Page
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg='#333333')
tk.Frame.__init__(self, parent, bg='#333333')
label = ttk.Label(self, text='Fitness App', font=LARGEFONT, background='#333333')
label.grid(row=3, column=4, padx=200, pady=100)
button1 = ttk.Button(self, text='Begin', command=lambda: controller.show_frame(Page1),)
button1.grid(row=4, column=4, padx=200, pady=10)
#Page 1
class Page1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg='#333333')
tk.Frame.__init__(self, parent, bg='#333333')
label = ttk.Label(self, text='Athlete name:', font=('Courier', 15), background='#333333')
label.grid(row=0, column=0, padx=275, pady=20)
text_entry = ttk.Entry(self)
text_entry.grid(row=1, column=0, pady=10)
label = ttk.Label(self, text='height: in', font=('Courier', 15), background='#333333')
label.grid(row=2, column=0, padx=275, pady=20)
text_entry2 = ttk.Entry(self)
text_entry2.grid(row=3, column=0, pady=10)
label = ttk.Label(self, text='weight: lbs', font=('Courier', 15), background='#333333')
label.grid(row=4, column=0, padx=275, pady=20)
text_entry3 = ttk.Entry(self)
text_entry3.grid(row=5, column=0, pady=10)
button1 = ttk.Button(self, text='Continue', command=lambda: combined_command())
button1.grid(row=7, column=0, padx=10, pady=50)
button2 = ttk.Button(self, text='Restart', command=lambda: controller.show_frame(StartPage))
button2.grid(row=8, column=0, padx=10, pady=10)
def set_list(): #Save
name = text_entry.get() #Data
height = text_entry2.get()
weight = text_entry3.get()
data = [name, height, weight]
h2 = int(height) * int(height)
bmi = round((int(weight)/h2)*703, 1)
judge = 0
if bmi < 18.5:
judge = 1
elif bmi > 18.5 and bmi < 24.9:
judge = 2
elif bmi > 25.0 and bmi < 29.9:
judge = 3
elif bmi > 30.0 and bmi < 34.9:
judge = 4
elif bmi > 35.0:
judge = 5
return data, bmi, judge
def combined_command(): #Show
data, bmi, judge = set_list()
controller.show_frame(Page2) #Next Page
controller.frames[Page2].display_bmi(bmi, judge, data)
#Page 2
class Page2(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, bg='#333333')
self.image = tk.PhotoImage(file='meter2.png')
label_image = tk.Label(self, image=self.image, bg='#333333')
label_image.grid(row=3, column=0, pady=60, padx=130)
label = ttk.Label(self, text=f'BMI', font=LARGEFONT, background='#333333')
label.grid(row=0, column=0, padx=10)
self.bmi_label = ttk.Label(self, text='', font=('Courier', 15), background='#333333')
self.bmi_label.grid(row=1, column=0, padx=10, pady=20)
self.bmi_label2 = ttk.Label(self, text='', font=('Courier', 15), background='#333333')
self.bmi_label2.grid(row=2, column=0, pady=20)
button1 = ttk.Button(self, text='Page1', command=lambda: controller.show_frame(Page1))
button1.grid(row=5, column=0, padx=10, pady=10)
button2 = ttk.Button(self, text='Start Page', command=lambda: controller.show_frame(StartPage))
button2.grid(row=6, column=0, padx=10, pady=10)
def display_bmi(self, bmi, judge, data):
self.bmi_label.config(text=f'Your BMI: {bmi}', foreground='white')
if judge == 1:
self.bmi_label2.config(text=f'{data[0]}, consider eating in a calorie surplus.', foreground='white')
elif judge == 2:
self.bmi_label2.config(text=f'Looking healthy, {data[0]}!', foreground='white')
elif judge == 3:
self.bmi_label2.config(text=f'{data[0]}, consider eating less.', foreground='white')
elif judge == 4:
self.bmi_label2.config(text=f'{data[0]}, you are obese. Eat less & work out.', foreground='white')
elif judge == 5:
self.bmi_label2.config(text=f'You are extremely obese.', foreground='red')
#App Loops
app = tkinterApp()
app.mainloop()