forked from LuisOchoa1495/Python-Tkinter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
350 lines (290 loc) · 18.1 KB
/
main.py
File metadata and controls
350 lines (290 loc) · 18.1 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
"""
FORMULARIO DE LOGIN
Ingresar al sistema con su dni y contraseña
Mostrar messagebox
"""
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
#Python image Library
from PIL import ImageTk, Image
import sqlite3
from Buscador import Producto
class Login:
db_name='database_proyecto.db'
def __init__(self,ventana_login):
self.window=ventana_login
self.window.title("INGRESAR AL SISTEMA")
self.window.geometry("330x370")
self.window.resizable(0,0)
self.window.config(bd=10)
"--------------- Titulo --------------------"
titulo= Label(ventana_login, text="INICIAR SESION",fg="black",font=("Comic Sans", 13,"bold"),pady=10).pack()
"--------------- Loginlogo --------------------"
imagen_login=Image.open("D:/EIGHTA/PYTHON-TKINTER/SISTEMA DESKTOP/Imagenes/login.png")
nueva_imagen=imagen_login.resize((40,40))
render=ImageTk.PhotoImage(nueva_imagen)
label_imagen= Label(ventana_login, image= render)
label_imagen.image=render
label_imagen.pack(pady=5)
"--------------- Marco --------------------"
marco = LabelFrame(ventana_login, text="Ingrese sus datos",font=("Comic Sans", 10,"bold"))
marco.config(bd=2)
marco.pack()
"--------------- Formulario --------------------"
label_dni=Label(marco,text="DNI: ",font=("Comic Sans", 10,"bold")).grid(row=0,column=0,sticky='s',padx=5,pady=10)
self.dni_login=Entry(marco,width=25)
self.dni_login.focus()
self.dni_login.grid(row=0, column=1, padx=5, pady=10)
label_nombres=Label(marco,text="Contraseña: ",font=("Comic Sans", 10,"bold")).grid(row=1,column=0,sticky='s',padx=10,pady=10)
self.password_login=Entry(marco,width=25,show="*")
self.password_login.grid(row=1, column=1, padx=10, pady=10)
"--------------- Frame botones --------------------"
frame_botones=Frame(ventana_login)
frame_botones.pack()
"--------------- Botones --------------------"
boton_ingresar=Button(frame_botones,text="INGRESAR",command=self.Login,height=2,width=12,bg="green",fg="white",font=("Comic Sans", 10,"bold")).grid(row=0, column=1, padx=10, pady=15)
boton_registrar=Button(frame_botones,text="REGISTRAR",command=self.Ventana_registrar_usuario,height=2,width=12,bg="blue",fg="white",font=("Comic Sans", 10,"bold")).grid(row=0, column=2, padx=10, pady=15)
label_=Label(frame_botones,text="⬇ ¿Olvido su contraseña? ⬇",font=("Comic Sans", 10,"bold")).grid(row=1,column=1,columnspan=2,sticky='s')
boton_olvido=Button(frame_botones,text="RECUPERAR CONTRASEÑA",command=self.Ventana_recuperar_password ,height=2,width=24,bg="gray",fg="white",font=("Comic Sans", 10,"bold")).grid(row=2, column=1, columnspan=2, padx=10, pady=8)
def Validar_login(self, dni, password):
with sqlite3.connect(self.db_name) as conexion:
cursor=conexion.cursor()
sql= f"SELECT * FROM Usuarios WHERE DNI = {dni} AND Contraseña = '{password}'"
cursor.execute(sql)
validacion= cursor.fetchall() # obtener respuesta como lista
cursor.close()
return validacion
def Validar_formulario_completo(self):
if len(self.dni_login.get()) !=0 and len(self.password_login.get()) !=0:
return True
else:
messagebox.showerror("ERROR DE INGRESO", "Ingrese su DNI y contraseña!!!")
self.Limpiar_login()
def Limpiar_login(self):
self.dni_login.delete(0, END)
self.password_login.delete(0, END)
def Login(self):
try:
if(self.Validar_formulario_completo()):
dni= self.dni_login.get()
password= self.password_login.get()
dato = self.Validar_login(dni, password)
if (dato != []):
messagebox.showinfo("BIENVENIDO", "Datos ingresados correctamente")
Producto.__init__(self,ventana_producto=ventana_login)
else:
messagebox.showerror("ERROR DE INGRESO", "DNI o contraseña incorrecto")
self.Limpiar_login()
except:
messagebox.showerror("ERROR", "Ha ocurrido un error, reinicie el programa")
self.Limpiar_login()
"--------------------------------------------- REGISTRAR USUARIO --------------------------------------------------"
def Ventana_registrar_usuario(self):
self.Ventana_registrar=Toplevel()
self.Ventana_registrar.title("FORMULARIO DE REGISTRO")
self.Ventana_registrar.geometry("390x630")
self.Ventana_registrar.resizable(0,0)
self.Ventana_registrar.config(bd=10)
"--------------- Titulo --------------------"
titulo= Label(self.Ventana_registrar, text="REGISTRO DE USUARIO",fg="black",font=("Comic Sans", 13,"bold"),pady=5).pack()
"--------------- Nuevo usuario logo --------------------"
imagen_registro=Image.open("D:/EIGHTA/PYTHON-TKINTER/SISTEMA DESKTOP/Imagenes/nuevo_usuario.png")
nueva_imagen=imagen_registro.resize((40,40))
render=ImageTk.PhotoImage(nueva_imagen)
label_imagen= Label(self.Ventana_registrar, image= render)
label_imagen.image=render
label_imagen.pack(pady=5)
"--------------- Marco --------------------"
marco = LabelFrame(self.Ventana_registrar, text="Datos personales",font=("Comic Sans", 10,"bold"))
marco.config(bd=2,pady=5)
marco.pack()
"--------------- Formulario --------------------"
label_dni=Label(marco,text="DNI: ",font=("Comic Sans", 10,"bold")).grid(row=0,column=0,sticky='s',padx=5,pady=8)
self.dni=Entry(marco,width=25)
self.dni.focus()
self.dni.grid(row=0, column=1, padx=5, pady=8)
label_nombres=Label(marco,text="Nombre: ",font=("Comic Sans", 10,"bold")).grid(row=1,column=0,sticky='s',padx=10,pady=8)
self.nombres=Entry(marco,width=25)
self.nombres.grid(row=1, column=1, padx=10, pady=8)
label_apellidos=Label(marco,text="Apellidos: ",font=("Comic Sans", 10,"bold")).grid(row=2,column=0,sticky='s',padx=10,pady=8)
self.apellidos=Entry(marco,width=25)
self.apellidos.grid(row=2, column=1, padx=10, pady=8)
label_sexo=Label(marco,text="Sexo: ",font=("Comic Sans", 10,"bold")).grid(row=3,column=0,sticky='s',padx=10,pady=8)
self.combo_sexo=ttk.Combobox(marco,values=["Masculino", "Femenino"], width=22,state="readonly")
self.combo_sexo.current(0)
self.combo_sexo.grid(row=3,column=1,padx=10,pady=8)
label_edad=Label(marco,text="Edad: ",font=("Comic Sans", 10,"bold")).grid(row=4,column=0,sticky='s',padx=10,pady=8)
self.edad=Entry(marco,width=25)
self.edad.grid(row=4, column=1, padx=10, pady=8)
label_correo=Label(marco,text="Correo electronico: ",font=("Comic Sans", 10,"bold")).grid(row=5,column=0,sticky='s',padx=10,pady=8)
self.correo=Entry(marco,width=25)
self.correo.grid(row=5, column=1, padx=10, pady=8)
label_password=Label(marco,text="Contraseña: ",font=("Comic Sans", 10,"bold")).grid(row=6,column=0,sticky='s',padx=10,pady=8)
self.password=Entry(marco,width=25,show="*")
self.password.grid(row=6, column=1, padx=10, pady=8)
label_password=Label(marco,text="Repetir contraseña: ",font=("Comic Sans", 10,"bold")).grid(row=7,column=0,sticky='s',padx=10,pady=8)
self.repetir_password=Entry(marco,width=25,show="*")
self.repetir_password.grid(row=7, column=1, padx=10, pady=8)
"--------------- Marco pregunta --------------------"
marco_pregunta = LabelFrame(self.Ventana_registrar, text="Si olvidas tu contraseña",font=("Comic Sans", 10,"bold"),pady=10)
marco_pregunta.config(bd=2,pady=5)
marco_pregunta.pack()
"--------------- Pregunta --------------------"
label_pregunta=Label(marco_pregunta,text="Pregunta: ",font=("Comic Sans", 10,"bold")).grid(row=0,column=0,sticky='s',padx=10,pady=8)
self.combo_pregunta=ttk.Combobox(marco_pregunta,values=["¿Nombre de tu primera mascota?","¿Lugar dónde fuiste al colegio?","¿En que ciudad naciste?","¿Cómo se llama tu equipo favorito?"], width=30,state="readonly")
self.combo_pregunta.current(0)
self.combo_pregunta.grid(row=0,column=1,padx=10,pady=8)
label_respuesta=Label(marco_pregunta,text="Respuesta: ",font=("Comic Sans", 10,"bold")).grid(row=1,column=0,sticky='s',padx=10,pady=8)
self.respuesta=Entry(marco_pregunta,width=33)
self.respuesta.grid(row=1, column=1, padx=10, pady=8)
label_nota=Label(marco_pregunta,text="*Esta respuesta te permitira recuperar tu contraseña.",font=("Comic Sans", 9,"bold"),foreground="blue").grid(row=2,column=0,columnspan=2,sticky='s',padx=10)
"--------------- Frame botones --------------------"
frame_botones=Frame(self.Ventana_registrar)
frame_botones.pack()
"--------------- Botones --------------------"
boton_registrar=Button(frame_botones,text="REGISTRAR",command=self.Registrar_usuario ,height=2,width=10,bg="green",fg="white",font=("Comic Sans", 10,"bold")).grid(row=0, column=1, padx=10, pady=15)
boton_limpiar=Button(frame_botones,text="LIMPIAR",command=self.Limpiar_formulario_registro ,height=2,width=10,bg="gray",fg="white",font=("Comic Sans", 10,"bold")).grid(row=0, column=2, padx=10, pady=15)
boton_cancelar=Button(frame_botones,text="CERRAR",command=self.Ventana_registrar.destroy ,height=2,width=10,bg="red",fg="white",font=("Comic Sans", 10,"bold")).grid(row=0, column=3, padx=10, pady=15)
self.Ventana_registrar.mainloop()
def Ejecutar_consulta(self, query, parameters=()):
with sqlite3.connect(self.db_name) as conexion:
cursor=conexion.cursor()
result=cursor.execute(query,parameters)
conexion.commit()
return result
def Limpiar_formulario_registro(self):
self.dni.delete(0, END)
self.nombres.delete(0, END)
self.apellidos.delete(0, END)
self.combo_sexo.delete(0, END)
self.edad.delete(0, END)
self.correo.delete(0, END)
self.password.delete(0, END)
self.repetir_password.delete(0, END)
self.combo_pregunta.delete(0, END)
self.respuesta.delete(0, END)
def Validar_formulario_completo_registro(self):
if len(self.dni.get()) !=0 and len(self.nombres.get()) !=0 and len(self.apellidos.get()) !=0 and len(self.combo_sexo.get()) !=0 and len(self.edad.get()) !=0 and len(self.password.get()) !=0 and len(self.repetir_password.get()) !=0 and len(self.correo.get()) !=0 and len(self.respuesta.get()) !=0:
return True
else:
messagebox.showerror("ERROR EN REGISTRO", "Complete todos los campos del formulario")
def Validar_contraseña_registro(self):
if(str(self.password.get()) == str(self.repetir_password.get())):
return True
else:
messagebox.showerror("ERROR EN REGISTRO", "Contraseñas no coinciden")
def Buscar_dni(self, dni):
with sqlite3.connect(self.db_name) as conexion:
cursor=conexion.cursor()
sql="SELECT * FROM Usuarios WHERE DNI = {}".format(dni)
cursor.execute(sql)
dnix= cursor.fetchall() # obtener respuesta como lista
cursor.close()
return dnix
def Validar_dni(self):
dni= self.dni.get()
dato = self.Buscar_dni(dni)
if (dato == []):
return True
else:
messagebox.showerror("ERROR EN REGISTRO", "DNI registrado anteriormente")
def Registrar_usuario(self):
if self.Validar_formulario_completo_registro() and self.Validar_contraseña_registro() and self.Validar_dni():
query='INSERT INTO Usuarios VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?)'
parameters = (self.dni.get(),self.nombres.get(),self.apellidos.get(),self.combo_sexo.get(),self.edad.get(),self.correo.get(),self.password.get(),self.respuesta.get())
self.Ejecutar_consulta(query, parameters)
messagebox.showinfo("REGISTRO EXITOSO", f'Bienvenido {self.nombres.get()} {self.apellidos.get()}')
print('USUARIO CREADO')
self.Limpiar_formulario_registro()
"--------------------------------------------- RECUPERAR CONTRASEÑA --------------------------------------------------"
def Ventana_recuperar_password(self):
self.Ventana_recuperar = Toplevel()
self.Ventana_recuperar.geometry("410x420")
self.Ventana_recuperar.title('RECUPERAR CONTRASEÑA')
self.Ventana_recuperar.resizable(0,0)
self.Ventana_recuperar.config(bd=10)
"--------------- Titulo --------------------"
titulo= Label(self.Ventana_recuperar, text="RECUPERAR CONTRASEÑA",fg="black",font=("Comic Sans", 13,"bold"),pady=8).pack()
"--------------- Recuperar password logo --------------------"
imagen_password=Image.open("D:/EIGHTA/PYTHON-TKINTER/SISTEMA DESKTOP/Imagenes/recuperar_contraseña.png")
nueva_imagen=imagen_password.resize((60,60))
render=ImageTk.PhotoImage(nueva_imagen)
label_imagen= Label(self.Ventana_recuperar, image= render)
label_imagen.image=render
label_imagen.pack(pady=5)
"--------------- Marco --------------------"
marco = LabelFrame(self.Ventana_recuperar, text="Datos de recuperacion",font=("Comic Sans", 10,"bold"))
marco.config(bd=2)
marco.pack()
"--------------- Formulario --------------------"
label_dni=Label(marco,text="DNI: ",font=("Comic Sans", 10,"bold")).grid(row=0,column=0,sticky='s',padx=5,pady=8)
self.dni=Entry(marco,width=25)
self.dni.focus()
self.dni.grid(row=0, column=1, padx=5, pady=8)
label_nota=Label(marco,text="*Seleccione una pregunta y brinde la respuesta correcta.",font=("Comic Sans", 9,"bold"),foreground="blue").grid(row=1,column=0,columnspan=2,sticky='s',padx=8)
label_pregunta=Label(marco,text="Pregunta: ",font=("Comic Sans", 10,"bold")).grid(row=2,column=0,sticky='s',padx=5,pady=8)
self.combo_pregunta=ttk.Combobox(marco,values=["¿Nombre de tu primera mascota?","¿Lugar dónde fuiste al colegio?","¿En que ciudad naciste?","¿Cómo se llama tu equipo favorito?"], width=30,state="readonly")
self.combo_pregunta.current(0)
self.combo_pregunta.grid(row=2,column=1,padx=5,pady=8)
label_respuesta=Label(marco,text="Respuesta: ",font=("Comic Sans", 10,"bold")).grid(row=3,column=0,sticky='s',padx=5,pady=8)
self.respuesta=Entry(marco,width=33)
self.respuesta.grid(row=3, column=1, padx=5, pady=8)
label_password=Label(marco,text="Nueva Contraseña: ",font=("Comic Sans", 10,"bold")).grid(row=4,column=0,sticky='s',padx=5,pady=8)
self.nuevo_password=Entry(marco,width=25,show="*")
self.nuevo_password.grid(row=4, column=1, padx=5, pady=8)
label_password=Label(marco,text="Repetir contraseña: ",font=("Comic Sans", 10,"bold")).grid(row=5,column=0,sticky='s',padx=10,pady=8)
self.repetir_password=Entry(marco,width=25,show="*")
self.repetir_password.grid(row=5, column=1, padx=5, pady=8)
"--------------- Frame botones --------------------"
frame_botones=Frame(self.Ventana_recuperar)
frame_botones.pack()
"--------------- Botones --------------------"
boton_recuperar=Button(frame_botones,text="RECUPERAR",command=self.Restablecer_contraseña ,height=2,width=10,bg="green",fg="white",font=("Comic Sans", 10,"bold")).grid(row=0, column=1, padx=10, pady=10)
boton_cancelar=Button(frame_botones,text="CANCELAR",command=self.Ventana_recuperar.destroy ,height=2,width=10,bg="red",fg="white",font=("Comic Sans", 10,"bold")).grid(row=0, column=3, padx=10, pady=10)
self.Ventana_recuperar.mainloop()
def Limpiar_formulario_recuperar(self):
self.dni.delete(0, END)
self.respuesta.delete(0, END)
self.nuevo_password.delete(0, END)
self.repetir_password.delete(0, END)
def Validar_formulario_completo_recuperar(self):
if len(self.dni.get()) !=0 and len(self.nuevo_password.get()) !=0 and len(self.repetir_password.get()) !=0 and len(self.respuesta.get()) !=0:
return True
else:
messagebox.showerror("ERROR", "Complete todos los campos del formulario")
def Validar_contraseña_recuperar(self):
if(str(self.nuevo_password.get()) == str(self.repetir_password.get())):
return True
else:
messagebox.showerror("ERROR DE RECUPERACION", "Contraseñas no coinciden")
def Buscar_usuario(self, dni, respuesta):
with sqlite3.connect(self.db_name) as conexion:
cursor=conexion.cursor()
sql=f"SELECT * FROM Usuarios WHERE DNI = {dni} AND Respuesta = '{respuesta}'"
cursor.execute(sql)
busqueda= cursor.fetchall() # obtener respuesta como lista
cursor.close()
return busqueda
def Validar_datos_usuario(self):
dni= self.dni.get()
respuesta=self.respuesta.get()
busqueda = self.Buscar_usuario(dni, respuesta)
if (busqueda != []):
return True
else:
messagebox.showerror("ERROR DE RECUPERACION", "Datos de recuperacion no son correctos")
def Restablecer_contraseña(self):
if self.Validar_formulario_completo_recuperar() and self.Validar_datos_usuario() and self.Validar_contraseña_recuperar():
query='UPDATE Usuarios SET Contraseña = (?) WHERE DNI= (?)'
parameters = (self.nuevo_password.get(), self.dni.get())
self.Ejecutar_consulta(query, parameters)
messagebox.showinfo("CONTRASEÑA RECUPERADA", f'Contraseña actualizada correctamente: {self.nuevo_password.get()}')
print('DATOS ACTUALIZADO')
self.Limpiar_formulario_recuperar()
self.Ventana_recuperar.destroy()
#verificar si el modulo ha sido ejecutado correctamente
if __name__ == '__main__':
ventana_login=Tk()
application=Login(ventana_login)
ventana_login.mainloop()