tkinter GUI 客户端页面编程 登录注册案例开发

导读

使用 python默认自带的 tkinter开发实现一个客户端GUI登录注册界面,使用pickle模拟数据库用户信息更新

项目最终展示

tkinter GUI 客户端页面编程 登录注册案例开发_第1张图片

最终源码展示

# 登陆窗口
import tkinter as tk
from tkinter import messagebox
import pickle

# 初始化Tkinter
window = tk.Tk()
window.title('welocme to movi\'s python')
window.geometry('650x450')

# 欢迎LOGO
canvas = tk.Canvas(window, width=500,height=200)
image_file = tk.PhotoImage(file="weclome.jpg")
image = canvas.create_image(0,0,anchor='nw',image=image_file)
canvas.pack(side="top")

# 表单控件 文字
tk.Label(window, text="用户名").place(x = 150,y = 200)
tk.Label(window, text="密  码").place(x = 150,y = 250)

# 表单控件 输入框 用户名/密码
username_text = tk.StringVar()
entry_username = tk.Entry(window, textvariable=username_text,width=30)
entry_username.place(x = 240,y = 200)

password_text = tk.StringVar()
entry_password = tk.Entry(window, textvariable=password_text,show="*",width=30)
entry_password.place(x = 240,y = 250)

# 登陆注册按钮

# 点击登陆按钮后处理的业务逻辑
def user_login():
	# 获取用户的登陆信息
	user_name = username_text.get()
	user_pwd = password_text.get()

	# 将用户信息存入本地
	try:
		with open("user_info.pickle",'rb') as f:
			user_info = pickle.load(f)
	except Exception as e:
		with open("user_info.pickle",'wb') as f:
			user_info = {'admin':'admin'}
			pickle.dump(user_info,f)
	pass

	# 验证用户信息是否正确
	if user_name in user_info:
		if(user_pwd == user_info[user_name]):
			tk.messagebox.showinfo(title='weclome',message='hello '+user_name)
		else:
			tk.messagebox.showerror(title='error',message='密码错误,请重试!')
	else:
		is_sign_up = tk.messagebox.askyesno(title="是否注册", message="您还没注册,请问是否注册呢?")
		if(is_sign_up):
			user_signup()	

# 点击注册按钮后处理的业务逻辑
def user_signup():

	def sign_to_database():

		n_pwd = new_pwd.get()
		nr_pwd = new_pwd_confirm.get()
		n_name = new_name.get()

		# 读取本地用户信息
		with open('user_info.pickle','rb') as f:
			exists_user_info = pickle.load(f)

		# 判断是两处密码是否一致
		if(n_pwd != nr_pwd):
			tk.messagebox.showerror(title='error',message='两次密码输入不一致,请重试!')
			pass
		else:
			#判断该用户是否存在数据库
			if(n_name in exists_user_info):
				tk.messagebox.showerror(title='error',message='该用户已存在!')
			else:
				# 更新写入本地数据
				exists_user_info[n_name] = nr_pwd
				with open("user_info.pickle",'wb') as f:
					pickle.dump(exists_user_info,f)
				# 注册成功
				tk.messagebox.showinfo(title='welocme',message='注册成功!')
				# 关闭窗口
				window_signup.destroy()


	window_signup = tk.Toplevel(window)
	window_signup.title('welocme to sign up')
	window_signup.geometry('350x200')

	new_name = tk.StringVar()
	new_name.set('[email protected]')
	tk.Label(window_signup, text='User name: ').place(x=10, y= 10)
	entry_new_name = tk.Entry(window_signup, textvariable=new_name)
	entry_new_name.place(x=150, y=10)

	new_pwd = tk.StringVar()
	tk.Label(window_signup, text='Password: ').place(x=10, y=50)
	entry_usr_pwd = tk.Entry(window_signup, textvariable=new_pwd, show='*')
	entry_usr_pwd.place(x=150, y=50)

	# 再次密码输入框
	new_pwd_confirm = tk.StringVar()
	tk.Label(window_signup, text="Confirm password").place(x = 10, y = 90)
	tk.Entry(window_signup,textvariable=new_pwd_confirm,show="*").place(x = 150,y = 90)

	# 确定按钮
	btn_comfirm_signup = tk.Button(window_signup, text="Sign Up", command=sign_to_database).place(x = 150, y = 130)
	pass

btn_login = tk.Button(window, text="Login", command=user_login)
btn_login.place(x = 250, y = 290)

btn_signup = tk.Button(window, text="Sign Up", command=user_signup)
btn_signup.place(x = 350, y = 290)

window.mainloop() 

你可能感兴趣的:(python,GUI,python,tkinter)