· 6 years ago · Aug 18, 2019, 03:16 PM
1# This is a simple Database management System using python and sqlite3
2# To run this program you have to install "python3-tkinter" and "sqlite3" on your computer
3# Code written by Loknath Dhar - written on 16 & 17 Aug, 2019
4# Code last tested on 18 Aug 2019, 9.09 PM
5
6from tkinter import * #GUI package
7import sqlite3 as sq #For tables and database
8
9window = Tk()
10window.title("Kids Progress")
11window.geometry('1280x720+0+0')
12header = Label(window, text="Kids Progress Management", font=("arial",30,"bold"), fg="steelblue").pack()
13
14kidprog = sq.connect('Kids_progress.db') #dB browser for sqlite needed
15cursor = kidprog.cursor() #SQLite command, to connect to db so 'execute' method can be called
16
17
18Lab1 = Label(window, text = "Term:", font=("arial", 18)).place(x=15,y=150)
19Lab2 = Label(window, text = "Enter kids ID:", font=("arial",18)).place(x=15,y=200)
20Lab3 = Label(window, text = "Bangla:", font=("arial",18)).place(x=15,y=250)
21Lab4 = Label(window, text = "Math:", font=("arial",18)).place(x=15,y=300)
22Lab5 = Label(window, text = "English:", font=("arial",18)).place(x=15,y=350)
23
24# Create variables for each list
25
26kid = StringVar(window) #For 1st dd
27kid.set('----') #Inital placeholder for field
28
29kiddb = StringVar(window) #2nd dropdown list
30kiddb.set('----')
31
32Id = StringVar(window)
33Bangla = StringVar(window)
34Math = StringVar(window)
35English = StringVar(window)
36
37#Dictionary for drop down list
38
39Term = {'Final', 'Mid', 'First'}
40
41term = OptionMenu(window, kid, *Term) #For 1st drop down list
42term.place(x=220,y=155)
43
44kidbase = OptionMenu(window, kiddb, *Term)#For 2nd drop down list
45kidbase.place(x=700,y=155)
46
47# getting input for GUI
48
49KidID = Entry(window, textvariable=Id)
50KidID.place(x=220,y=205)
51
52BanNum = Entry(window, textvariable=Bangla)
53BanNum.place(x=220,y=255)
54
55MathNum = Entry(window, textvariable=Math)
56MathNum.place(x=220,y=305)
57
58EngNum = Entry(window, textvariable=English)
59EngNum.place(x=220,y=355)
60
61#get func to isolate the text entered in the entry boxes and submit to database
62def get():
63 print("You have submitted a record")
64
65 cursor.execute('CREATE TABLE IF NOT EXISTS ' +kid.get()+ ' (ID INT, Bangla INT, Math INT, English INT)') #SQL syntax
66
67 # date = datetime.date(int(year.get()),int(month.get()), int(day.get())) #Date in format from 'import datetime'
68
69 cursor.execute('INSERT INTO ' +kid.get()+ ' (Id, Bangla, Math, English) VALUES (?, ?, ?, ?)',
70 (Id.get(), Bangla.get(), Math.get(), English.get())) #Insert record into database.
71 kidprog.commit()
72
73#Reset fields after submit
74 kid.set('----')
75 Id.set('')
76 Bangla.set('')
77 Math.set('')
78 English.set('')
79
80#Clear boxes when submit button is hit
81def clear():
82 kid.set('----')
83 kiddb.set('----')
84 Id.set('')
85 Bangla.set('')
86 Math.set('')
87 English.set('')
88
89def record():
90 cursor.execute('SELECT * FROM ' +kiddb.get()) #Select from which ever compound lift is selected
91
92 frame = Frame(window)
93 frame.place(x=730, y =300)
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, 'ID | Bangla | Math | English') #first row in listbox
105
106 data = cursor.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 = kiddb.get()+ ' ',
112 font=("arial", 20)).place(x=800,y=255) # 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), fg="orange").place(x=700,y=205)
116
117 kidprog.commit()
118
119button_1 = Button(window, text="Submit",command=get)
120button_1.place(x=350,y=400)
121
122#button_2 = Button(window,text= "Clear",command=clear)
123#button_2.place(x=450,y=400)
124
125button_3 = Button(window,text="Open DB",command=record)
126button_3.place(x=800,y=155)
127
128
129window.mainloop() #mainloop() -> make sure that window stays open