creé un Login donde registro usuarios e inicio sesión pero ya que mis conocimientos de Python son casi nulos, sé que hay muchas lineas de código que se pueden llegar a simplificar y agradecería que me ayudaran.
Errores que sé que tengo: -si se dan cuenta tengo 3 funciones de registro donde pido exactamente los mismos datos, cambiando únicamente la variable "Position" -También tengo 3 funciones de confirm_password debido a que no se como hacer para que se muestren los mismos mensajes en diferentes pantallas.
PD: Sé que el código es muy básico y poco estético, si desean agregar cualquier cosa para mejorar la visualización del programa siéntanse en total libertad
Gracias
from tkinter import *
import os
def main_screen():
global screen
screen = Tk()
screen.geometry("300x250")
screen.title("Login")
Label(text= "Login 1.2", bg = "grey", width = "300", height = "2", font = ("calibri", 13)).pack()
Label(text = "").pack()
Button(text = "Login", height ="2", width = "30", command = login).pack()
Label(text = "").pack()
Button(text = "Register", height ="2", width = "30", command = kind_register).pack()
def login():
global screen2
screen2 = Toplevel(screen)
screen2.title("Login")
Label(screen2, text = "Please enter details below to login").grid(row=0, column=1, padx=10, pady=10)
global username_verify
global password_verify
username_verify = StringVar()
password_verify = StringVar()
global username_entry1
global password_entry1
Label(screen2, text = "Username:").grid(row=2, column=0, sticky="e", padx=10, pady=10)
username_entry1 = Entry(screen2, textvariable = username_verify)
username_entry1.grid(row=2, column=1)
Label(screen2, text = "Password:").grid(row=3, column=0, sticky="e", padx=10, pady=10)
password_entry1 = Entry(screen2, textvariable = password_verify, show="*")
password_entry1.grid(row=3, column=1)
Button(screen2, text = "Login", width = 10, height = 1, command = login_verify).grid(row=5, column=1, padx=10, pady=10)
def login_verify():
username1 = username_verify.get()
password1 = password_verify.get()
list_of_files = os.listdir()
if username1 in list_of_files:
file1 = open(username1, "r")
verify = file1.read().splitlines()
if password1 in verify:
login_sucess()
else:
password_entry1.delete(0, END)
password_is_not_correct()
else:
username_entry1.delete(0, END)
password_entry1.delete(0, END)
user_not_found()
def register_decano():
global screen1
screen1 = Toplevel(screen)
screen1.title("Register")
global username
global username_entry
global password
global password_entry
global password2
global password2_entry
global name
global name_entry
global last_name
global last_name_entry
global dni
global dni_entry
global adress
global adress_entry
global rango
global rango_entry
global position
username = StringVar()
password = StringVar()
password2 = StringVar()
name = StringVar()
last_name = StringVar()
dni = StringVar()
adress = StringVar()
rango = StringVar()
position = StringVar()
position="Decano"
Label(screen1, text = "Please enter details below").grid(row=0, column=1, padx=10, pady=10)
Label(screen1, text = "Name:").grid(row=2, column=0, sticky="e", padx=10, pady=10)
name_entry = Entry(screen1, textvariable = name)
name_entry.grid(row=2, column=1)
Label(screen1, text = "Last name:").grid(row=3, column=0, sticky="e", padx=10, pady=10)
last_name_entry = Entry(screen1, textvariable = last_name)
last_name_entry.grid(row=3, column=1)
Label(screen1, text = "DNI:").grid(row=4, column=0, sticky="e", padx=10, pady=10)
dni_entry = Entry(screen1, textvariable = dni)
dni_entry.grid(row=4, column=1)
Label(screen1, text = "Adress:").grid(row=5, column=0, sticky="e", padx=10, pady=10)
adress_entry = Entry(screen1, textvariable = adress)
adress_entry.grid(row=5, column=1)
Label(screen1, text = "Range:").grid(row=6, column=0, sticky="e", padx=10, pady=10)
rango_entry = Entry(screen1, textvariable = rango)
rango_entry.grid(row=6, column=1)
Label(screen1, text = "Username:").grid(row=7, column=0, sticky="e", padx=10, pady=10)
username_entry = Entry(screen1, textvariable = username)
username_entry.grid(row=7, column=1)
Label(screen1, text = "Password:").grid(row=8, column=0, sticky="e", padx=10, pady=10)
password_entry = Entry(screen1, textvariable = password, show="*")
password_entry.grid(row=8, column=1)
Label(screen1, text = "Corfirm Password:").grid(row=9, column=0, sticky="e", padx=10, pady=10)
password2_entry = Entry(screen1, textvariable = password2, show="*")
password2_entry.grid(row=9, column=1)
Button(screen1, text = "Register", width =10, height =1, command = confirm_password_d).grid(row=11, column=1, padx=10, pady=10)
def register_security_boss():
global screen6
screen6 = Toplevel(screen)
screen6.title("Register")
global username
global username_entry
global password
global password_entry
global password2
global password2_entry
global name
global name_entry
global last_name
global last_name_entry
global dni
global dni_entry
global adress
global adress_entry
global rango
global rango_entry
global position
username = StringVar()
password = StringVar()
password2 = StringVar()
name = StringVar()
last_name = StringVar()
dni = StringVar()
adress = StringVar()
rango = StringVar()
position = StringVar()
position="Security Boss"
Label(screen6, text = "Please enter details below").grid(row=0, column=1, padx=10, pady=10)
Label(screen6, text = "Name:").grid(row=2, column=0, sticky="e", padx=10, pady=10)
name_entry = Entry(screen6, textvariable = name)
name_entry.grid(row=2, column=1)
Label(screen6, text = "Last name:").grid(row=3, column=0, sticky="e", padx=10, pady=10)
last_name_entry = Entry(screen6, textvariable = last_name)
last_name_entry.grid(row=3, column=1)
Label(screen6, text = "DNI:").grid(row=4, column=0, sticky="e", padx=10, pady=10)
dni_entry = Entry(screen6, textvariable = dni)
dni_entry.grid(row=4, column=1)
Label(screen6, text = "Adress:").grid(row=5, column=0, sticky="e", padx=10, pady=10)
adress_entry = Entry(screen6, textvariable = adress)
adress_entry.grid(row=5, column=1)
Label(screen6, text = "Range:").grid(row=6, column=0, sticky="e", padx=10, pady=10)
rango_entry = Entry(screen6, textvariable = rango)
rango_entry.grid(row=6, column=1)
Label(screen6, text = "Username:").grid(row=7, column=0, sticky="e", padx=10, pady=10)
username_entry = Entry(screen6, textvariable = username)
username_entry.grid(row=7, column=1)
Label(screen6, text = "Password:").grid(row=8, column=0, sticky="e", padx=10, pady=10)
password_entry = Entry(screen6, textvariable = password, show="*")
password_entry.grid(row=8, column=1)
Label(screen6, text = "Corfirm Password:").grid(row=9, column=0, sticky="e", padx=10, pady=10)
password2_entry = Entry(screen6, textvariable = password2, show="*")
password2_entry.grid(row=9, column=1)
Button(screen6, text = "Register", width =10, height =1, command = confirm_password_sb).grid(row=11, column=1, padx=10, pady=10)
def register_guard():
global screen7
screen7 = Toplevel(screen)
screen7.title("Register")
global username
global username_entry
global password
global password_entry
global password2
global password2_entry
global name
global name_entry
global last_name
global last_name_entry
global dni
global dni_entry
global adress
global adress_entry
global rango
global rango_entry
global position
username = StringVar()
password = StringVar()
password2 = StringVar()
name = StringVar()
last_name = StringVar()
dni = StringVar()
adress = StringVar()
rango = StringVar()
position = StringVar()
position="Guard"
Label(screen7, text = "Please enter details below").grid(row=0, column=1, padx=10, pady=10)
Label(screen7, text = "Name:").grid(row=2, column=0, sticky="e", padx=10, pady=10)
name_entry = Entry(screen7, textvariable = name)
name_entry.grid(row=2, column=1)
Label(screen7, text = "Last name:").grid(row=3, column=0, sticky="e", padx=10, pady=10)
last_name_entry = Entry(screen7, textvariable = last_name)
last_name_entry.grid(row=3, column=1)
Label(screen7, text = "DNI:").grid(row=4, column=0, sticky="e", padx=10, pady=10)
dni_entry = Entry(screen7, textvariable = dni)
dni_entry.grid(row=4, column=1)
Label(screen7, text = "Adress:").grid(row=5, column=0, sticky="e", padx=10, pady=10)
adress_entry = Entry(screen7, textvariable = adress)
adress_entry.grid(row=5, column=1)
Label(screen7, text = "Range:").grid(row=6, column=0, sticky="e", padx=10, pady=10)
rango_entry = Entry(screen7, textvariable = rango)
rango_entry.grid(row=6, column=1)
Label(screen7, text = "Username:").grid(row=7, column=0, sticky="e", padx=10, pady=10)
username_entry = Entry(screen7, textvariable = username)
username_entry.grid(row=7, column=1)
Label(screen7, text = "Password:").grid(row=8, column=0, sticky="e", padx=10, pady=10)
password_entry = Entry(screen7, textvariable = password, show="*")
password_entry.grid(row=8, column=1)
Label(screen7, text = "Corfirm Password:").grid(row=9, column=0, sticky="e", padx=10, pady=10)
password2_entry = Entry(screen7, textvariable = password2, show="*")
password2_entry.grid(row=9, column=1)
Button(screen7, text = "Register", width =10, height =1, command = confirm_password_g).grid(row=11, column=1, padx=10, pady=10)
def confirm_password_d():
passwordC = password.get()
password2C = password2.get()
usernameC = username.get()
nameC = name.get()
last_nameC = last_name.get()
dniC = dni.get()
adressC = adress.get()
rangoC = rango.get()
if nameC=="":
Label(screen1, text = "Please write a name", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if last_nameC=="":
Label(screen1, text = "Please write a last name", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if dniC=="":
Label(screen1, text = "Please write a DNI", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if adressC=="":
Label(screen1, text = "Please write an adress", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if rangoC=="":
Label(screen1, text = "Please write a range", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if usernameC=="":
Label(screen1, text = "Please write a usermane", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if passwordC=="":
Label(screen1, text = "Please please write a password", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if password2C=="":
Label(screen1, text = "Please confirm your password", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if passwordC==password2C:
Label(screen1, text= "The registration was Successfull", fg = "green", font = ("calibri", 11)).grid(row=12, column=1)
register_user()
else:
password_entry.delete(0, END)
password2_entry.delete(0, END)
Label(screen1, text = "The passwords are not the same", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
def confirm_password_sb():
passwordC = password.get()
password2C = password2.get()
usernameC = username.get()
nameC = name.get()
last_nameC = last_name.get()
dniC = dni.get()
adressC = adress.get()
rangoC = rango.get()
if nameC=="":
Label(screen6, text = "Please write a name", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if last_nameC=="":
Label(screen6, text = "Please write a last name", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if dniC=="":
Label(screen6, text = "Please write a DNI", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if adressC=="":
Label(screen6, text = "Please write an adress", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if rangoC=="":
Label(screen6, text = "Please write a range", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if usernameC=="":
Label(screen6, text = "Please write a usermane", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if passwordC=="":
Label(screen6, text = "Please please write a password", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if password2C=="":
Label(screen6, text = "Please confirm your password", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if passwordC==password2C:
Label(screen6, text= "The registration was Successfull", fg = "green", font = ("calibri", 11)).grid(row=12, column=1)
register_user()
else:
password_entry.delete(0, END)
password2_entry.delete(0, END)
Label(screen6, text = "The passwords are not the same", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
def confirm_password_g():
passwordC = password.get()
password2C = password2.get()
usernameC = username.get()
nameC = name.get()
last_nameC = last_name.get()
dniC = dni.get()
adressC = adress.get()
rangoC = rango.get()
if nameC=="":
Label(screen7, text = "Please write a name", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if last_nameC=="":
Label(screen7, text = "Please write a last name", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if dniC=="":
Label(screen7, text = "Please write a DNI", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if adressC=="":
Label(screen7, text = "Please write an adress", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if rangoC=="":
Label(screen7, text = "Please write a range", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if usernameC=="":
Label(screen7, text = "Please write a usermane", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if passwordC=="":
Label(screen7, text = "Please please write a password", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if password2C=="":
Label(screen7, text = "Please confirm your password", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
else:
if passwordC==password2C:
Label(screen7, text= "The registration was Successfull", fg = "green", font = ("calibri", 11)).grid(row=12, column=1)
register_user()
else:
password_entry.delete(0, END)
password2_entry.delete(0, END)
Label(screen7, text = "The passwords are not the same", fg = "red", font = ("calibri", 11)).grid(row=12, column=1)
def register_user():
username_info = username.get()
password_info = password.get()
name_info = name.get()
last_name_info = last_name.get()
dni_info = dni.get()
adress_info = adress.get()
rango_info = rango.get()
file=open(username_info, "w")
file.write(position+"\n"+"\n")
file.write("Username: "+"\n")
file.write(username_info+"\n")
file.write("Password: "+"\n")
file.write(password_info+"\n")
file.write("Name: "+name_info+"\n")
file.write("Last name: "+last_name_info+"\n")
file.write("DNI: "+dni_info+"\n")
file.write("Adress: "+adress_info+"\n")
file.write("Range: "+rango_info+"\n")
file.close()
username_entry.delete(0, END)
password_entry.delete(0, END)
password2_entry.delete(0, END)
name_entry.delete(0, END)
last_name_entry.delete(0, END)
adress_entry.delete(0, END)
dni_entry.delete(0, END)
rango_entry.delete(0, END)
def login_sucess():
global screen3
screen3 = Toplevel(screen)
screen3.title("sucess")
screen3.geometry("150x100")
Label(screen3, text = "Login sucess").pack()
Button(screen3, text = "Ok", command = delete3).pack()
def user_not_found():
global screen5
screen5 = Toplevel(screen)
screen5.title("Wrong User")
screen5.geometry("150x100")
Label(screen5, text = "User not found").pack()
Button(screen5, text = "Ok", command = delete5).pack()
def password_is_not_correct():
global screen4
screen4 = Toplevel(screen)
screen4.title("Wrong Password")
screen4.geometry("150x100")
Label(screen4, text = "Password is not correct").pack()
Button(screen4, text = "Ok", command = delete4).pack()
def kind_register():
global screen5
screen5 = Toplevel(screen)
screen5.title("Kind of register")
Label(screen5, text = "Kinds of registration").grid(row=0, column=1, padx=10, pady=10)
Button(screen5, text = "Decano", command = register_decano).grid(row=1, column=0, padx=10, pady=10)
Button(screen5, text = "Security boss", command = register_security_boss).grid(row=1, column=1, padx=10, pady=10)
Button(screen5, text = "Guard", command = register_guard).grid(row=1, column=2, padx=10, pady=10)
def delete3():
screen3.destroy()
def delete4():
screen4.destroy()
def delete5():
screen5.destroy()
main_screen()
screen.mainloop()