内容
编写一个用户登录管理程序,设计窗体的布局,制定密码规则和验证码规则。要求使用 Python 标准库 tkinter 编写基于GUI 的用户登录窗体。通过该窗体,用户输入用户名、密码和验证码等信息,确认登录后需要判断用户信息是否合法,并弹出对应的反馈信息。通过判断用户是否在用户信息中,以及输入密码是否与用户信息文件中用户名所对应的密码是否相同,来判断用户是否合法。此外,新用户还可以进行注册,注册需要输入用户名、密码、手机号和身份证号等信息。注册时密码需要遵循一定规则(长度不小于8且含有数字、字母和特殊字符),注册后将用户信息存储到文件中,更新用户信息文件。步骤
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | from tkinter import * from tkinter import messagebox import os import xlrd from xlutils.copy import copy login = Tk() login.title( '登录界面' ) # 设计窗口大小 login.geometry( '210x200' ) # 设计GUI用户登录窗体 Label(login,text = "用户登录" ).grid(row = 0 ,column = 0 ,columnspan = 2 ) Label(login,text = "用户名" ).grid(row = 1 ,column = 0 ) name = Entry(login) name.grid(row = 1 ,column = 1 ) Label(login,text = "密码:" ).grid(row = 2 ,column = 0 ) passWord = Entry(login,show = '●' ) passWord.grid(row = 2 ,column = 1 ) Label(login,text = "验证码:" ).grid(row = 3 ,column = 0 ,sticky = E) verifyCode = Entry(login) verifyCode.grid(row = 3 ,column = 1 ) # 判断用户是否在用户信息.xls文件中 def isInExcel(data): filename = "用户信息.xls" excel = xlrd.open_workbook(filename, encoding_override = "utf-8" ) sheet = excel.sheets()[ 0 ] sheet_row_mount = sheet.nrows # 行数 sheet_col_mount = sheet.ncols # 列数 sheet_name = [] # 所有用户名信息 for x in range( 1 , sheet_row_mount): y = 0 sheet_name.append(sheet.cell_value(x, y)) for x in sheet_name: # 找到用户名 if (data = = x): # 已有用户 return 1 break ; # 未注册用户 return - 1 # 用户存在时,判断密码是否正确 def isPasswordDirect(data,passWord): filename = "用户信息.xls" excel = xlrd.open_workbook(filename, encoding_override = "utf-8" ) sheet = excel.sheets()[ 0 ] sheet_row_mount = sheet.nrows # 行数 sheet_col_mount = sheet.ncols # 列数 sheet_name = [] # 所有用户名信息 for x in range( 1 , sheet_row_mount): y = 0 sheet_name.append(sheet.cell_value(x, y)) sheet_passWord = [] # 所有密码信息 for x in range( 1 , sheet_row_mount): y = 1 sheet_passWord.append(sheet.cell_value(x, y)) for i in range(len(sheet_name)): if (data = = sheet_name[i]): # 记录用户名在数组中的位置 record = i break for i in range(len(sheet_passWord)): if (passWord = = sheet_passWord[i]): # 判断用户名位置与密码位置是否相同 # 以及密码是否与用户信息中的密码一致 if (i = = record): # 密码正确 return 1 break # 密码错误 return - 1 def successful(): # 判断用户名不存在 if (isInExcel(name.get()) = = - 1 ): messagebox.showerror(title = 'wrong' , message = '用户不存在,请注册' ) # 密码不正确 elif (isPasswordDirect(name.get(),passWord.get()) = = - 1 ): messagebox.showerror(title = 'wrong' , message = '密码不正确' ) # 验证码位数不正确 elif len(verifyCode.get())! = 4 : messagebox.showerror(title = 'wrong' ,message = '验证码应为4位' ) else : messagebox.showinfo(title = 'successful' ,message = '登录成功' ) Button(login, text = '发送验证码' ).grid(row = 4 , column = 0 , columnspan = 3 ) Button(login,text = '登录' ,command = successful).grid(row = 5 ,column = 0 ,columnspan = 3 ) # 设计用户注册GUI界面 def registereds(): registered = Tk() registered.title( 'registered' ) registered.geometry( '230x185' ) Label(registered, text = '用户注册' ).grid(row = 0 , column = 0 , columnspan = 2 ) Label(registered, text = '用户名:' ).grid(row = 1 , column = 0 , sticky = E) names = Entry(registered) names.grid(row = 1 , column = 1 ) Label(registered, text = '密码:' ).grid(row = 2 , column = 0 , sticky = E) passwds = Entry(registered, show = '●' ) passwds.grid(row = 2 , column = 1 ) Label(registered, text = '确认密码:' ).grid(row = 3 , column = 0 ) repasswd = Entry(registered, show = '●' ) repasswd.grid(row = 3 , column = 1 ) Label(registered, text = '手机号:' ).grid(row = 4 , column = 0 , sticky = E) phonenum = Entry(registered) phonenum.grid(row = 4 , column = 1 ) Label(registered, text = '身份证号:' ).grid(row = 5 , column = 0 ) man = Entry(registered) man.grid(row = 5 , column = 1 ) # 判断是否含有特殊符号 def teshufuhao(input_psd): string = "~!@#$%^&*()_+-*/<>,.[]\/" for i in string: if i in input_psd: return True return False def registeredes(): # 密码长度小于8 if len(passwds.get()) < 8 : messagebox.showerror(title = 'wrong' , message = '注册失败,密码不应少于8位' ) # 密码不同时含有数字、字母和特殊符号 elif not (any([x.isdigit() for x in passwds.get()]) and any([x.isalpha() for x in passwds.get()]) and teshufuhao( passwds.get())): messagebox.showerror(title = 'wrong' , message = '注册失败,密码格式错误,必须包括字母和数字以及特殊符号' ) # 两次密码输入不一样 elif passwds.get() ! = repasswd.get(): messagebox.showerror(title = 'wrong' , message = '注册失败,两次密码不相同' ) # 手机号不正确 elif not (phonenum.get().isdigit() and len(phonenum.get()) = = 11 ): messagebox.showerror(title = 'wrong' , message = '注册失败,请输入正确的11位手机号' ) # 身份证号不正确 elif len(man.get()) ! = 18 : messagebox.showerror(title = 'wrong' , message = '注册失败,请输入正确的18位身份证号' ) else : messagebox.showinfo(title = 'successful' , message = '注册成功!' ) # 将新用户信息存入用户信息文件 excel = xlrd.open_workbook( '用户信息.xls' ) sheet = excel.sheets()[ 0 ] nrow = sheet.nrows # 文件行数 wb = copy(excel) w_sheet = wb.get_sheet( 0 ) # 从数据下一行开始写入新用户信息 w_sheet.write(nrow, 0 , names.get()) w_sheet.write(nrow, 1 , repasswd.get()) w_sheet.write(nrow, 2 , phonenum.get()) w_sheet.write(nrow, 3 , man.get()) wb.save( '用户信息.xls' ) Button(registered, text = '注册' , command = registeredes).grid(row = 6 , column = 0 , columnspan = 3 ) Button(login, text = '注册' , command = registereds).grid(row = 6 , column = 0 , columnspan = 3 ) login.mainloop() |
结果
(1)密码正确(对照用户信息文件输入密码),结果显示登录成功
(2)密码不正确,结果显示密码不正确
(3)密码正确,验证码位数不对
(4)用户名不存在
5. 注册新用户
(1)新用户写入用户信息文件
(2)再次登录,用户登录成功
到此这篇关于Python中tkinter的用户登录管理的实现的文章就介绍到这了,更多相关tkinter用户登录管理内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!
- 本文固定链接: https://zxbcw.cn/post/210344/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)