Python3 Tkinkter + SQLite实现登录和注册界面
作者:广州-辉Python开发 发布时间:2023-10-01 14:56:15
标签:python3,登录,注册
本文实例为大家分享了Python3 Tkinkter + SQLite 实现登录和注册界面,供大家参考,具体内容如下
Ubuntu14 + Python3.4 + Pycharm2018
一、设计数据库
数据库内容(login.db):用户名,密码,邮箱,密码错误次数(达到三次时锁定)
create table loginuser (
username varchar(30) NOT NULL PRIMARY KEY ,
password varchar(16) NOT NULL ,
email varchar NOT NULL ,
loginerror int
);
二、登录界面Tkinter
login.py
1.登录界面代码
2.注册界面代码
from tkinter import *
import sqlite3
import tkinter.messagebox as messagebox
class LoginPage:
"""登录界面"""
def __init__(self, master):
self.root = master
self.root.geometry('400x200+600+400')
self.root.title('项目资源管理平台Alpha0.1')
self.conn = sqlite3.connect('data/login.db')
self.username = StringVar()
self.password = StringVar()
self.page = Frame(self.root)
self.creatapage()
def creatapage(self):
"""界面布局"""
Label(self.page).grid(row=0)
Label(self.page, text='用户名:').grid(row=1, stick=W, pady=10)
Entry(self.page, textvariable=self.username).grid(row=1, column=1, stick=E)
Label(self.page, text='密码:').grid(row=2, stick=W, pady=10)
Entry(self.page, textvariable=self.password, show='*').grid(row=2, stick=E, column=1)
Button(self.page, text='登录', command=self.login).grid(row=3, stick=W, pady=10)
Button(self.page, text='注册账号', command=self.register).grid(row=3, stick=E, column=1)
self.page.pack()
def login(self):
"""登录功能"""
curs = self.conn.cursor()
query = "select username, password, loginerror from loginuser where username='%s'" % self.username.get()
curs.execute(query) # 返回一个迭代器
c = curs.fetchall() # 接收全部信息
if len(c) == 0:
messagebox.showerror('登录失败', '账户不存在')
else:
us, pw, lerror = c[0]
if lerror >= 3:
messagebox.showwarning('登录失败', '账户已被锁定')
elif us == self.username.get() and pw == self.password.get():
self.conn.close()
messagebox.showinfo('登录成功', '欢迎:%s' % us)
else:
messagebox.showwarning('登录失败', '密码错误')
def register(self):
"""注册功能跳转"""
self.conn.close()
self.page.destroy()
RegisterPage(self.root)
class RegisterPage:
"""注册界面"""
def __init__(self, master=None):
self.root = master
self.root.title('账号注册')
self.root.geometry('400x250')
self.conn = sqlite3.connect('data/login.db')
self.username = StringVar()
self.password0 = StringVar() # 第一次输入密码
self.password1 = StringVar() # 第二次输入密码
self.email = StringVar()
self.page = Frame(self.root)
self.createpage()
def createpage(self):
"""界面布局"""
Label(self.page).grid(row=0)
Label(self.page, text="账号:").grid(row=1, stick=W, pady=10)
Entry(self.page, textvariable=self.username).grid(row=1, column=1, stick=E)
Label(self.page, text="密码:").grid(row=2, stick=W, pady=10)
Entry(self.page, textvariable=self.password0, show='*').grid(row=2, column=1, stick=E)
Label(self.page, text="再次输入:").grid(row=3, stick=W, pady=10)
Entry(self.page, textvariable=self.password1, show='*').grid(row=3, column=1, stick=E)
Label(self.page, text="Email*:").grid(row=4, stick=W, pady=10)
Entry(self.page, textvariable=self.email).grid(row=4, column=1, stick=E)
Button(self.page, text="返回", command=self.repage).grid(row=5, stick=W, pady=10)
Button(self.page, text="注册", command=self.register).grid(row=5, column=1, stick=E)
self.page.pack()
def repage(self):
"""返回登录界面"""
self.page.destroy()
self.conn.close()
LoginPage(self.root)
def register(self):
"""注册"""
if self.password0.get() != self.password1.get():
messagebox.showwarning('错误', '密码核对错误')
elif len(self.username.get()) == 0 or len(self.password0.get()) == 0 or len(self.email.get()) == 0:
messagebox.showerror("错误", "不能为空")
else:
curs = self.conn.cursor()
query = 'insert into loginuser values (?,?,?,?)'
val = [self.username.get(), self.password0.get(), self.email.get(), 0]
try:
curs.execute(query, val)
self.conn.commit()
self.conn.close()
messagebox.showinfo("成功", "注册成功,按确定返回登录界面")
self.page.destroy()
LoginPage(self.root)
except sqlite3.IntegrityError:
messagebox.showerror("注册失败", "该账户已存在")
if __name__ == '__main__':
root = Tk()
LoginPage(root)
root.mainloop()
三、实现效果
1.登录界面:
2.注册界面:
来源:https://blog.csdn.net/weixin_41098458/article/details/81780701
0
投稿
猜你喜欢
- 微信支付、支付宝等第三方支付,需要和银联、网联对接,有清算机构和银行的交易处理通道成本。下文说的费率是指支付手续费的费率,在用户支付的时候,
- 1. 什么是Matplotlibmatplotlib是专门用于开发2D图表(包括3D图表),以渐进、交互式方式实现数据可视化。使用pytho
- 在机器学习或者深度学习中,我们常常碰到一个问题是数据集的切分。比如在一个比赛中,举办方给我们的只是一个带标注的训练集和不带标注的测试集。其中
- python库-密码学库pynacl什么是pynacl官方: https://pynacl.readthedocs.io/en/latest
- 是否曾经有过这样的经历:把一个元素置于另一个元素之上,而希望下面的那个元素成为可点击的?现在,利用css的pointer-events属性即
- 写入txt文件def text_save(filename, data):#filename为写入CSV文件的路径,data为要写入数据列表
- 通常用户看到的页面的样式会受到三层控制,第一层是浏览器的默认样式,第二层是网页定义样式,第三层是用户自定义样式。和CSS一样,后面的优先级高
- UDP 客户端一个使用UDP协议的客户端示例代码,用于实现连续对话。请注意,UDP是无连接协议,因此在实现连续对话时需要特别小心。以下是示例
- 请教:如何在现存的文档中动态嵌入一段JS?如果是一段CSS呢?创建一个<script></script>并在scri
- 核心代码:from random import randomfrom time import timefrom PyQt5.QtCore i
- python 获取蓝牙设备类型扫描蓝牙设备获取到的信息中,无法判断扫描到的蓝牙设备属于什么类型的设备。扫描蓝牙信息使用的是python 里面
- 今天简单使用了一下python的re模块和lxml模块,分别利用的它们提供的正则表达式和xpath来解析页面源码从中提取所需的title,x
- 本文实例为大家分享了python接入微信聊天机器人的具体代码,供大家参考,具体内容如下1.安装库wxpy:pip install -U wx
- 如何使用mailto?1)基本用法<a href=mailto:sample@163.com>send em
- Django提供了一个新的类来帮助管理分页数据,这个类存放在django/core/paginator.py.它可以接收列表、元组或其它可迭
- 酝酿了将近一个春夏秋冬的腾讯网首页终于亮剑!反响热烈!让我们来分享它成功背后的酸甜苦辣吧。腾讯网首页改版终于开花结果。于2008年3月25日
- join toString该两种方法会将数组元素的类型转换为字符串var arr = [1, [2, [3, [4, 5]]], 6];co
- 出于工作需要,学习了GAN,原理这块就不多讲了,主要讲怎么训练自己的数据生成新的图片,因为博客上大多是生成MNIST数据集,生成自己的图片时
- os 包 和 bufio 包Go 标准库的 os 包,为我们提供很多操作文件的函数,如 Open(name) 打开文件、Create(nam
- 以下函数采用FSO对象,文件位置在FSO.ASP。FSO对象的文件编码属性只有三种,系统默认,Unicode,ASCII,并没有我们要的ut