1.将账号信息存入pickle文件中,以便调用
1 import pickle 2 3 acc = {'abc':'123'} 4 5 with open('acc.pkl','wb') as f: 6 pickle.dump(acc,f)
2.主程序
1 #! /usr/bin/env python3 2 # -*- coding:utf-8 -*- 3 # Author:Jailly 4 5 import easygui as g 6 import pickle,os 7 8 #登录函数 9 def login(acc_sql,locked_acc_sql): 10 n = 0 11 12 while n < 3: 13 14 # 得到用户输入 15 acc = g.multpasswordbox('请输入您的用户名和密码:', fields=('用户名', '密码')) 16 17 #去除用户名左右两边的空格 18 if acc: 19 acc[0] = acc[0].strip() 20 21 #点击cancel,退出 22 else: 23 exit() 24 25 #账号被锁定的情况 26 if acc and acc[0] in locked_acc_sql: 27 g.msgbox('对不起,您的账号已被锁定!') 28 exit() 29 30 #账号密码输入正确的情况 31 elif acc and acc[0] in acc_sql and acc[1] == acc_sql[acc[0]]: 32 g.msgbox('登陆成功!') 33 exit() 34 35 # 用户名不在账号字典中的情况 36 elif acc and acc[0] not in acc_sql: 37 acc = g.msgbox('您输入的用户名不存在,请重新输入') 38 39 #用户名在账号字典中,但密码输入错误的情况 40 else: 41 acc = g.msgbox('用户名或密码错误,请重新输入') 42 n += 1 43 44 #密码输入错误超过3次之后的处理 45 with open('locked_acc.pkl','wb') as f: 46 locked_acc_sql.append(acc[0]) 47 pickle.dump(locked_acc_sql,f) 48 49 g.msgbox('对不起,密码输入错误超过3次,您的账号已被锁定!') 50 51 if __name__ == '__main__': 52 53 # 事先将账号密码组成的字典{'abc':'123'}存入pickle文件,打开本程序时导入该字典 54 with open('acc.pkl', 'rb') as f: 55 acc_sql = pickle.load(f) 56 57 # 初始化'被锁定账号'列表 58 if not os.path.exists('locked_acc.zip'): 59 with open('locked_acc.pkl', 'wb') as f: 60 pickle.dump([], f) 61 62 # 导出'被锁定账号'列表 63 with open('locked_acc.pkl', 'rb') as f: 64 locked_acc_sql = pickle.load(f) 65 66 login(acc_sql,locked_acc_sql)