1.导入必要的库:
import pickle
import tkinter as tk
import tkinter.messagebox
2.创建主窗口:
window = tk.Tk()
window.title('Welcome')
window. Geometry('450x300')
3.加载欢迎图片:
canvas = tk.Canvas(window, height=200, width=500)
image_file = tk.PhotoImage(file='welcome/welcome.gif')
image = canvas.create_image(0, 0, anchor='nw', image=image_file)
canvas.pack(side='top')
4.创建用户信息的标签和输入框:
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()
var_usr_name.set('example@python.com')
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)
5.创建登录和注册按钮,并定义相应的函数:
def usr_login():
...
def usr_sign_up():
...
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)
(1)登录函数里面的全部内容:
在 usr_login
函数中:
- 首先获取用户输入的用户名和密码。
- 然后尝试打开一个名为 “usrs_info.pickle” 的文件,如果文件存在,尝试加载其中的用户信息。如果文件不存在,则创建该文件,并将初始用户信息写入其中。
- 接着检查用户输入的用户名是否存在于用户信息中,如果存在则检查密码是否匹配,若匹配则显示欢迎信息,否则显示密码错误的提示框。如果用户名不存在,则提示用户是否进行注册
def usr_login():
usr_name = var_usr_name.get()
usr_pwd = var_usr_pwd.get()
print(usr_name)
try:
with open("usrs_info.pickle","rb")as usr_file:
print("1")
usrs_info=pickle.load(usr_file)
print(usrs_info)
except FileNotFoundError:
with open("usrs_info.pickle","wb")as usr_file:
print("2")
usrs_info={"admin":"admin"}
pickle.dump(usrs_info,usr_file)
print("ok")
print("usr_name:",usr_name)
if usr_name in usrs_info:
print("3")
if usr_pwd == usrs_info[usr_name]:
tk.messagebox.showinfo(title = "Welcome",message="How are your? "+usr_name)
else:
tk.messagebox.showerror(message="Error,your,passworrd is wrong ,try agin.")
else:
print("4")
is_sign_up = tk.messagebox.askyesno(title="Welcome",message="You have not sing up yet .Sing up today?")
if is_sign_up:
usr_sign_up()
(2)注册函数的全部内容:
在 usr_sign_up
函数中:
- 创建一个新窗口用于注册。
- 获取用户输入的用户名和密码,再次确认密码。
- 检查两次输入的密码是否一致,如果不一致则提示错误。
- 如果用户名已存在则提示用户已存在。
- 如果用户名不存在且两次密码输入一致,则将新用户名和密码添加到用户信息中,并更新存储文件。
- 最后显示注册成功的提示,并关闭注册窗口。
def usr_sign_up():
print("注册开始")
def sign_up():
nn = input_entry1.get()
np = input_entry2.get()
npf = input_entry3.get()
# 读取后台数据
with open("usrs_info.pickle", "rb") as usr_file:
exist_usr_info = pickle.load(usr_file)
#
if np != npf:
tk.messagebox.showerror("错误,两次密码输入不一样")
elif nn in exist_usr_info:
print("已经注册过了")
tk.messagebox.showerror("错误,用户已存在")
#
else:
exist_usr_info[nn] = np
with open("usrs_info.pickle", "wb") as usr_file:
pickle.dump(exist_usr_info, usr_file)
tk.messagebox.showerror("注册成功")
# 销毁窗口
root.destroy()
root = tk.Tk()
root.title("Sign up window")
root.geometry("350x200")
# 设置标签和输入框
tk.Label(root, text="User name").grid(row=1, column=0, sticky='e', padx=10, pady=5)
input_entry1 = tk.Entry(root)
input_entry1.grid(row=1, column=1, padx=10, pady=5)
tk.Label(root, text="Password").grid(row=2, column=0, sticky='e', padx=10, pady=5)
input_entry2 = tk.Entry(root, show='*')
input_entry2.grid(row=2, column=1, padx=10, pady=5)
tk.Label(root, text="Confirm password").grid(row=3, column=0, sticky='e', padx=10, pady=5)
input_entry3 = tk.Entry(root, show='*')
input_entry3.grid(row=3, column=1, padx=10, pady=5)
# 设置按钮
signup_button = tk.Button(root, text="Sign Up",command=sign_up)
signup_button.grid(row=4, column=1, columnspan=1, padx=10, pady=5)
6.主循环,使窗口持续显示:
window.mainloop()