井字棋游戏
游戏登录注册界面
1.1导入需要的模块
导入tkinter库,从中导入messagebox模块;导入PIL库,从中导入pickle模块。
import tkinter as tk
from PIL import Image,ImageTk
import pickle
from tkinter import messagebox
1.2游戏窗口显示
设置窗口标题和大小。
window = tk.Tk()
window.title('Welcome')
window.geometry('500x400')
1.3创建两个标签(Label)
用于在tkinter窗口中显示用户名和密码的标签,并设置标签文本,宽度,高度及坐标位置。
var1 = tk.Label(window, text='User name', width=10, height=1)
var1.place(x=70, y=190)
var2 = tk.Label(window, text='Password', width=10, height=1)
var2.place(x=65, y=230)
1.4创建一个标签(Label)
将image转换为tkinter可以显示的格式图像photo,并设置标签的坐标位置。
var3 = tk.Label(window,image=photo)
var3.place(x=10, y=10)
1.5创建两个输入框(Entry)
用于用户输入用户名和密码,并设置其边框宽度和输入框宽度和坐标位置。
Iteme1 = tk.Entry(window, bd=1, width=20)
Iteme1.place(x=170, y=190)
Iteme2 = tk.Entry(window, bd=1, width=20)
Iteme2.place(x=170, y=230)
1.6 创建一个输入框(Entry)
使用StringVar对象来保存输入框的内容。
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window,textvariable=var_usr_pwd)
1.7 定义一个函数
处理用户登录,用get()方法从输入框获取用户名和密码。用try块尝试打开文件读取用户信息,会触发异常,进入excepe块;在except块中创建一个新文件,并将初始用户信息存储在其中
def usr_login():
usr_name=Iteme1.get()
usr_pwd=Iteme2.get()
print(usr_name)
try:
with open('usr_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)
1.8 判断条件语句
首先检查用户输入的用户名是否存在。如果存在,它继续检查输入的密码是否与对应用户名已保存的密码匹配。如果密码匹配,它会显示一个欢迎消息框,否则会显示一个密码错误的消息框。如果用户名不存在,程序会提示用户是否要进行注册。如果用户选择进行注册,会调用usr_sign_up()函数。
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:
print('4')
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()
1.9 创建一个按钮
用于用户登录操作。设置按钮的文本、宽度、高度、前景色及坐标。点击按钮时,会触发usr_login()函数。
button1 = tk.Button(window, text='Login', width=6, height=1, fg='black',command=usr_login)
button1.place(x=180, y=260)
1.10注册函数
用于创建用户注册窗口,设置窗口标题以及大小。定义三个标签,分别用于提示用户输入的用户名、密码和确认密码。每个标签都使用place()方法进行设置。创建三个文本输入框,用于用户输入用户名、密码和确认密码,也都使用place()方法进行设置。
def sign_up():
# 获取输入
nn = Iteme3.get()
np = Iteme4.get()
npf = Iteme5.get()
# 读取后台数据
with open('usrs_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
# 判断两次输入的密码是否一致
if np != npf:
tk.messagebox.showerror('Error', 'Password and confirm password must be the same!')
# 注册成功,写入后台
1.11注册功能实现
用于处理用户注册操作。首先,获取用户在注册窗口中输入的用户名、密码和确认密码。然后读取用户信息,接着,检查用户输入的密码和确认密码是否一致。如果不一致,则会弹出一个错误提示框。如果一致,就将用户名和密码添加到已有的用户信息中,并将更新后的用户信息写入到文件中。然后,弹出一个提示框,显示注册成功的消息最后,代码销毁了注册窗口中的输入框。
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!')
# 然后销毁窗口
Iteme1.destroy()
1.12按钮创建
用于触发用户注册操作。设置按钮的文本、宽度、高度、前景色及坐标。点击按钮时,会触发sign_up()函数。
button = tk.Button(window, text='Sign up', width=6, height=1, fg='black',command=sign_up)
button.place(x=150, y=150)
button2 = tk.Button(window, text='Sign up', width=6, height=1, fg='black',command=usr_sign_up)
button2.place(x=250, y=260)
1.13 完整代码如下
import tkinter as tk
from PIL import Image,ImageTk
import pickle
from tkinter import messagebox
window = tk.Tk()
window.title('Welcome')
window.geometry('500x400')
image = Image.open("1.png")
photo = ImageTk.PhotoImage(image)
var1 = tk.Label(window, text='User name', width=10, height=1)
var1.place(x=70, y=190)
var2 = tk.Label(window, text='Password', width=10, height=1)
var2.place(x=65, y=230)
var3 = tk.Label(window,image=photo)
var3.place(x=10, y=10)
Iteme1 = tk.Entry(window, bd=1, width=20)
Iteme1.place(x=170, y=190)
Iteme2 = tk.Entry(window, bd=1, width=20)
Iteme2.place(x=170, y=230)
var_usr_pwd = tk.StringVar()
entry_usr_pwd = tk.Entry(window,textvariable=var_usr_pwd)
def usr_login():
usr_name=Iteme1.get()
usr_pwd=Iteme2.get()
print(usr_name)
try:
with open('usr_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 password is wrong,try again.')
else:
print('4')
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()
button1 = tk.Button(window, text='Login', width=6, height=1, fg='black',command=usr_login)
button1.place(x=180, y=260)
def usr_sign_up():
window = tk.Tk()
window.title('Sign up window')
window.geometry('350x200')
var4 = tk.Label(window, text='User name:', width=10, height=1)
var4.place(x=10, y=30)
var5 = tk.Label(window, text='Password:', width=10, height=1)
var5.place(x=10, y=70)
var6 = tk.Label(window, text='Confirm password:', width=16, height=1)
var6.place(x=10, y=110)
Iteme3 = tk.Entry(window, bd=1, width=20)
Iteme3.place(x=150, y=30)
Iteme4 = tk.Entry(window, bd=1, width=20)
Iteme4.place(x=150, y=70)
Iteme5 = tk.Entry(window, bd=1, width=20)
Iteme5.place(x=150, y=110)
def sign_up():
# 获取输入
nn = Iteme3.get()
np = Iteme4.get()
npf = Iteme5.get()
# 读取后台数据
with open('usrs_info.pickle', 'rb') as usr_file:
exist_usr_info = pickle.load(usr_file)
# 判断两次输入的密码是否一致
if np != npf:
tk.messagebox.showerror('Error', 'Password and confirm password must be the same!')
# 注册成功,写入后台
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!')
# 然后销毁窗口
Iteme1.destroy()
button = tk.Button(window, text='Sign up', width=6, height=1, fg='black',command=sign_up)
button.place(x=150, y=150)
window.mainloop()
button2 = tk.Button(window, text='Sign up', width=6, height=1, fg='black',command=usr_sign_up)
button2.place(x=250, y=260)
window.mainloop()