验证用户登录(python)

2024-10-01 15:42:24 发布

您现在位置:Python中文网/ 问答频道 /正文

登录时需要验证用户名和密码的帮助

我要做的是让用户用一个已经创建的帐户登录(主窗口)

主窗口:

import tkinter 
from tkinter import *
from tkinter import messagebox

import data_create_acct

class DataGUI:
 def __init__(self):
     self.main_window = tkinter.Tk() # create the main windowt
     self.main_window.title(" Theater Kiosk Program")
     self.main_window.minsize(width=260,height=270) #window size
     self.main_window.resizable(False,False)
     self.main_window.config(bg='gray5',borderwidth= 4,relief= GROOVE)
     # create labels and position them using grid  (row, column)

     self.main_window.columnconfigure(0, minsize=100)
     self.main_window.columnconfigure(1, minsize=100)
     self.main_window.columnconfigure(2, minsize=100)
     self.main_window.columnconfigure(3, minsize=100)
     self.main_window.columnconfigure(4, minsize=100)

     self.heading_label = tkinter.Label(text='Theater Management System', \
                                        bg='gray5',font=("Helvetica",18),fg="DarkOrange2")
     self.heading_label.grid(row=1,column=1,columnspan=3,rowspan=2,padx=10,pady=10)

     self.login_button = tkinter.Button(text='Login',height="2", width="21",\
                                        bg='gray5',fg='DarkOrange2',font=("Helvetica",10),\
                                        command=self.login)
     self.login_button.grid(row=3,column=1,columnspan=3,rowspan=1,padx=1,pady=10)

     self.create_acct_button = tkinter.Button(text='Create Account',height="2", width="21",\
                                              bg='gray5',fg='DarkOrange2',font=("Helvetica",10),\
                                              command=self.create_account)  # function
     self.create_acct_button.grid(row=4,column=1,columnspan=3,rowspan=1,padx=1,pady=10)

     self.quit_button= tkinter.Button(text='Cancel',height="2", width="21",font=("Helvetica",10),\
                                      bg='gray5',fg='DarkOrange2',command=self.main_window.destroy)
     self.quit_button.grid(row=5,column=1,columnspan=3,rowspan=3,padx=1,pady=10)

     tkinter.mainloop()

 def create_account(self):
     CreateAcctWin=data_create_acct.AccountGUI()

     CreateAcctWin.account_window.wait_window()

     self.create_acct_button.config(state = DISABLED)
     self.login_button.config(state = NORMAL)

 def login(self):
     self.login_button.config(state = DISABLED)
     self.main_window.minsize(width=260,height=370)
     self.main_window.resizable(False,False)

这是我需要帮助的部分。我需要验证帐户是否已创建/存在

#blank label is just used as extra spacing between the cancel button and the login entry

     self.login_blank_label = `tkinter.Label(self.main_window,text='Spam',width='5',fg="gray5",bg='gray5')`
     self.login_blank_label.grid(row=8,column=3,pady=5,padx=10)

     self.login_username_label = tkinter.Label(self.main_window,text='Username:',fg="DarkOrange2",\
                                           bg='gray5',font=("Helvetica",10))
     self.login_username_label.grid(row=9,column=0)

     self.login_username_entry = tkinter.Entry(self.main_window,width=20,justify='right',\
                                      font=("Helvetica",10))
     self.login_username_entry.grid(row=9,column=1)
     self.login_username_entry.bind("<Return>",self.verify_user)
     self.login_username_entry.focus_force()

     self.login_password_label = tkinter.Label(self.main_window,text='Password:',fg="DarkOrange2",\
                                           bg='gray5',font=("Helvetica",10))
     self.login_password_label.grid(row=9,column=2)

     self.login_password_entry = tkinter.Entry(self.main_window,width=20,justify='right',\
                                      font=("Helvetica",10),show='*')
     self.login_password_entry.grid(row=9,column=3)
     self.login_password_entry.focus_force()


TicketSystemProgram = DataGUI()

第二个窗口:

# Theater Ticket Program Account Creation
# contains user name and password setup and validation

import tkinter
from tkinter import *
from tkinter import messagebox

class AccountGUI:
    def __init__(acct):

        print('In the init for AccountGUI')
        acct.account_window = tkinter.Tk()
        acct.account_window.title("Account Creation")
        acct.account_window.minsize(width=250,height=250)# set the window size
        acct.account_window.resizable(False,False)
        acct.account_window.config(bg='gray5',borderwidth= 3,relief= GROOVE)

        acct.account_window.columnconfigure(0, minsize=10)
        acct.account_window.columnconfigure(1, minsize=50)
        acct.account_window.columnconfigure(2, minsize=10)
        acct.account_window.columnconfigure(3, minsize=5)
        acct.account_window.columnconfigure(4, minsize=50)


        acct.account_window.userName_label = tkinter.Label(acct.account_window, text='Username:', font=("Helvetica",10),\
                                                           bg='gray5',fg='DarkOrange2')
        acct.account_window.userName_label.grid(row=2, column=1, pady=20)

        acct.account_window.userName_entry = tkinter.Entry(acct.account_window,width = 20, \
                                                           justify='right',font=("Helvetica",10))
        acct.account_window.userName_entry.grid(row=2, column=2, padx=90, pady=20, sticky=W)
        acct.account_window.userName_entry.focus_force()

        acct.account_window.passrequ_label = tkinter.Label(acct.account_window, text= \
                                                               'Create a password of at least six (6) characters,\n'\
                                                               'that contains at least one digit, one uppercase,\n'\
                                                               'and one lowercase letter.',bg='gray5',fg='DarkOrange2',\
                                                                font=("Helvetica",10))
        acct.account_window.passrequ_label.grid(row=4, column=2,padx=10)

        acct.account_window.password_label = tkinter.Label(acct.account_window, text='Password:', font=("Helvetica",10),\
                                                           bg='gray5',fg='DarkOrange2')
        acct.account_window.password_label.grid(row=6, column=1, pady=20)

        acct.account_window.password_entry = tkinter.Entry(acct.account_window,width = 20,\
                                                           justify='right',font=("Helvetica",10))
        acct.account_window.password_entry.grid(row=6, column=2, padx=90, pady=20, sticky=W)
        acct.account_window.password_entry.focus_force()

        acct.account_window.create_button = tkinter.Button(acct.account_window, text='Create Account', \
                                                           font=("Helvetica",10),bg='gray5',fg='DarkOrange2',\
                                                           command=acct.verify_new_user)
        acct.account_window.create_button.grid(row=15, column=2, columnspan=5,pady=30)

        acct.account_window.cancel_button = tkinter.Button(acct.account_window, text='Cancel', \
                                                           font=("Helvetica",10),bg='gray5',fg='DarkOrange2',\
                                                           command=acct.account_window.destroy)
        acct.account_window.cancel_button.grid(row=15, column=3,pady=30)


#This function verifies that a new user is in fact a new user name
    def verify_new_user(acct):
        valid = True
        newUser = (acct.account_window.userName_entry.get())
        print('newUser is: ' + newUser + '\n')
        try:
            userDataFile = open('acct_user_names.txt', 'r')

            for userTemp in userDataFile:
                print('userTemp from file is: ' + userTemp)
                if newUser == userTemp.rstrip():
                    valid = False

            userDataFile.close()

            if (valid == False):    
                tkinter.messagebox.showinfo('Invalid User Name','That user name already exists.')
                acct.account_window.userName_entry.delete(0,END)
                acct.account_window.userName_entry.focus_force()   
                acct.account_window.lift()
            else:    
                acct.verify_new_pass(newUser)

        except IOError:
            print('No File exists.')


    def verify_new_pass(acct, user):
        valid = False
        txt = (acct.account_window.password_entry.get())      
        print('Getting password   ' + txt)

        if acct.verify_pass(txt):     
            userFile = open('acct_user_names.txt', 'a')    
            userFile.write(user + '\n')
            userFile.close()

            passwordFile = open('acct_user_passwords.txt', 'a')    
            passwordFile.write(txt + '\n')                        
            passwordFile.close()
            tkinter.messagebox.showinfo('Account Creation','Account Successfully Created.')
            acct.account_window.lift()
            acct.account_window.destroy()     
        else:
            tkinter.messagebox.showinfo('Password Validation', '"' + txt + '"' + ' is not a valid password')
            acct.account_window.lift()
            acct.account_window.password_entry.delete(0,END)
            acct.account_window.focus()
            acct.account_window.password_entry.focus()

    def verify_pass(acct, txt):

        isValid = False
        longEnough = False
        hasUpper = False
        hasLower = False
        hasDigit = False

        if len(txt) >= 6:
            longEnough = True

            for ch in txt:
                if ch.isupper():
                    hasUpper = True
                if ch.islower():
                    hasLower = True
                if ch.isdigit():
                    hasDigit = True

        if longEnough and hasUpper and hasLower and hasDigit:
            isValid = True

        return isValid

    def create_account(self):
         print('Inside create_account and creating the GUI')     
         CreateAcctWin = data_create.acct.AccountGUI()

         CreateAcctWin.account_window.wait_window()

         self.create_acct_button.config(state = DISABLED)  
         self.login_button.config(state = NORMAL)


data_create_acct = AccountGUI

我不知道该怎么办。当用户尝试登录时,必须读取包含密码和用户名的文本文件,以验证帐户是否存在


Tags: selfmaintkintercreatelogincolumnbuttonaccount

热门问题