使用tkinter实现用户登录注册界面
题目的要求为
使用tkinter实现用户登录注册界面,包括:
注册功能(包括用户名、密码、手机号、身份证号,注册时对以上信息进行验证,用户名要求包括字母和数字以及特殊符号,密码不能少于8位),登录功能(用户名和密码为admin登录提示登录成功,否则提示登录失败)
代码
from tkinter import *
from tkinter import messagebox
login = Tk()
login.title('Login')
login.geometry('210x150')
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,sticky=E)
passwd = Entry(login,show='*')
passwd.grid(row=2,column=1)
def successful():
if name.get() == 'admin' and passwd.get() == 'admin':
messagebox.showinfo(title='successful',message='登录成功')
else:
messagebox.showerror(title='wrong',message='登录失败,用户名或密码错误')
Button(login,text='登录',command=successful).grid(row=3,column=0,columnspan=2)
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():
if not (any([x.isdigit() for x in names.get()]) and any([x.isalpha() for x in names.get()]) and teshufuhao(names.get())):
messagebox.showerror(title='wrong',message='注册失败,用户名格式错误,必须包括字母和数字以及特殊符号')
elif len(passwds.get()) < 8:
messagebox.showerror(title='wrong',message='注册失败,密码不应少于8位')
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='注册成功!欢迎您。新会员')
Button(registered,text='注册',command=registeredes).grid(row=6,column=0,columnspan=2)
Button(login,text='还没有账号?点我注册!',command=registereds).grid(row=4,column=0,columnspan=2)
login.mainloop()
显示效果