0% found this document useful (0 votes)
5 views4 pages

Python GUI

Uploaded by

stavankalkumbe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Python GUI

Uploaded by

stavankalkumbe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

GUI In Python

>>Code:
import tkinter as tk
from tkinter import messagebox
import sqlite3

# Initialize Tkinter Window


t = tk.Tk()
t.geometry("800x500")
t.title("Fitness Tracker")

# Connect to SQLite database


conn = sqlite3.connect('FitnessTracker.db')
cursor = conn.cursor() # Create a cursor object

# Create table if it doesn't already exist


cursor.execute('''
CREATE TABLE IF NOT EXISTS exercises (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
duration INTEGER NOT NULL
)
''')
conn.commit()

# Calorie burn rates per exercise


dictionary_calories = {
"Running": 7,
"Swimming": 5,
"PUSH-UPS": 8,
"Jumping Rope": 12
}

def add_exercise():
try:
duration = int(duration_entry.get())
exercise = exercise_var.get()

if exercise == "Select Exercise":


messagebox.showerror("Input Error", "Select an appropriate Exercise")
return

# Insert data into the database


cursor.execute("INSERT INTO exercises(name, duration) VALUES(?, ?)",
(exercise, duration))
conn.commit()

result_label.config(text=f"{exercise} for {duration} mins added.")

duration_entry.delete(0, tk.END)

except ValueError:
messagebox.showerror("Input Error", "Please Enter a Valid Duration")

def calculate_total():
cursor.execute("SELECT name, duration FROM exercises")
exercises = cursor.fetchall()

total_calories = sum(duration * dictionary_calories.get(exercise, 0) for exercise,


duration in exercises)
result_label.config(text=f"Total Calories Burned: {total_calories}")

def clear_database():
cursor.execute("DELETE FROM exercises")
conn.commit()
result_label.config(text="Data cleared.")

# GUI Elements
title_label = tk.Label(t, text="-- Fitness Tracker --", font=("Helvetica", 18, "bold"))
title_label.pack(pady=20)

work_out = tk.Label(t, text="Choose Exercise:", font=("Helvetica", 14))


work_out.pack()

exercise_var = tk.StringVar()
exercise_var.set("Select Exercise")

exercise_option = ["Running", "Swimming", "PUSH-UPS", "Jumping Rope"]


exercise_menu = tk.OptionMenu(t, exercise_var, *exercise_option)
exercise_menu.pack()

duration_label = tk.Label(t, text="Enter Duration of Exercise (in min):",


font=("Helvetica", 14))
duration_label.pack(pady=10)

duration_entry = tk.Entry(t, width=18)


duration_entry.pack()

add_button = tk.Button(t, text="ADD", command=add_exercise, font=("Helvetica",


14))
add_button.pack(pady=10)

calculate_button = tk.Button(t, text="CALCULATE", command=calculate_total,


font=("Helvetica", 14))
calculate_button.pack(pady=10)

clear_button = tk.Button(t, text="CLEAR DATA", command=clear_database,


font=("Helvetica", 14))
clear_button.pack(pady=10)

result_label = tk.Label(t, text="", font=("Helvetica", 16))


result_label.pack()

# Run the Tkinter event loop


t.mainloop()

# Close the database connection after the main loop


conn.close()

Explanation:

calories_per_minute = {
"Running": 10, # Key: "Running", Value: 10
"Yoga": 3, # Key: "Yoga", Value: 3
"Cycling": 8 # Key: "Cycling", Value: 8
}

value = dictionary.get(key, default_value)

calories_per_minute = {
"Running": 10,
"Yoga": 3,
"Cycling": 8
}
# Fetching calories burned per minute for Running
calories_running = calories_per_minute.get("Running", 0)
print(calories_running) # Output: 10

You might also like