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

news/2024/7/8 9:09:57 标签: python, 开发语言

目录

1.认识Python

2.环境与工具

python%E7%8E%AF%E5%A2%83-toc" style="margin-left:40px;">2.1 python环境

2.2 Visual Studio Code编译

 3.登录和注册窗口

3.1 代码构思

3.2 代码实例

3.3 运行结果

 4.总结


1.认识Python

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

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

 

2.环境与工具

python%E7%8E%AF%E5%A2%83">2.1 python%E7%8E%AF%E5%A2%83&spm=1001.2101.3001.7020" title="python环境">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('注册失败', '两次输入的密码不一致')
        return
    if os.path.exists(path):
        userinfo = read_file(path)
    else:
        userinfo = {}
    if name in userinfo.keys():
        messagebox.showerror('注册失败', '用户名已存在')
        return
    else:
        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库实现信息的交互。


http://www.niftyadmin.cn/n/5536894.html

相关文章

Rust简明教程第四章-其他类型泛型

观看B站软件工艺师杨旭的rust教程学习记录,有删减有补充 枚举 枚举类型可以让我们的程序使用一些固定长度和固定数值的变量值范围 enum 枚举 枚举类型中的值被称为变体 enum IpAddrKind{V4,//变体V6,//变体 } fn main(){let four IpAddrKind::V4;let six IpA…

django admin添加自己的页面

建立模型 如果要单独建一个页面,用于展示model的数据,可以新建一个model,继承自要展示的那个类 class ViewsByDayModel(ViewsByDay): # 父类为要展示的model类class Meta:proxy True # 使用代理verbose_name 每日浏览次数统计verbose_nam…

ChatGPT在Java后端开发中的应用与影响

随着人工智能技术的发展,尤其是OpenAI推出的聊天机器人模型ChatGPT,其强大的自然语言理解和生成能力正在改变着我们的生活和工作方式。在Java后端开发领域,ChatGPT同样有着广泛的应用前景,并且能够为Java后端开发者带来诸多好处。…

Android高级——智能指针

智能指针 智能指针是一种能够自动维护对象引用计数的技术 引用了一个实际使用的对象,而不是一个指针智能指针构造时,增加它所引用的对象的引用计数智能指针析构时,减少它所引用的对象的引用计数 但智能指针无法解决循环引用问题&#xff0…

【项目管理】项目风险管理(Word原件)

风险和机会管理就是在一个项目开发过程中对风险进行识别、跟踪、控制的手段。风险和机会管理提供了对可能出现的风险进行持续评估,确定重要的风险机会以及实施处理的策略的一种规范化的环境。包括识别、分析、制定处理和减缓行动、跟踪 。合理的风险和机会管理应尽力…

三菱PLC 6行程序实现8电机顺序启动逆序停止

目录 概要指令概述顺序启动梯形图逆序停止梯形图 概要 这里主要用到的是三菱的位左移(SFTL)和右移(SFTR)指令来实现顺序启动和逆序停止,时间可自由设置,除了应用在电机上,也可以用来做跑马灯。…

有哪些在本地运行大模型的方法

前言 在本文中,我们将看到在本地运行任何 LLM 的不同方法 1/ LMStudio LM Studio 是一款桌面应用程序,用于在计算机上运行本地 LLM。链接:https://lmstudio.ai/ 2/ Ollama Ollama 是一款工具,可让您在机器上本地运行开源大型语…

“拆分盘投资:机遇与风险并存

一、引言 随着互联网技术的日新月异,金融投资领域迎来了前所未有的变革,其中拆分盘作为一种新兴的投资模式,正逐渐进入公众的视野。其独特的价值增长逻辑和创新的投资机制,为投资者开辟了新的财富增值渠道。本文旨在深入探讨拆分…