0

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()

1 respuesta 1

0

Por lo que veo en tu codigo para cada rango que quieres registrar escribes toda la interfaz grafica devuelta siendo eso innecesario.Aqui te dejare el codigo que escribi, que es parecido pero no es necesario escribirlo tantas veces. En cuanto a la funcionalidad del programa nose que es lo que queres hacer por ende te ayudare con la interfaz grafica y en decirte que no es necesario en python poner:

Else:
   if:    

Puesto que python tiene una palabra reservada que funciona parecido:

Elif A == B:
   pass

Aqui la interfaz grafica:

import tkinter as tk 

root=tk.Tk()
root.title("Login")
root.geometry("300x200+120+120")
root.resizable(0,0)

def Register():
    Registrarse=tk.Toplevel(root)
    Registrarse.title("Register")
    Registrarse.geometry("300x230+120+120")
    LabelTipo=tk.Label(Registrarse,text="Kinds of registration",font=("Arial",20),fg="black")
    LabelTipo.place(x=28,y=0)
    Registrarse.resizable(0,0)

    BotonDecano=tk.Button(Registrarse,text="Decano",font=("Arial",15),fg="black",width=15,command=Register2)
    BotonDecano.place(x=58,y=60)

    BotonSecurity=tk.Button(Registrarse,text="Security Boss",font=("Arial",15),fg="black",width=15,command=Register2)
    BotonSecurity.place(x=58,y=110)

    BotonGuard=tk.Button(Registrarse,text="Guard",font=("Arial",15),fg="black",width=15,command=Register2)
    BotonGuard.place(x=58,y=160)


def Register2():
    Registrarse2=tk.Toplevel(root)
    Registrarse2.title("Register")
    Registrarse2.geometry("350x480+120+120")
    LabelRegister=tk.Label(Registrarse2,text="Register",font=("Arial",20),fg="black")
    LabelRegister.place(x=117,y=0)
    Registrarse2.resizable(0,0)

    EntryName=tk.Entry(Registrarse2)
    EntryName.place(x=180,y=50)
    LabelName=tk.Label(Registrarse2,text="Name:",font=("Arial",15))
    LabelName.place(x=100,y=45)

    EntryLastName=tk.Entry(Registrarse2)
    EntryLastName.place(x=180,y=100)
    LabelLastName=tk.Label(Registrarse2,text="Last name:",font=("Arial",15))
    LabelLastName.place(x=60,y=95)


    EntryDNI=tk.Entry(Registrarse2)
    EntryDNI.place(x=180,y=150)
    LabelDNI=tk.Label(Registrarse2,text="DNI:",font=("Arial",15))
    LabelDNI.place(x=120,y=145)

    EntryAdress=tk.Entry(Registrarse2)
    EntryAdress.place(x=180,y=200)
    LabelAdress=tk.Label(Registrarse2,text="Adress:",font=("Arial",15))
    LabelAdress.place(x=90,y=195)

    EntryRange=tk.Entry(Registrarse2)
    EntryRange.place(x=180,y=250)
    LabelRange=tk.Label(Registrarse2,text="Range:",font=("Arial",15))
    LabelRange.place(x=95,y=245)

    EntryUsername=tk.Entry(Registrarse2)
    EntryUsername.place(x=180,y=300)
    LabelUsername=tk.Label(Registrarse2,text="Username:",font=("Arial",15))
    LabelUsername.place(x=65,y=295)

    EntryPassword=tk.Entry(Registrarse2)
    EntryPassword.place(x=180,y=350)
    LabelPassword=tk.Label(Registrarse2,text="Password:",font=("Arial",15))
    LabelPassword.place(x=65,y=345)

    EntryConfirm=tk.Entry(Registrarse2)
    EntryConfirm.place(x=180,y=400)
    LabelConfirm=tk.Label(Registrarse2,text="Confirm:",font=("Arial",15))
    LabelConfirm.place(x=85,y=395)

    BotonRegister2=tk.Button(Registrarse2,text="Register",font=("Arial",15),width=10)
    BotonRegister2.place(x=100,y=430)


def Login():
    Login=tk.Toplevel(root)
    Login.title("Login")
    Login.geometry("300x200+120+120")
    Login.resizable(0,0)
    LabelLogin=tk.Label(Login,text="Login",font=("Arial",20),fg="black")
    LabelLogin.place(x=117,y=0)

    EntryUsername2=tk.Entry(Login)
    EntryUsername2.place(x=150,y=50)
    LabelUsername2=tk.Label(Login,text="Username:",font=("Arial",15))
    LabelUsername2.place(x=50,y=45)

    EntryPassword2=tk.Entry(Login)
    EntryPassword2.place(x=150,y=100)
    LabelPassword2=tk.Label(Login,text="Password:",font=("Arial",15))
    LabelPassword2.place(x=50,y=95)

    BotonLogin2=tk.Button(Login,text="Login",width=10,height=1,font=("Arial",15))
    BotonLogin2.place(x=90,y=145)


Label=tk.Label(text="Login 1.2",font=("Arial",15),bg="gray",width=27,height=2)
Label.place(x=0,y=0)

BotonLogin=tk.Button(root,text="Login",font=("Arial",13),width=20,height=2,command=Login)
BotonLogin.place(x=55,y=60)

BotonRegister=tk.Button(root,text="Register",font=("Arial",13),width=20,height=2,command=Register)
BotonRegister.place(x=55,y=130)

root.tk.mainloop()

Al codigo introducido anteriormente le faltan los labels para cuando hay un campo vacio y tampoco tiene nada de funcionalidad.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.