B
B
Bobrenok_kola2021-07-18 21:54:24
linux
Bobrenok_kola, 2021-07-18 21:54:24

How to normally implement a registration form in Python + Tkinter?

I am writing a registration form in Python3 TKinter. The problem is that there are 2 bugs: the first one in registration when trying to enter data gets a strange error; the second, when logging in, the login check is successful, and the absolutely identical password check confuses the system. Code below

from tkinter import *
from functools import partial
from tkinter.messagebox import *
import re




 

#текст
a="Авторизируйтесь" #Зоглоловок окна
b="Вход в приложение"#Текст в приложениии
c="Пожайлуста введите данные:"#Текст в приложение 2
d="Вы зарагистрированны?"#Вопрос о регистрации пользовотеля
e="Придумайте надежный логин и пароль"
f="Регистрация"


#Логины
namelist = ["ilia"]
#Пароли
passwdlist= ["2" "1"]

window = Tk()
window.title(a)


text1=Label(text=b)
text1.pack(fill=BOTH, expand=True)

text2=Label(text=c)
text2.pack(fill=BOTH, side=LEFT, expand=True)

entryLOGIN = Entry()
entryLOGIN.insert(0,"Логин")
entryLOGIN.pack(fill=BOTH, side=LEFT, expand=True)


entryPASS = Entry()
entryPASS.pack(fill=BOTH, side=LEFT, expand=True)
entryPASS.insert(0,"Пароль")


i=0


#Оценка стойкости пароля
def ChekDATA(entryEPASS,entryELOGIN,namelist,passwdlist):
  passw=entryEPASS.get()
  name=entryELOGIN.get()
  fl=0
  a=0
  while True:
    if (len(passw)<8):
      fl=-1
      break
    elif not re.search("[a-z]",passw):
      fl=-1
      break
    elif not re.search ("[A-Z]",passw):
      fl=-1					
      break	
    elif not re.search("[0-9]",passw):
      fl=-1	
      break	
    elif re.search("\s",passw):
      fl=-1
      break
  if fl==0:
    passwdlist.append(passw)
    a=a+1
  while True:
    if (len(name)<8):

      fl=-1
      break
    elif not re.search("[a-z]",name):
      fl=-1
      break
    elif not re.search ("[A-Z]",name):
      fl=-1					
      break	
    elif not re.search("[0-9]",name):
      fl=-1	
      break	
    elif re.search("\s",name):
      fl=-1
      break
  if fl==0:
    namelist.append(name)
    a=a+1
  if a==2:
    avt(passwdlist,namelist,a,b,c,d,e,f,window)
  else:
    showinfo("Информация","Пароль ненадежн повторите попытку")
    reg(window)	



#Приложение регистрации
def reg(window):
  window.destroy()
  Regw= Tk()
  Regw.title(f)

  text1E=Label(text=e)
  text1E.pack(fill=BOTH, expand=True)

  text2E=Label(text=c)
  text2E.pack(fill=BOTH, side=LEFT, expand=True)

  entryELOGIN = Entry()
  entryELOGIN.insert(0,"Логин")
  entryELOGIN.pack(fill=BOTH, side=LEFT, expand=True)


  entryEPASS = Entry()
  entryEPASS.pack(fill=BOTH, side=LEFT, expand=True)
  entryEPASS.insert(0,"Пароль")

  buttonREG = Button(text="Ввести",command=partial
  (
  ChekDATA,
  entryPASS,
  entryLOGIN,
  namelist,
  passwdlist
  ))
  buttonREG.pack(fill=BOTH, side=BOTTOM, expand=True) 
  

def avt(passwdlist,namelist,a,b,c,d,e,f,window):	
  #Приложение авторизации
  window.destroy()
  window = Tk()
  window.title(a)


  text1=Label(text=b)
  text1.pack(fill=BOTH, expand=True)

  text2=Label(text=c)
  text2.pack(fill=BOTH, side=LEFT, expand=True)

  entryLOGIN = Entry()
  entryLOGIN.insert(0,"Логин")
  entryLOGIN.pack(fill=BOTH, side=LEFT, expand=True)


  entryPASS = Entry()
  entryPASS.pack(fill=BOTH, side=LEFT, expand=True)
  entryPASS.insert(0,"Пароль")


  i=0
  #Функция авторизации и проверки паролей на подлинность
  def increase(namelist,passwdlist,entryLOGIN,entryPASS,i):
    p=entryPASS.get()
    name=entryLOGIN.get()
    
    while i<10:
      if name in namelist:
        print("имя есть")
        if p in passwdlist:
          print(a)
          window.destroy()
          main(name)
        else:
          
          entryPASS.delete(0,END)
          i=+1
      else :
        showerror(
      "Ошибка","Введен неверный логин"
      )	
        enrtyLOGIN.delete(0,END)
        i=+1
    else:
      showerror(
      "Ошибка","Попытки кончились"
      )
      


  buttonVXOD = Button(
        master=window,
        text="Войти",
        command=partial(increase,
        namelist,
        passwdlist,
        entryLOGIN,
        entryPASS,
        i
      )
    )
  buttonVXOD.pack(fill=BOTH, side=BOTTOM, expand=True) 





  #Вопрос о том зарегистрирован ли пользователь
def check():
  answer = askyesno(
  title="Вопрос", 
  message=d
  )
  if answer:
    avt(passwdlist,namelist,a,b,c,d,e,f,window)
  else:
    reg(window)
       
    
check()









window.mainloop()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-07-21
@Bobrenok_kola

the first in registration when trying to enter data gets a strange error

Because the names are confusing. For example, reg uses entry E PASS elements and passes entryPASS for verification.
login verification is successful, and absolutely identical password verification confuses the system

You have a strange check there, you look for a login among all, then a password among all, and if they match, you go into an endless loop.
And in general, your logins are untethered from passwords, a match will show even for mismatched login:password pairs, even if they are simply in the data. For a normal check, you need to store them in pairs, for example, in the form of a dictionary logins = {"login1": "password1", ...}
It would be more correct to check (using your own variables name, p) and the above dictionary
if logins.get(name) == p:
will immediately check that the entered login and password match.
And it is better not to bind the password check itself to interface elements. Make a function that simply accepts ready-made strings (login, password) and returns the result of the check. And pass strings from the necessary interface elements to these functions: entryPASS.get(), etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question