源代码:
import tkinter as tk
from tkinter import messagebox # import this to fix messagebox error
import pickle
# 创建主窗口
window = tk.Tk()
window.title('欢迎来到登录界面')
window.geometry('450x300')
# 添加背景图
canvas = tk.Canvas(window, height=200, width=500) # 创建画布
image_file = tk.PhotoImage(file='feihao.png') # 加载图片文件
image = canvas.create_image(180, 20, anchor='nw', image=image_file) # 将图片置于画布上
canvas.pack() # 放置画布
# 用户信息
tk.Label(window, text='User name:').place(x=120, y=150) # 创建一个`label`名为`User name: `置于座标(120,150)
tk.Label(window, text='Password:').place(x=120, y= 190)
# 账号密码输入框
var_usr_name = tk.StringVar() # 定义变量,用于输入用户名
var_usr_name.set('write your name') # 输入用户名窗口默认显示'write your name'
entry_usr_name = tk.Entry(window, textvariable=var_usr_name) # 创建输入框
entry_usr_name.place(x=200, y=150)
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*') # `show`这个参数将输入的密码变为`***`的形式
entry_usr_pwd.place(x=200, y=190)
# 登录功能
def usr_login():
# 这两行代码就是获取用户输入的`usr_name`和`usr_pwd`
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
# 这里设置异常捕获,当我们第一次访问用户信息文件时是不存在的,所以这里设置异常捕获
try:
# 'rb'是打开文件为以二进制格式“读”,文件必须存在,否则会报错
with open('usrs_info.pickle', 'rb') as usr_file:
usrs_info = pickle.load(usr_file)
except FileNotFoundError:
# 这里就是我们在没有读取到`usr_file`的时候,程序会创建一个`usr_file`这个文件,并将管理员
# 的用户和密码写入,即用户名为`admin`密码为`admin`。
with open('usrs_info.pickle', 'wb') as usr_file:
usrs_info = {'admin': 'admin'}
pickle.dump(usrs_info, usr_file)
# 如果用户名和密码与文件中的匹配成功,则会登录成功,并跳出弹窗`how are you?`加上你的用户名
if usr_name in usrs_info:
if usr_pwd == usrs_info[usr_name]:
# 登录成功后的功能,可以改为其它功能
tk.messagebox.showinfo(title='Welcome', message='How are you?'+usr_name)
else:
tk.messagebox.showerror(message='Error, your password is wrong, try again')
else: # 如果发现用户名不存在
is_sign_up = tk.messagebox.askyesno('Welcome',
'You have not sign up yet. Sign up today?')
# 提示需不需要注册新用户
if is_sign_up:
usr_sign_up()
# 注册功能
def usr_sign_up():
def sign_to_Mofan_Python():
# 以下三行就是获取我们注册时所输入的信息
np = new_pwd.get()
npf = new_pwd_confirm.get()
nn = new_name.get()
# 这里是打开我们记录数据的文件,将注册信息读出
with open('usrs_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
# 这里就是判断,如果两次密码输入不一致,则提示`'Error', 'Password and confirm password must be the same!'`
if np != npf:
tk.messagebox.showerror('Error', 'Password and confirm password must be the same!')
# 如果用户名已经在我们的数据文件中,则提示`'Error', 'The user has already signed up!'`
elif nn in exist_usr_info:
tk.messagebox.showerror('Error', 'The user has already signed up!')
else:
exist_usr_info[nn] = np
with open('usrs_info.pickle', 'wb') as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.messagebox.showinfo('Welcome', 'You have successfully signed up!')
window_sign_up.destroy()
# 这里就是在主体窗口的window上创建一个Sign up window窗口
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('Sign up window')
# 用户名
new_name = tk.StringVar()
new_name.set('write your name')
tk.Label(window_sign_up, text='User name: ').place(x=10, y= 10) # 将`User name:`放置在座标(10,10)。
entry_new_name = tk.Entry(window_sign_up, textvariable=new_name) # 设置输入姓名框
entry_new_name.place(x=150, y=10) # `entry`放置在座标(150,10)
# 初次密码
new_pwd = tk.StringVar()
tk.Label(window_sign_up, text='Password: ').place(x=10, y=50)
entry_usr_pwd = tk.Entry(window_sign_up, textvariable=new_pwd, show='*')
entry_usr_pwd.place(x=150, y=50)
# 确认密码
new_pwd_confirm = tk.StringVar()
tk.Label(window_sign_up, text='Confirm password: ').place(x=10, y=90)
entry_usr_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*')
entry_usr_pwd_confirm.place(x=150, y=90)
# 下面的 sign_to_Mofan_Python 我们再后面接着说
btn_comfirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_to_Mofan_Python)
btn_comfirm_sign_up.place(x=150, y=130)
# 登录和注册按钮
btn_login = tk.Button(window, text='log in', command=usr_login) # 定义一个`button`按钮,名为`Login`,触发命令为`usr_login'
btn_login.place(x=270, y=230)
btn_sign_up = tk.Button(window, text='Sign up', command=usr_sign_up)
btn_sign_up.place(x=120, y=230)
window.mainloop()