· 6 years ago · Oct 09, 2019, 10:10 AM
1from tkinter import * #GUI package
2import sqlite3 as sq #For tables and database
3import datetime
4
5window = Tk()
6window.title("Compound Tracker")
7window.geometry('800x600+0+0')
8header = Label(window, text="Compound Tracker for Weightlifting", font=("arial",30,"bold"), fg="steelblue").pack()
9
10con = sq.connect('Gym.db') #dB browser for sqlite needed
11c = con.cursor() #SQLite command, to connect to db so 'execute' method can be called
12
13
14L1 = Label(window, text = "Compound Lift", font=("arial", 18)).place(x=10,y=100)
15L2 = Label(window, text = "Day (dd)", font=("arial",18)).place(x=10,y=150)
16L3 = Label(window, text = "Month (mm)", font=("arial",18)).place(x=10,y=200)
17L4 = Label(window, text = "Year (yyyy)", font=("arial",18)).place(x=10,y=250)
18L5 = Label(window, text = "Max Weight (KG)", font=("arial",18)).place(x=10,y=300)
19L6 = Label(window, text = "Reps", font=("arial",18)).place(x=10,y=350)
20
21#Create variables for each list
22comp = StringVar(window)#For 1st dd
23comp.set('----') #Inital placeholder for field
24
25compdb = StringVar(window)#2nd dropdown list
26compdb.set('----')
27
28day = StringVar(window)
29month = StringVar(window)
30year = StringVar(window)
31weight = StringVar(window)
32reps = StringVar(window)
33
34#Dictionary for drop down list
35compound = {'Bench', 'Squat', 'Deadlift','OVH'}
36
37compd = OptionMenu(window, comp, *compound) #For 1st drop down list
38compd.place(x=220,y=105)
39
40compdbase = OptionMenu(window, compdb, *compound)#For 2nd drop down list
41compdbase.place(x=100,y=500)
42
43#Entry for 'input' in GUI
44dayT = Entry(window, textvariable=day)
45dayT.place(x=220,y=155)
46
47monthT = Entry(window, textvariable=month)
48monthT.place(x=220,y=205)
49
50yearT = Entry(window, textvariable=year)
51yearT.place(x=220,y=255)
52
53weightT = Entry(window, textvariable=weight)
54weightT.place(x=220,y=305)
55
56repT = Entry(window, textvariable=reps)
57repT.place(x=220,y=355)
58
59#get func to isolate the text entered in the entry boxes and submit to database
60def get():
61 print("You have submitted a record")
62
63 c.execute('CREATE TABLE IF NOT EXISTS ' +comp.get()+ ' (Datestamp TEXT, MaxWeight INTEGER, Reps INTEGER)') #SQL syntax
64
65 date = datetime.date(int(year.get()),int(month.get()), int(day.get())) #Date in format from 'import datetime'
66
67 c.execute('INSERT INTO ' +comp.get()+ ' (Datestamp, MaxWeight, Reps) VALUES (?, ?, ?)',
68 (date, weight.get(), reps.get())) #Insert record into database.
69 con.commit()
70
71#Reset fields after submit
72 comp.set('----')
73 day.set('')
74 month.set('')
75 year.set('')
76 weight.set('')
77 reps.set('')
78
79#Clear boxes when submit button is hit
80def clear():
81 comp.set('----')
82 compdb.set('----')
83 day.set('')
84 month.set('')
85 year.set('')
86 weight.set('')
87 reps.set('')
88
89def record():
90 c.execute('SELECT * FROM ' +compdb.get()) #Select from which ever compound lift is selected
91
92 frame = Frame(window)
93 frame.place(x= 400, y = 150)
94
95 Lb = Listbox(frame, height = 8, width = 25,font=("arial", 12))
96 Lb.pack(side = LEFT, fill = Y)
97
98 scroll = Scrollbar(frame, orient = VERTICAL) # set scrollbar to list box for when entries exceed size of list box
99 scroll.config(command = Lb.yview)
100 scroll.pack(side = RIGHT, fill = Y)
101 Lb.config(yscrollcommand = scroll.set)
102
103
104 Lb.insert(0, 'Date, Max Weight, Reps') #first row in listbox
105
106 data = c.fetchall() # Gets the data from the table
107
108 for row in data:
109 Lb.insert(1,row) # Inserts record row by row in list box
110
111 L7 = Label(window, text = compdb.get()+ ' ',
112 font=("arial", 16)).place(x=400,y=100) # Title of list box, given which compound lift is chosen
113
114 L8 = Label(window, text = "They are ordered from most recent",
115 font=("arial", 16)).place(x=400,y=350)
116 con.commit()
117
118button_1 = Button(window, text="Submit",command=get)
119button_1.place(x=100,y=400)
120
121button_2 = Button(window,text= "Clear",command=clear)
122button_2.place(x=10,y=400)
123
124button_3 = Button(window,text="Open DB",command=record)
125button_3.place(x=10,y=500)
126
127
128window.mainloop() #mainloop() -> make sure that window stays open