本篇文章只讲解登入和注册页面,在后面的文章中会讲解井字棋游戏,然后把井字棋和登入界面进行连接,整合成一个完整的游戏。
登入注册界面在本篇文章的末尾。
1.实现登入界面
(1)导入图片
把这张图片存储在与代码路径一样的文件夹,并取名为welcome,该图片是gif格式。
先创建个窗口,然后把图片绘制到里面。
代码:
import tkinter as tk
window = tk.Tk()
window.title("Welcome")
window.geometry("600x400")logo = tk.PhotoImage(file="image/welcome.gif")
label = tk.Label(window, image=logo)
label.pack()window.mainloop()
运行结果如下:
(2)设置窗口输入框和按钮
在窗口图片下面设置名字和密码以及相应的输入框,还有登入注册按钮。
代码:
t1 = tk.Label(window, text="User name")
t1.place(x=70, y=130)
label_title1 = tk.Entry(window, bd=5)
label_title1.pack(padx=10, pady=5)
t2 = tk.Label(window, text="Password")
t2.place(x=70, y=180)
label_title2 = tk.Entry(window, bd=5)
label_title2.pack(padx=10, pady=5)button1 = tk.Button(window, text="登入", width=10, height=2, fg='black')
button1.place(x=180, y=250)
button2 = tk.Button(window, text="注册", width=10, height=2, fg='black')
button2.place(x=350, y=250)
运行结果如下:
2.实现注册窗口
(1)设置注册窗口的输入框和标签
定义函数把注册框的注册窗口都放置在函数中
def usr_sign_up():print('开始注册')
# 注册窗口
win = tk.Toplevel(window)
win.title("Sign up window")
win.geometry("300x200")
lb1_title2 = tk.Label(win, text='User name:')
lb1_title2.grid(row=0, column=0)
e1 = tk.Entry(win, bd=5, width=10)
e1.grid(row=0, column=2)
lb2 = tk.Label(win, text='Password:')
lb2.grid(row=1, column=0)
e2 = tk.Entry(win, bd=5, width=10)
e2.grid(row=1, column=2)
lb3 = tk.Label(win, text='Confirm password:')
lb3.grid(row=2, column=0)
e3 = tk.Entry(win, bd=5, width=10)
e3.grid(row=2, column=2)
button_z = tk.Button(win, text="Sign up",command=sign_up)
button_z.grid(row=4, column=2)
我们还需要把注册界面的函数连接到登录界面的按钮中
button2 = tk.Button(window, text="注册", width=10, height=2, fg='black',command=usr_sign_up)
button2.place(x=350, y=250)
这样我们点击登入窗口中的注册按钮就会弹出注册界面
(2)获取注册数据判断是否注册过
在这之前我们先在代码最上方导入两个模块,如下所示,我们再定义一个函数,检测三个输入框输入的值,如果输入框中两次输入的密码不一样则会弹出提示,如果注册过也会弹出提示,如果注册成功会提示你注册成功,最后销毁窗口。
1.import tkinter.messagebox:
导入了tkinter.messagebox模块,该模块提供了对话框和消息框的功能,用于在GUI应用程序中显示消息、警告或确认对话框等。常见的方法包括showinfo()、showwarning()、showerror()等,用于显示不同类型的消息框。
2.import pickle:
这行代码导入了pickle模块,pickle模块是Python中用于序列化和反序列化对象的模块。通过pickle模块,可以将对象转换为字节流进行存储或传输,也可以从字节流中恢复对象。pickle模块提供了dump()、load()等方法来实现对象的序列化和反序列化操作。
代码:
import tkinter.messagebox
import pickle
def sign_up():#获取输入nn = e1.get()np = e2.get()npf =e3.get()#读取后台数据with open('usrs_info.pickle', 'rb') as usr_file:#从usr_file文件中加载数据,将其反序列化为Python对象,并将结果存储在exist_usr_info变量中。exist_usr_info = pickle.load(usr_file)#判断两次输入的密码是否一致if np != npf:tk.messagebox.showerror('Error','Password and confirm password must be the same')#判断用户名是否已经注册过elif nn in exist_usr_info:print('已经注册过了')tk.messagebox.showerror('Error','The user has already signed up')#注册成功,写入后台else:exist_usr_info[nn] = npwith open('user_info.pickle', 'wb') as usr_file:pickle.dump(exist_usr_info, usr_file)tk.messagebox.showinfo('Welcome','You have successfully signed up')# 销毁窗口win.destroy()
在注册界面的按钮中导入此函数,在注册时好进行判断
以下是代码运行的结果:
3.定义登入按钮
当用户通过输入框输入用户名和密码。尝试打开一个名为文件,如果文件存在,则加载其中的用户信息数据到文件中。如果文件不存在则创建一个默认的用户信息字典并将其序列化写入文件中。检查用户输入的用户名是否在用户信息字典中。如果用户名存在,检查用户输入的密码是否与用户名对应的密码匹配。如果密码匹配,显示欢迎消息框,如果密码不匹配,显示密码错误的错误消息框。如果用户名不存在,显示询问框询问用户是否要注册。如果用户选择注册,调用usr_sign_up()函数进行注册操作。
代码:
def usr_login():#获取输入框label_title1中用户输入的用户名,并将其存储在usr_name变量中。usr_name = label_title1.get()#获取输入框label_title2中用户输入的密码,并将其存储在usr_pwd变量中。usr_pwd = label_title2.get()print(usr_name)try:#打开名为usrs_info.pickle的文件,以二进制只读模式打开,并将文件对象赋给usr_file变量。with open('usrs_info.pickle', 'rb') as usr_file:print('1')#从usr_file文件中加载数据,将其反序列化为Python对象,并将结果存储在usrs_info变量中。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'}#将usrs_info字典序列化并写入usr_file文件。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)window.destroy()subprocess.Popen(["python", "demo3.py"])# main()else:#显示一个错误框,提示用户密码错误。tk.messagebox.showerror(message='Error,your password is wrong,try again.')else:#显示一个询问框,询问用户是否要注册。is_sign_up = tk.messagebox.askyesno(title='Welcome', message='You have not sign up yet.Sign up today?')if is_sign_up:usr_sign_up()
以下是运行结果:
完整代码如下:
import tkinter as tk
import tkinter.messagebox
import pickle
import subprocesswindow = tk.Tk()
window.title("Welcome")
window.geometry("600x400")
logo = tk.PhotoImage(file="image/welcome.gif")
label = tk.Label(window, image=logo)
label.pack()
t1 = tk.Label(window, text="User name")
t1.place(x=70, y=130)
label_title1 = tk.Entry(window, bd=5)
label_title1.pack(padx=10, pady=5)
t2 = tk.Label(window, text="Password")
t2.place(x=70, y=180)
label_title2 = tk.Entry(window, bd=5)
label_title2.pack(padx=10, pady=5)def usr_login():#获取输入框label_title1中用户输入的用户名,并将其存储在usr_name变量中。usr_name = label_title1.get()#获取输入框label_title2中用户输入的密码,并将其存储在usr_pwd变量中。usr_pwd = label_title2.get()print(usr_name)try:#打开名为usrs_info.pickle的文件,以二进制只读模式打开,并将文件对象赋给usr_file变量。with open('usrs_info.pickle', 'rb') as usr_file:print('1')#从usr_file文件中加载数据,将其反序列化为Python对象,并将结果存储在usrs_info变量中。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'}#将usrs_info字典序列化并写入usr_file文件。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 password is wrong,try again.')else:#显示一个询问框,询问用户是否要注册。is_sign_up = tk.messagebox.askyesno(title='Welcome', message='You have not sign up yet.Sign up today?')if is_sign_up:usr_sign_up()def usr_sign_up():print('开始注册')def sign_up():#获取输入nn = e1.get()np = e2.get()npf =e3.get()#读取后台数据with open('usrs_info.pickle', 'rb') as usr_file:#从usr_file文件中加载数据,将其反序列化为Python对象,并将结果存储在exist_usr_info变量中。exist_usr_info = pickle.load(usr_file)#判断两次输入的密码是否一致if np != npf:tk.messagebox.showerror('Error','Password and confirm password must be the same')#判断用户名是否已经注册过elif nn in exist_usr_info:print('已经注册过了')tk.messagebox.showerror('Error','The user has already signed up')#注册成功,写入后台else:exist_usr_info[nn] = npwith open('user_info.pickle', 'wb') as usr_file:pickle.dump(exist_usr_info, usr_file)tk.messagebox.showinfo('Welcome','You have successfully signed up')# 销毁窗口win.destroy()#注册窗口win = tk.Toplevel(window)win.title("Sign up window")win.geometry("300x200")lb1_title2 = tk.Label(win, text='User name:')lb1_title2.grid(row=0, column=0)e1 = tk.Entry(win, bd=5, width=10)e1.grid(row=0, column=2)lb2 = tk.Label(win, text='Password:')lb2.grid(row=1, column=0)e2 = tk.Entry(win, bd=5, width=10)e2.grid(row=1, column=2)lb3 = tk.Label(win, text='Confirm password:')lb3.grid(row=2, column=0)e3 = tk.Entry(win, bd=5, width=10)e3.grid(row=2, column=2)button_z = tk.Button(win, text="Sign up", command=sign_up)button_z.grid(row=4, column=2)button1 = tk.Button(window, text="登入", width=10, height=2, fg='black',command=usr_login)
button1.place(x=180, y=250)
button2 = tk.Button(window, text="注册", width=10, height=2, fg='black',command=usr_sign_up)
button2.place(x=350, y=250)window.mainloop()