深入了解python的tkinter实现简单登录
作者:Weightlessly 时间:2023-03-25 14:15:27
目录
通过python的tkinter实现简单的注册登录
代码
截图
登录页面
注册页面
个人主页
修改个人信息失败
修改个人信息成功
重新登录twb
总结
通过python的tkinter实现简单的注册登录
参考文章:https://www.jb51.net/article/197751.htm
编写一个用户登录界面,用户可以登录账户信息,如果账户已经存在,可以直接登录,登录名或者登录密码输入错误会提示,如果账户不存在,提示用户注册,点击注册进去注册页面,输入注册信息,确定后便可以返回登录界面进行登录。进入个人主页可以对个人信息进行修改。
代码
import tkinter as tk
from tkinter import messagebox # import this to fix messagebox error
import pickle
username = ''
# 登录界面
def welcome():
def usr_login():
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
try:
with open('usrs_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
usr_file.close()
except FileNotFoundError:
with open('usrs_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
usr_file.close()
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title='Welcome', message='How are you? ' + usr_name)
# 进入主页
global username
username= usr_name
print(username)
window.destroy()
index_page()
else:
tk.messagebox.showerror(message='Error, your password is wrong, try again.')
else:
is_sign_up = tk.messagebox.askyesno('Welcome', 'You have not signed up yet. Sign up today?')
if is_sign_up:
usr_sign_up()
window = tk.Tk()
window.title('Welcome to the login page!')
window.geometry('450x300')
# welcome image
canvas = tk.Canvas(window, height=200, width=500)
image_file = tk.PhotoImage(file='welcome.gif')
image = canvas.create_image(0, 0, anchor='nw', image=image_file)
canvas.pack(side='top')
# user information
tk.Label(window, text='User name: ').place(x=50, y=150)
tk.Label(window, text='Password: ').place(x=50, y=190)
var_usr_name = tk.StringVar()
entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
entry_usr_name.place(x=160, y=150)
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
entry_usr_pwd.place(x=160, y=190)
# login and sign up button
btn_login = tk.Button(window, text='Login', command=usr_login)
btn_login.place(x=170, y=230)
btn_sign_up = tk.Button(window, text='Sign up', command=usr_sign_up)
btn_sign_up.place(x=270, y=230)
window.mainloop()
# 个人信息页面
def index_page():
global username
index_window = tk.Tk()
index_window.title('请选择你要进行的操作')
index_window.geometry('300x200')
tk.Label(index_window,text="你好!"+username).place(x=50,y=50)
# 修改个人信息
def change_personal_info():
# 新窗口
change_info__window = tk.Toplevel()
change_info__window.title('修改个人信息')
change_info__window.geometry('400x300')
# 修改个人信息点击页面
def change_info_click():
name = new_name.get()
psw = new_password.get()
conpsw = new_password_confirm.get()
with open('usrs_info.pickle', 'rb') as f:
usrs_info = pickle.load(f)
f.close()
if conpsw!= psw:
tk.messagebox.showerror(title='Error', message='两次密码不一致请重新输入!')
else:
if name in usrs_info:
tk.messagebox.showerror(title='Error', message='用户名已存在,请重新换一个新用户名')
else:
# 创建新
usrs_info.pop(username)
usrs_info[name] = psw
with open('usrs_info.pickle', 'wb') as file:
pickle.dump(usrs_info, file)
f.close()
tk.messagebox.showinfo(title='修改成功', message='修改成功!')
change_info__window.destroy()
tk.messagebox.showinfo(title='请重新登录!', message='请重新登录!')
index_window.destroy()
welcome()
# 修改用户名变量
new_name = tk.StringVar()
tk.Label(change_info__window, text="用户名:").place(x=50, y=50)
new_name_entry = tk.Entry(change_info__window, show=None, textvariable=new_name).place(x=120, y=50)
# 修改新密码变量
new_password = tk.StringVar()
tk.Label(change_info__window, text='密码:').place(x=50, y=80)
new_password_entry = tk.Entry(change_info__window, show="*", textvariable=new_password).place(x=120, y=80)
# 修改新确认密码变量
new_password_confirm = tk.StringVar()
tk.Label(change_info__window, text="确认密码:").place(x=50, y=110)
new_password_confirm_entry = tk.Entry(change_info__window, show="*", textvariable=new_password_confirm).place(x=120,
y=110)
# 修改信息按钮绑定
sign_window_button = tk.Button(change_info__window, text='确认', command=change_info_click).place(x=150, y=140)
# 修改个人信息按钮绑定
change_info_button = tk.Button(index_window,text='修改个人信息',command=change_personal_info).place(x=100,y=100)
# 注册页面
def usr_sign_up():
sign_up_window = tk.Toplevel()
sign_up_window.title('注册')
sign_up_window.geometry('400x200')
# 注册点击事件
def sign_up_hit():
name = new_name.get()
psw = new_password.get()
conpsw = new_password_confirm.get()
with open('usrs_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
if psw!=conpsw:
tk.messagebox.showerror(title='Error', message='两次密码不一致请重新输入!')
else:
if name in usrs_info:
tk.messagebox.showerror(title='Error', message='用户名已存在')
else:
# 创建新
usrs_info[name] = psw
with open('usrs_info.pickle', 'wb') as f:
pickle.dump(usrs_info,f)
tk.messagebox.showinfo(title='注册成功', message='注册成功!')
sign_up_window.destroy()
# 注册名变量
new_name = tk.StringVar()
tk.Label(sign_up_window,text="注册名:").place(x=50,y=50)
new_name_entry = tk.Entry(sign_up_window,show=None,textvariable=new_name).place(x=120,y=50)
# 注册密码变量
new_password = tk.StringVar()
tk.Label(sign_up_window,text='密码:').place(x=50,y=80)
new_password_entry = tk.Entry(sign_up_window,show="*",textvariable=new_password).place(x=120,y=80)
# 注册确认密码变量
new_password_confirm= tk.StringVar()
tk.Label(sign_up_window,text="确认密码:").place(x=50,y=110)
new_password_confirm_entry = tk.Entry(sign_up_window,show="*",textvariable=new_password_confirm).place(x=120,y=110)
# 注册按钮和点击事件绑定
sign_window_button = tk.Button(sign_up_window,text='注册',command=sign_up_hit).place(x=150,y=140)
welcome()
截图
登录页面
注册页面
个人主页
修改个人信息失败
修改个人信息成功
重新登录twb
发现已注销除非重新注册。
发现成功修改个人信息。登陆上。
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
来源:https://blog.csdn.net/weixin_45860674/article/details/121803928
标签:python,tkinter,登录
0
投稿
猜你喜欢
js中string转int把String类型转化成int类型
2024-05-03 15:30:11
bpython 功能强大的Python shell
2022-05-08 22:12:06
mysql的存储过程、游标 、事务实例详解
2024-01-12 22:02:15
浅谈django框架集成swagger以及自定义参数问题
2022-01-09 20:25:10
Python常用数据结构和公共方法技巧总结
2021-10-18 06:02:01
在python中利用pycharm自定义代码块教程(三步搞定)
2022-11-20 01:00:50
Python-OpenCV实现图像缺陷检测的实例
2023-02-16 19:38:46
xhEditor的异步载入实现代码
2022-01-29 10:40:28
JavaScript给数组添加元素的6个方法
2024-04-30 08:46:47
python爬虫之利用selenium+opencv识别滑动验证并模拟登陆知乎功能
2023-10-17 22:33:37
详解phpMyAdmin的安装和配置
2007-06-15 09:56:00
关于Python 3中print函数的换行详解
2021-04-09 09:57:48
解决mysql创建数据库后出现:Access denied for user 'root'@'%' to database 'xxx'的问题
2024-01-21 22:51:52
在MySQL中使用更新日志文件
2009-02-26 16:22:00
一文搞懂Golang 时间和日期相关函数
2024-02-06 00:29:01
numpy创建神经网络框架
2023-07-10 22:17:50
Python读取文件内容为字符串的方法(多种方法详解)
2023-05-18 18:16:27
简单介绍Python中的JSON模块
2023-12-30 00:16:29
Python实现简单的获取图片爬虫功能示例
2023-01-31 06:15:13
PyQt5中向单元格添加控件的方法示例
2023-10-20 05:08:48