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