-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElection2020_GUI.py
298 lines (265 loc) · 8.35 KB
/
Election2020_GUI.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import random
from hashlib import md5
from tkinter import *
from PIL import ImageTk, Image
import tkinter.messagebox
import cx_Oracle
F=open('./.resources/connection.txt','r')
global connection_string
connection_string = F.read()
F.close()
def Query_DB(query):
connection = None
res = None
flag = False
try:
#create connection
connection = cx_Oracle.connect(connection_string)
cur = connection.cursor()
cur.execute(query)
res = cur.fetchall()
flag = True
except cx_Oracle.Error as error:
print(error)
flag = error
finally:
# release the connection
if connection:
connection.close()
return res,flag
def Inser_DB(stm):
connection = None
flag = False
try:
#create connection
connection = cx_Oracle.connect(connection_string)
cur = connection.cursor()
cur.execute(stm)
#cur.execute("commit")
flag = True
except cx_Oracle.Error as error:
print(error)
flag = error
finally:
# release the connection
if connection:
connection.commit()
#print("OK")
cur.close()
connection.close()
return flag
def randomPass():
def randomString():
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
return ''.join(random.choice(letters) for i in range(4))
def randomNum():
return ''.join(str(random.randint(0,9)) for i in range(4))
return randomString()+'@'+randomNum()
def Inscription():
def Inscription_(Lname,Fname):
Lname=Lname.upper()
Fname=Fname.upper()
result,flag = Query_DB("SELECT * FROM personnes")
for line in result:
if (line[0]==Lname) and (line[1]==Fname):
return False
return True
def click():
Lname=textentry1.get().upper()
Fname=textentry2.get().upper()
if(Inscription_(Lname,Fname)==False) :
tkinter.messagebox.showinfo("ALERT!!","vous etes deja inscrit!")
window2.destroy()
else:
num,flag = Query_DB("SELECT COUNT(*) FROM personnes")
if flag == True:
i=int(num[0][0])+1
if(i<10) :
code='E00'+str(i)
elif(i<100):
code='E0'+str(i)
else:
code='E'+str(i)
paswd=randomPass()
stm = f"INSERT INTO personnes VALUES('{Lname}','{Fname}','{code}','{str(md5(paswd.encode()).hexdigest())}','OK')"
Inser_DB(stm)
window4= Toplevel()
window4.geometry("250x100")
window4.title("INFO!!")
window4.resizable(0,0)
window4.configure(background=background_color)
Label(window4,text="inscription réussie",bg=background_color).pack()
output= Text(window4,width=25,height=10,wrap=WORD,background=background_color)
output.pack()
m="Login: "+code+"\nPassword: "+paswd
output.insert(END,m)
window2.destroy()
window4.iconbitmap('./.resources/Team-Male.ico')
window4.mainloop()
else:
tkinter.messagebox.showinfo("ERROR!!",str(flag))
window2 =Toplevel()
window2.title("Inscription")
window2.configure(background="#32a6a8")
window2.geometry("270x180")
window2.resizable(0, 0)
window2.iconbitmap('./.resources/Team-Male.ico')
Label(window2,text="Nom:",bg=background_color).place(x=10,y=10)
textentry1= Entry(window2,bg="white",width=38)
textentry1.place(x=10,y=30)
Label(window2,text="Prenom:",bg=background_color).place(x=10,y=60)
textentry2= Entry(window2,bg="white",width=38)
textentry2.place(x=10,y=80)
Button(window2,font="none 9 italic",fg="white",bg="black",text="Sign Up",command=click).place(x=190,y=120)
window2.mainloop()
def Liste_Candidat():
F,flag=Query_DB("SELECT * FROM candidats")
if flag == True:
i=1
R=""
for line in F:
R+=line[0]+" --> "+line[1]+" "+line[2]+'\n'
i+=1
#R+='C'+str(i)+" --> blanc\n"
tkinter.messagebox.showinfo("Liste des candidats",R)
return i
else:
tkinter.messagebox.showinfo("ERROR!!",str(flag))
def Statistiques():
D = DB_Dic()
n,flag=Query_DB("SELECT SUM(POINTS) FROM stats")
n = n[0][0]
if(n==0) :
n=1
A,flag = Query_DB("SELECT * FROM candidats")
if flag == True:
i=1
R=""
for a in A:
R+=str(a[1])+" "+str(a[2])+" --> "+str(D['C'+str(i)])+" votes "+str(D['C'+str(i)]*100/n)+"%"+'\n'
i+=1
tkinter.messagebox.showinfo("Statistiques",R)
else:
tkinter.messagebox.showinfo("ERROR!!",str(flag))
def Vote():
radio_var=StringVar()
def click1():
code=textentry1.get().upper()
paswd=str(md5(textentry2.get().upper().encode()).hexdigest())
window3.destroy()
F,flag=Query_DB("SELECT * FROM personnes")
if flag == True:
u=0
for line in F:
l = list(line)
if(l[-3]==code) and (l[-2]==paswd) :
u=1
tkinter.messagebox.showinfo("INFO","Bienvenue "+l[1]+" "+l[0])
if(l[-1]!='OK') :
tkinter.messagebox.showinfo("ALERT!!","vous avez deja voté!!! ")
return False
l[-1]='NO'
def click2():
window5.destroy()
D = DB_Dic()
choice=v.get()
if choice in D:
D[choice]+=1
stm = f"UPDATE stats SET POINTS = '{D[choice]}' WHERE CODE = '{choice}'"
Inser_DB(stm)
tkinter.messagebox.showinfo("ALERT!!","Merci pour votre vote!")
stm = f"UPDATE personnes SET STATUS='NO' WHERE LOGIN='{l[2]}'"
Inser_DB(stm)
else:
tkinter.messagebox.showinfo("ALERT!!","Contact your administrator to clear results!")
else:
tkinter.messagebox.showinfo("ERROR!!",str(flag))
if(u==0) :
tkinter.messagebox.showinfo("ALERT!!","code ou mot de passe incorrecte!")
return False
n=Liste_Candidat()
window5 = Toplevel()
window5.resizable(0, 0)
window5.title("Vote")
window5.iconbitmap('./.resources/vote.ico')
window5.configure(background=background_color)
#window5.geometry("225x200")
Label(window5,text="veuillez votez pour votre candidat",bg=background_color).pack()
values={}
for i in range(1,n):
C='C'+str(i)
values[C]=C
v = StringVar(window5, "1")
for (text, value) in values.items():
Radiobutton(window5,bg=background_color, text = text, variable = v, value = value).pack(side = TOP, ipady = 5)
Button(window5,font="none 9 italic",fg="white",bg="black",text="VOTE",command=click2).pack()
window5.mainloop()
window3 = Toplevel()
window3.resizable(0, 0)
window3.title("Login")
window3.iconbitmap('./.resources/Team-Male.ico')
window3.configure(background=background_color)
window3.geometry("225x150")
Label(window3,text="Saisir votre code(E...):",bg=background_color).place(x=20,y=10)
textentry1= Entry(window3,bg="white",width=30)
textentry1.place(x=20,y=30)
Label(window3,text="Password:",bg=background_color).place(x=20,y=60)
textentry2= Entry(window3,show='*',bg="white",width=30)
textentry2.place(x=20,y=80)
Button(window3,font="none 9 italic",fg="white",bg="black",text="Submit",command=click1).place(x=90,y=120)
window3.mainloop()
def Dic_candidat():
c=[]
C,flag=Query_DB("SELECT * FROM candidats")
if flag == True:
i=1
for line in C:
i+=1
c.append(line[0])
D={}
for e in c:
D[e]=0
return D
else:
tkinter.messagebox.showinfo("ERROR!!",str(flag))
def Dic_candidat_DB(D = Dic_candidat()):
Inser_DB("DELETE FROM stats")
for e in D:
stm = f"INSERT INTO stats VALUES('{e}','{D[e]}','NO')"
Inser_DB(stm)
def DB_Dic():
A,flag = Query_DB("SELECT CODE,POINTS FROM stats")
if flag == True:
D={}
for a in A:
D[a[0]] = a[1]
return D
else:
tkinter.messagebox.showinfo("ERROR!!",str(flag))
def clear_():
D=Dic_candidat()
Dic_candidat_DB()
Inser_DB("UPDATE personnes SET STATUS='OK'")
tkinter.messagebox.showinfo("ALERT!!","Les résultats sont mis à zéro.\ntous les membres puissent voter à nouveau!!")
def prcp_gui(background_color):
window.resizable(0, 0)
Button(window,font=button_font,fg="white",bg=button_color,width=20,text="Inscription",command=Inscription).place(x=20,y=170)
Button(window,font=button_font,fg="white",bg=button_color,width=20,text="Liste: code --> candidat",command=Liste_Candidat).place(x=420,y=170)
Button(window,font=button_font,fg="white",bg=button_color,width=20,text="Voter",command=Vote).place(x=20,y=220)
Button(window,font=button_font,fg="white",bg=button_color,width=20,text="Statistiques",command=Statistiques).place(x=420,y=225)
#Button(window,font=button_font,fg="white",bg=button_color,width=20,text="Clear Result",command=clear_).place(x=220,y=280)
background_color="#32a6a8"
button_color="#576161"
button_font="none 12 bold"
window = Tk()
window.geometry("653x350")
window.title("Election 2020 by ZKARA(©)")
window.configure(background=background_color)
window.iconbitmap('./.resources/vote.ico')
imge=Image.open("./.resources/Team Male.png")
photo=ImageTk.PhotoImage(imge)
lab=Label(image=photo,bg=background_color)
lab.place(x=272,y=20)
prcp_gui(background_color)
DB_Dic()
window.mainloop()