- import subprocess
-
- import tkinter as tk
-
- import pickle
-
- from tkinter import messagebox
-
-
-
- window = tk.Tk()
-
- window.title('Welcome')
-
- window.geometry('450x300')

导入必要的模块,并初始化了主窗口window,设置了窗口的标题和大小。
- 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')

创建一个画布canvas,并在画布上显示了一个welcome.gif图像(结尾附图像)
- 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)

创建了两个标签User name和Password,以及对应的输入框entry_usr_name和entry_usr_pwd,用于用户输入用户名和密码。
- 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='登陆成功!!用户命:' + usr_name)
-
- window.destroy()
-
- # import lx3
-
- subprocess.Popen(["python", "九宫格画布.py"])
-
- else:
-
- tk.messagebox.showerror(message='错误,密码错误,请重试.')
-
- else:
-
- print('4')
-
- is_sign_up = tk.messagebox.askyesno(title='Welcome', message='你还没有注册呢。今天就报名?')
-
- if is_sign_up:
-
- usr_sign_up()


标红的位置为连接到同目录下的游戏程序(本博客2024.04.18日发布的程序)
登录函数usr_login包含了登录的逻辑,包括读取用户信息、验证用户名和密码,并显示相应的消息框。
- def usr_sign_up():
-
- print('开始注册')
-
-
-
- def sign_up():
-
- # 获取输入
-
- nn = new_name.get()
-
- np = new_pwd.get()
-
- npf = new_pwd_confirm.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.showinfo('欢迎你', '您已成功注册!')
-
- # 然后销毁窗口。
-
- window_sign_up.destroy()
-
-
-
- # 注册窗口
-
- window_sign_up = tk.Toplevel(window)
-
- window_sign_up.geometry('350x200')
-
- window_sign_up.title('Sign up window')
-
-
-
- tk.Label(window_sign_up, text='User name: ').place(x=10, y=10) # 将`User name:`放置在坐标(10,10)。
-
- tk.Label(window_sign_up, text='Password: ').place(x=10, y=50)
-
- tk.Label(window_sign_up, text='Confirm password: ').place(x=10, y=90)
-
-
-
- # 用户名输入框
-
- new_name = tk.StringVar() # 将输入的注册名赋值给变量
-
- new_name.set(var_usr_name.get()) # 将最初显示定为example@python.com
-
- entry_new_name = tk.Entry(window_sign_up, textvariable=new_name) # 创建一个注册名的entry,变量为new_name
-
- entry_new_name.place(x=150, y=10) # entry放置在坐标(150,10).
-
-
-
- # 密码输入框
-
- new_pwd = tk.StringVar()
-
- 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()
-
- entry_usr_pwd_confirm = tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*')
-
- entry_usr_pwd_confirm.place(x=150, y=90)
-
-
-
- # 注册按钮
-
- btn_comfirm_sign_up = tk.Button(window_sign_up, text='Sign up', command=sign_up)
-
- btn_comfirm_sign_up.place(x=150, y=130)



注册函数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)

创建登录和注册按钮,并将它们放置在窗口的指定位置。按钮的command属性绑定了相应的登录和注册函数。
window.mainloop()

启动了Tkinter的主事件循环,这使得窗口保持打开状态,并能够响应用户的交互,如点击按钮、输入文本等。
整个代码的结构是典型的GUI应用程序结构,包括窗口初始化、用户界面元素布局、事件处理函数定义和主事件循环启动。