python设计一个登录界面

perhaps° 2024-07-19 11:35:01 阅读 84

游戏登录界面

1.设计登录界面1.1导入需要的工具包1.2窗口显示1.3登录界面图片显示1.4 用户名设置默认值1.5标签按钮输入框显示

2.登录功能实现2.1用户数据存储2.2登录和注册2.2.1登录功能实现注册功能实现

1.设计登录界面

1.1导入需要的工具包

<code>import tkinter as tk

from PIL import Image, ImageTk

import pickle

from tkinter import messagebox

import subprocess

1.2窗口显示

window = tk.Tk()

window.title("Welcome")

window.geometry('500x600')

1.3登录界面图片显示

image = Image.open("welcome.gif")

photo = ImageTk.PhotoImage(image)

1.4 用户名设置默认值

value = "GaoYue"

entry_var = tk.StringVar(window)

entry_var.set(value)

1.5标签按钮输入框显示

label1_image = tk.Label(window,image=photo)

label1_image.place(x=10,y=10)

label2 = tk.Label(window,text='User name',width=10,height=5)code>

label2.place(x=40,y=130)

label3 = tk.Label(window,text='password',width=10,height=5)code>

label3.place(x=40,y=180)

label4 = tk.Entry(window,width=30,bd=5)

label4.place(x=140,y=210)

label5 = tk.Entry(window,textvariable=entry_var,width=30,bd=5)

label5.place(x=140,y=160)

button1 = tk.Button(window,text='Loign',width=10,command=usr_login)code>

button1.place(x=140,y=280)

button2 = tk.Button(window,text='Sign up',width=10,command=usr_sign_up)code>

button2.place(x=250,y=280)

在这里插入图片描述

2.登录功能实现

2.1用户数据存储

<code>def usr_login():

usr_name = label5.get()

usr_pwd=label4.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)

2.2登录和注册

2.2.1登录功能实现

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 you"+usr_name)code>

tk.messagebox.showinfo(title="Game Start", message="Let's start the game!")code>

subprocess.run(["python", "井字棋.py"])

else:

tk.messagebox.showerror(message="Error,your password is wrong,try again.")code>

else:

print("4")

is_sign_up = tk.messagebox.askyesno(title="Welcome",message="You have not sign up yet.sign up today?")code>

注册功能实现

def sign_up():

nn = entry9.get()

np = entry10.get()

npf = entry11.get()

with open('usrs_info.pickle','rb') as usr_file:

exist_usr_info = pickle.load(usr_file)

if np != npf:

tk.messagebox.showerror("Error","The user has already signed up!")

elif nn in exist_usr_info:

print("已经注册过了")

tk.messagebox.showerror("Error","The user has already signed up!")

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!")

window.destroy()

button3 = tk.Button(window, text='Sign up', width=10,command=sign_up)code>

button3.place(x=140, y=150)

print("开始注册")

在这里插入图片描述



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。