当前位置: 首页 > news >正文

学会python——用python制作一个登录和注册窗口(python实例十八)

目录

1.认识Python

2.环境与工具

2.1 python环境

2.2 Visual Studio Code编译

 3.登录和注册窗口

3.1 代码构思

3.2 代码实例

3.3 运行结果

 4.总结


1.认识Python

Python 是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。

Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字或标点符号,它具有比其他语言更有特色的语法结构。

 

2.环境与工具

2.1 python环境

在Windows上使用命令行窗口查看所安装的python版本

python --version

 

2.2 Visual Studio Code编译

Visual Studio Code是一款由微软开发且跨平台的免费源代码编辑器。该软件以扩展的方式支持语法高亮、代码自动补全、代码重构功能,并且内置了命令行工具和Git 版本控制系统。

 3.登录和注册窗口

3.1 代码构思

通过定义五个函数,实现对磁盘文件的读和写、用户登录判断、注册信息判断、保存以及注册窗口界面的生成。

3.2 代码实例

import tkinter as tk
from tkinter import messagebox
import pickle
import os# 用pickle模块将字典变量进行序列转换并写入文件
def write_file(path, dic):with open(path, 'wb') as f:pickle.dump(dic, f)def read_file(path):with open(path, 'rb') as f:dic = pickle.load(f)return dic# 判断用户登录时录入的信息是否正确
def login():if os.path.exists('name.pickle'):userinfo = read_file('name.pickle')else:userinfo = {}name = txt_name.get()passwd = txt_passwd.get()if name in userinfo.keys():if userinfo[name] == passwd:messagebox.showinfo('登录成功', '欢迎您,' + name)else:messagebox.showerror('登录失败', '密码错误')txt_name.set('')txt_passwd.set('')e_name.focus()else:messagebox.showerror('登录失败', '用户名不存在')# 实现用户注册功能
def reg(regwin, path, name, passwd, passwd2):if passwd != passwd2:messagebox.showerror('注册失败', '两次输入的密码不一致')returnif os.path.exists(path):userinfo = read_file(path)else:userinfo = {}if name in userinfo.keys():messagebox.showerror('注册失败', '用户名已存在')returnelse:userinfo.update({name: passwd})write_file(path, userinfo)messagebox.showinfo('注册成功', '欢迎您,' + name)regwin.destroy()# 生成一个注册窗口界面
def create_regwindow():regwin = tk.Toplevel(win)regwin.title('注册')regwin.geometry('300x250')lb_name = tk.Label(regwin, text='用户名', bg='gainsboro', font=('arial', 12), height=1, width=10)lb_name.place(x=50, y=50)lb_passwd = tk.Label(regwin, text='密码', bg='gainsboro', font=('arial', 12), height=1, width=10)lb_passwd.place(x=50, y=100)lb_passwd2 = tk.Label(regwin, text='确认密码', bg='gainsboro', font=('arial', 12), height=1, width=10)lb_passwd2.place(x=50, y=150)txt_name = tk.StringVar()e_name = tk.Entry(regwin, textvariable=txt_name, font=('arial', 12), width=15)e_name.place(x=150, y=50, anchor='nw')txt_passwd = tk.StringVar()e_passwd = tk.Entry(regwin, textvariable=txt_passwd, font=('arial', 12), show='*')e_passwd.place(x=150, y=100, anchor='nw')txt_passwd2 = tk.StringVar()e_passwd2 = tk.Entry(regwin, textvariable=txt_passwd2, font=('arial', 12), show='*')e_passwd2.place(x=150, y=150, anchor='nw')# 生成一个注册按钮btn_reg = tk.Button(regwin, text='注册', bg='gainsboro', font=('arial', 12), height=1, width=10, command=lambda: reg(regwin, './part4/name.pickle', txt_name.get(), txt_passwd.get(), txt_passwd2.get()))btn_reg.place(x=100, y=200, anchor='nw')if __name__ == '__main__':win = tk.Tk()win.title('登录')win.geometry('300x200')win['background'] = 'gainsboro'lb_name = tk.Label(win, text='用户名', bg='gainsboro', font=('arial', 12), height=1, width=10)lb_name.place(x=50, y=50, anchor='nw')lb_passwd = tk.Label(win, text='密码', bg='gainsboro', font=('arial', 12), height=1, width=10)lb_passwd.place(x=50, y=100, anchor='nw')txt_name = tk.StringVar()e_name = tk.Entry(win, textvariable=txt_name, font=('arial', 12), width=15)e_name.place(x=150, y=50, anchor='nw')txt_passwd = tk.StringVar()e_passwd = tk.Entry(win, textvariable=txt_passwd, font=('arial', 12), show='*')e_passwd.place(x=150, y=100, anchor='nw')btn_log = tk.Button(win, text='登录', bg='gainsboro', font=('arial', 12), height=1, width=10, command=login)btn_log.place(x=50, y=150, anchor='nw')btn_reg = tk.Button(win, text='注册', bg='gainsboro', font=('arial', 12), height=1, width=10, command=create_regwindow)btn_reg.place(x=150, y=150, anchor='nw')win.mainloop()

3.3 运行结果

 登录

注册(以用户名111,密码111为例)

 登录成功

 4.总结

通过定义函数实现可视化界面,通过利用python库实现信息的交互。

相关文章:

  • python-Django项目:图书后台管理系统
  • SpringMVC之文件上传下载
  • spring6框架解析(by尚硅谷)
  • 降级Spring Boot版本
  • Generative Modeling by Estimating Gradients of the Data Distribution
  • ChatGPT-5:开创对话式AI的新纪元
  • 【MindSpore学习打卡】应用实践-计算机视觉-SSD目标检测:从理论到实现
  • 2024年过半,新能源车谁在掉链子?
  • TP8/6 子域名绑定应用
  • docker介绍与详细安装
  • Docker的基本介绍
  • docker compose方式部署Zabbix 7.0 LTS
  • 3.Charles抓包工具学习
  • 掌握命令行中pip源的切换:提升Python包管理效率
  • 【C语言】宏定义在 a.c 中定义,如何在 b.c 中使用?
  • 深入了解以太坊
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • canvas 绘制双线技巧
  • canvas实际项目操作,包含:线条,圆形,扇形,图片绘制,图片圆角遮罩,矩形,弧形文字...
  • CentOS7 安装JDK
  • Consul Config 使用Git做版本控制的实现
  • LeetCode算法系列_0891_子序列宽度之和
  • PAT A1017 优先队列
  • Python 基础起步 (十) 什么叫函数?
  • python学习笔记-类对象的信息
  • SegmentFault 技术周刊 Vol.27 - Git 学习宝典:程序员走江湖必备
  • vuex 笔记整理
  • 动态魔术使用DBMS_SQL
  • 基于Volley网络库实现加载多种网络图片(包括GIF动态图片、圆形图片、普通图片)...
  • 将回调地狱按在地上摩擦的Promise
  • 聊聊directory traversal attack
  • 前端存储 - localStorage
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • 使用parted解决大于2T的磁盘分区
  • 数据仓库的几种建模方法
  • 算法之不定期更新(一)(2018-04-12)
  • 小程序开发中的那些坑
  • 新版博客前端前瞻
  • ​【原创】基于SSM的酒店预约管理系统(酒店管理系统毕业设计)
  • ​520就是要宠粉,你的心头书我买单
  • ​油烟净化器电源安全,保障健康餐饮生活
  • #宝哥教你#查看jquery绑定的事件函数
  • $.ajax()参数及用法
  • (1)STL算法之遍历容器
  • (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
  • (webRTC、RecordRTC):navigator.mediaDevices undefined
  • (二)PySpark3:SparkSQL编程
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (几何:六边形面积)编写程序,提示用户输入六边形的边长,然后显示它的面积。
  • (转)c++ std::pair 与 std::make
  • (转)JAVA中的堆栈
  • (转)真正的中国天气api接口xml,json(求加精) ...
  • (转载)PyTorch代码规范最佳实践和样式指南
  • (状压dp)uva 10817 Headmaster's Headache
  • .form文件_一篇文章学会文件上传