简单实现登陆注册gui界面以及打包成exe文件

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# @Author : Benjamin

# @Time   : 2021/10/11 20:33

# from tkinter import *

from tkinter import messagebox

from tkinter import Tk

import tkinter as tk

# 创建窗体

my_window=Tk()

my_window.title(" 登陆 ")

# 设置窗口大小并居中显示,

# 屏幕的宽度和亮度

screen_width,screen_height=my_window.maxsize()# 获取当前屏幕的高度和宽度

# 窗体的宽度高度

width=250

height=200

# 设置窗体再屏幕中央显示

align_str="%dx%d+%d+%d"%(width,height,(screen_width-width)/2,(screen_height-height)/2)

my_window.geometry(align_str)

# 设置宽高不可缩放

my_window.resizable(width=False,height=False)

# 添加标签 , 账户,密码

user_name_lable=tk.Label(my_window,text=" 账号 ",font=('FangSong',14))

user_name_lable.place(x=30,y=30)

user_pwd_lable=tk.Label(my_window,text=" 密码 ",font=('FangSong',14))

user_pwd_lable.place(x=30,y=70)

# 账号输入框,输入框文本设置

user_name_text=tk.StringVar()

user_name_text.set(" 输入账号 ")

user_name_entry=tk.Entry(my_window,textvariable=user_name_text,font=('FangSong',14),width=15)

user_name_entry.place(x=80,y=30)

# 密码输入框,输入框文本设置

user_pwd_text=tk.StringVar()# 定义文本框

user_pwd_text.set(" 输入密码 ")# 文本框提示语设置

user_pwd_entry=tk.Entry(my_window,textvariable=user_pwd_text,font=('FangSong',14),width=15)

user_pwd_entry.place(x=80,y=70)

# 数据读取,读取 data 文件

def read():

     with open('data.txt','r') as file:

         rows=file.readlines()

         user_info_dict={}

         # 字典数据封装

         for row in rows:

             dict_list=row.strip().split(':')# 去掉前后空格 , 然后切割生成数组

             # print(dict_list)

             user_info_dict[dict_list[0]]=dict_list[1]

         return user_info_dict

# 数据写入,打开 data 文件,写入数据

def write(name,pwd):

     with open('data.txt','a+') as file:#a+ 是追加写入

         file.write(name+":"+pwd+'\n')

# 登陆按钮事件处理

def user_login():

     # 获取用户输入的账号和密码

     name=user_name_text.get()

     pwd=user_pwd_text.get()

     print(name,pwd)

     user_dict=read()

     if name !='' and pwd !='':

         if name in user_dict and user_dict[name]==pwd:

             print("ok")

             messagebox.showinfo(title=" 成功 ",message=" 欢迎 "+name+" 登陆到这个页面 ")

         else:

             messagebox.showerror(title=" 错误 ",message=" 密码或用户名错误 ")

             # print(" 密码或用户名错误 ")

     else:

         messagebox.showerror(title=" 错误 ",message=" 请输入完整内容,用户名和密码不能为空!!! ")

         # print(" 请输入完整内容,外汇跟单https://www.gendan5.com/用户名和密码不能为空!!! ")

# 注册事件的处理

def user_reg():

     # 获取用户名和密码

     name=user_name_text.get()

     pwd=user_pwd_text.get()

     print(name,pwd)

     user_dict = read()

     if name!=""and pwd!="":

             if  name not in user_dict:

                 write(name,pwd)

                 messagebox.showinfo(title="ok",message=" 注册成功 ")

             else:

                 messagebox.showerror(title=" 错误 ",message=" 用户名已存在!!! ")

     else:

         messagebox.showerror(title=" 错误 ",message=" 请输入完整内容,用户名和密码不能为空!!! ")

# 按钮 , 登陆按钮,事件处理

user_login_button=tk.Button(my_window,text=" 登陆 ",font=('FangSong',14),command=user_login)

user_login_button.place(x=30,y=120)

# 注册。注册按钮,事件处理

user_reg_button=tk.Button(my_window,text=" 注册 ",font=('FangSong',14),command=user_reg)

user_reg_button.place(x=150,y=120)

my_window.mainloop()


请使用浏览器的分享功能分享到微信等