-
Notifications
You must be signed in to change notification settings - Fork 0
/
exam_schedule.py
211 lines (155 loc) · 7.29 KB
/
exam_schedule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'''
PROJECT NAME: TIME TABLE MANAGEMENT SYSTEM FOR EDUCATION INSTITUTES
PROJECT MODULE: CONNECTING SQL WITH PYTHON AND CREATING GUI
'''
import tkinter
from tkinter import *
from tkinter import messagebox
#PIL module is for inserting images in your GUI
import PIL
from PIL import Image, ImageTk
# database connectivity
# import pymysql for MySQL
# import pyodbc for SQL server management studio
import pyodbc
# for mail purpose
from fileinput import filename
import os
import smtplib
from email.message import EmailMessage
# extracting data from excel
import pandas as pd
''' --------------------- DESIGNING GUI of the window (text and text Fields) --------------------- '''
examWindow = tkinter.Tk()
# windows size
examWindow.geometry('1000x800')
#examWindow.title is used to name your GUI
examWindow.title("sagacity institution")
img = ImageTk.PhotoImage(
file='C:/Users/Rohan/Desktop/wipro internship/apple-book.jpg')
lab = Label(examWindow, image=img).place(x=0, y=1)
'''setting up text charactristics
Label () is used normal text
'''
L = Label(examWindow, text="Enter Exam Schedule", font = ('Times New Roman',20,'bold'), fg = 'blue',highlightbackground='yellow')
# location of that field
L.place(x=400,y=20)
LSub1 = Label(examWindow, text="Subject Name:", font = ('arial',15), fg = 'black')
LSub1.place(x=100,y=100)
''' setting up textfield characteristics
Entry() is used to set up a textfield
'''
ESub1 = Entry(examWindow, bd = 5, width=30) # border = bd
ESub1.place(x=400,y=100)
LSub1 = Label(examWindow, text="Subject Code:", font = ('arial',15), fg = 'black')
LSub1.place(x=100,y=150)
ESub2 = Entry(examWindow, bd = 5, width=20) # border = bd
ESub2.place(x=400,y=150)
LDate1 = Label(examWindow, text="Date (format: 'yyyy-mm-dd'):", font = ('arial',15), fg = 'black')
LDate1.place(x=100,y=200)
EDate1 = Entry(examWindow, bd = 5, width=20) # border = bd
EDate1.place(x=400,y=200)
LTime1 = Label(examWindow, text="Start Time ('hh:mm'):", font = ('arial',15), fg = 'black')
LTime1.place(x=100,y=250)
ETime1 = Entry(examWindow, bd = 5, width=20) # border = bd
ETime1.place(x=400,y=250)
LTime2 = Label(examWindow, text="End Time ('hh:mm'):", font = ('arial',15), fg = 'black')
LTime2.place(x=100,y=300)
ETime2 = Entry(examWindow, bd = 5, width=20) # border = bd
ETime2.place(x=400,y=300)
''' --------------------- DATABASE CONNECTIVITY --------------------- '''
# events
def myButtonEvent(selection):
SubName = ESub1.get()
SubCode = ESub2.get()
examDate = EDate1.get()
startTime = ETime1.get()
endTime = ETime2.get()
'''Inserting data'''
if selection in ("Insert"):
# database connectivity code must be written under button method
conn_str = (
r'DRIVER={SQL Server};'
r'SERVER=ROHAN\SQLEXPRESS;'
r'DATABASE=pythonConnection;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
# executing sql codes with cnxn.execute
cnxn.execute('''if OBJECT_ID(N'examSchedule',N'U') is null begin create table examSchedule(SubName char(20) Not Null, SubCode varchar(10), examDate DATE NOT NULL, startTime time(7) NOT NULL, endTime time(7) NOT NULL) end''') # sql commands
cnxn.commit()
insertQuery = "insert into examSchedule values('%s', '%s', '%s','%s','%s')" % (SubName, SubCode, examDate, startTime, endTime)
cnxn.execute(insertQuery)
cnxn.commit()
# closing the connection of MsSQL
cnxn.close()
messagebox.showinfo(title="EXAM SCHEDULE", message="Data Inserted successfully !")
# UPDATING DATA
elif selection in ("Update"):
# database connectivity code must be written under button method
conn_str = (
r'DRIVER={SQL Server};'
r'SERVER=ROHAN\SQLEXPRESS;'
r'DATABASE=pythonConnection;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
cnxn.execute("update examSchedule set examDate='%s'" % (examDate)+", startTime='%s'" %(startTime)+", endTime='%s'" % (endTime)+" where SubCode = '%s'" % (SubCode))
cnxn.commit()
cnxn.close()
messagebox.showinfo(title="EXAM SCHEDULE", message="Data of %s Updated Successfully !" %(SubCode))
# DELETING DATA
elif selection in ("Delete"):
# database connectivity code must be written under button method
conn_str = (
r'DRIVER={SQL Server};'
r'SERVER=ROHAN\SQLEXPRESS;'
r'DATABASE=pythonConnection;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
cnxn.execute("delete from examSchedule where SubCode='%s'"% (SubCode))
cnxn.commit()
cnxn.close()
messagebox.showinfo(title="EXAM SCHEDULE", message="Data Deleted successfully !")
# SEND MAIL
elif selection in ("senDmail"):
# grab the email_address and password from environment vairables for safety
EMAIL_ADDRESS = os.environ.get('email_user')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')
# extracting EMAILS from EXCEL sheet
filedata = pd.read_excel('emails.xlsx','Sheet1') # table name, sheet name
emailsList = filedata['email'].values.tolist()
msg = EmailMessage()
# subject of mail
msg['Subject'] = 'Python Test Mail'
# sender
msg['From'] = 'EMAIL_ADDRESS'
# receiver
msg['To'] = ",".join(emailsList)
# body of the mail
msg.set_content('Exam Scheduled attached...')
# path of an image
with open('C:/Users/Rohan/Desktop/wipro internship/emails.xlsx', 'rb') as f: # 'rb' --> read byte
file_data = f.read()
file_name = f.name
msg.add_attachment(file_data, maintype='application', subtype='xlsx', filename=file_name)
# subtype = 'pdf' --> for pdf
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD) # emaiID, password
# send mail
smtp.send_message(msg)
messagebox.showinfo(title="EXAM SCHEDULE", message="Mail Sent Successfully !")
''' --------------------- DESIGNING GUI of the window (BUTTONS) --------------------- '''
'''
tkinter.Button() is used to create and manipulate a button
connecting myButtonEvent() using command '''
insertButton = tkinter.Button(text='Insert', fg="black", bg='yellow',font=('arial', 10,'bold'), command=lambda:myButtonEvent('Insert'))
insertButton.place(x=200,y=500)
updateButton = tkinter.Button(text='Update', fg="white", bg='orange',font=('arial', 10,'bold'), command=lambda:myButtonEvent('Update'))
updateButton.place(x=400,y=500)
deleteButton = tkinter.Button(text='Delete', fg="red", bg='white', font=('arial', 10, 'bold'), command=lambda: myButtonEvent('Delete'))
deleteButton.place(x=600,y=500)
sendMail = tkinter.Button(text='SHARE', fg="white", bg='red', font=('arial', 10, 'bold'), command=lambda: myButtonEvent('senDmail'))
sendMail.place(x=400, y=600)
mainloop()