使用tkinter开发的一款登录和注册图形化界面
使用tkinter开发的一款登录和注册图形化界面,并且使用 ttkbootstrap 进行界面美化。
项目以学校选课系统为参考,将登陆身份设置为:管理员、教师、学生。登录时可根据登录的身份自动打开对应的界面【空白界面暂未开发】。
项目中用户的信息全部保存在 sqlite 数据库中,数据表有:users表保存账号密码信息,remember_psw表保存上次登录信息。
该项目还有其他小功能:左上角图标根据内容改变、根据输入有误信息准确提示;更多功能欢迎体验。
项目管理员手机号为:admin
项目管理员密码为:admin
登录界面包含:输入手机号、密码,记住密码选项框组件,输入正确信息后登录成功!输入错误信息及未输入完整信息均有提示框。
登录界面部分代码展示:
def login_window(self, frame=None):
# 用户名输入框
username_label = Label(frame, text="手机号:")
username_label.grid(row=1, column=1, padx=5, pady=20)
self.login_phone = tk.StringVar()
username_entry = Entry(frame, bootstyle=INFO, textvariable=self.login_phone, width=30)
username_entry.grid(row=1, column=2, padx=5, pady=10)
# 密码输入框
psw_label = Label(frame, text="密 码:")
psw_label.grid(row=2, column=1, padx=5, pady=10)
self.login_psw = tk.StringVar()
psw_entry = Entry(frame, bootstyle=INFO, textvariable=self.login_psw, show="*", width=30)
psw_entry.grid(row=2, column=2, padx=5, pady=10)
# 记住密码选项
self.remember_psw = tk.IntVar()
pwd_check_button = Checkbutton(frame, text="记住密码", variable=self.remember_psw)
pwd_check_button.grid(row=3, column=1, padx=5, pady=5, columnspan=2, sticky="w")
# 登录按钮
login_button = Button(frame, text="登 录", bootstyle=PRIMARY, width=40, command=self.login)
login_button.grid(row=4, column=1, padx=5, pady=10, columnspan=2)
# 查看是否记住密码
remember_select_sql = """SELECT phone,password,is_remember FROM remember_psw WHERE id=1;"""
is_remember = self.execute_sql(remember_select_sql)
if is_remember:
if is_remember[0][2] == "1":
self.login_phone.set(is_remember[0][0])
self.login_psw.set(is_remember[0][1])
self.remember_psw.set(int(is_remember[0][2]))
登录功能部分代码展示:
def login(self):
phone = self.login_phone.get()
password = self.login_psw.get()
remember = self.remember_psw.get()
if phone and password:
md5_password = self.encryption(password)
select_sql = f"""select 1 from users where phone='{phone}' AND password_md5='{md5_password}';"""
if self.execute_sql(select_sql):
create_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 更新记住密码操作
update_sql = f"""UPDATE remember_psw SET phone='{phone}',password='{password}',is_remember='{remember}',create_time='{create_time}' WHERE id=1;"""
self.execute_sql(update_sql)
# showinfo(title="登录", message="登录成功!")
self.register_login.destroy()
# 根据登录的身份打开不同的界面
select_id_sql = f"""SELECT name,identity FROM users WHERE phone='{phone}';"""
select_id_result = self.execute_sql(select_id_sql)
if select_id_result[0][1] == "管理员":
self.admin_window()
elif select_id_result[0][1] == "教师":
self.teacher_window()
elif select_id_result[0][1] == "学生":
self.student_window()
else:
showerror(title="失败", message="账号密码输入错误!")
else:
showerror(title="失败", message="账号密码输入有误!")
注册界面主要包括:输入用户名、手机号、密码、身份下拉框组件。输入信息有误均有不同弹窗提示输入错误信息及未输入完整信息均有提示框。
注册界面部分代码展示:
def register_window(self, frame=None):
padx = 5
pady = 5
# 用户名输入框
username_label = Label(frame, text="用户名:")
username_label.grid(row=1, column=1, padx=padx, pady=pady)
self.register_username = tk.StringVar()
self.register_username_entry = Entry(frame, bootstyle=INFO, textvariable=self.register_username, width=30)
self.register_username_entry.grid(row=1, column=2, padx=padx, pady=pady)
# 手机号输入框
username_label = Label(frame, text="手机号:")
username_label.grid(row=2, column=1, padx=padx, pady=pady)
self.register_pone = tk.StringVar()
self.register_phone_entry = Entry(frame, bootstyle=INFO, textvariable=self.register_pone, width=30)
self.register_phone_entry.grid(row=2, column=2, padx=padx, pady=pady)
# 密码输入框
psw_label = Label(frame, text="密 码:")
psw_label.grid(row=3, column=1, padx=padx, pady=pady)
self.register_psw = tk.StringVar()
self.register_psw_entry = Entry(frame, bootstyle=INFO, textvariable=self.register_psw, show="*", width=30)
self.register_psw_entry.grid(row=3, column=2, padx=padx, pady=pady)
# 身份
psw_label = Label(frame, text="身 份:")
psw_label.grid(row=4, column=1, padx=padx, pady=pady)
self.register_id = tk.StringVar()
self.register_combox = Combobox(frame, bootstyle=PRIMARY, textvariable=self.register_id, state="readonly",
width=28)
self.register_combox["value"] = ("下拉选择身份:", "教师", "学生")
self.register_combox.current(0)
self.register_combox.grid(row=4, column=2, padx=padx, pady=pady)
# 登录按钮
login_button = Button(frame, text="注 册", bootstyle=PRIMARY, width=40, command=self.register)
login_button.grid(row=5, column=1, padx=padx, pady=pady, columnspan=2)
注册功能部分代码展示:
def register(self):
user_name = self.register_username.get()
phone = self.register_pone.get()
password = self.register_psw.get()
identity = self.register_id.get()
if user_name and phone and password and (identity in ("教师", "学生")):
if not re.match(r"^1[356789]\d{9}$", phone):
showerror(title="失败", message="手机号填写有误!")
elif not (password.isalnum() and len(password) >= 6):
showerror(title="失败", message="密码至少6位且由数字、字母组成!")
else:
# 判断手机号是否存在
select_sql = f"""select 1 from users where phone='{phone}' LIMIT 1;"""
if self.execute_sql(select_sql):
# 手机号已注册
showerror(title="失败", message="该手机号已注册!")
else:
md5_password = self.encryption(password)
create_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
insert_users_sql = f"""
INSERT INTO users (name,phone,password,password_md5,identity,create_time) VALUES ('{user_name}','{phone}','{password}','{md5_password}','{identity}','{create_time}');
"""
self.execute_sql(insert_users_sql)
insert_remember_psw_sql = f"""
INSERT INTO remember_psw (phone,password,is_remember,create_time) VALUES ('{phone}','{password}','0','{create_time}');
"""
self.execute_sql(insert_remember_psw_sql)
showinfo(title="注册", message="注册成功!")
self.register_username_entry.delete(0, "end")
self.register_phone_entry.delete(0, "end")
self.register_psw_entry.delete(0, "end")
self.register_combox.current(0)
else:
showerror(title="失败", message="信息未填写完整!")
链接:https://pan.baidu.com/s/1IcBa-yuEDPRa6rHUlzIZiQ?pwd=g6w1
提取码:g6w1