Python + Tkinter连接本地MySQL数据库简单实现注册登录
作者:沧海黎明 发布时间:2023-04-24 22:07:07
标签:Python,Tkinter,实现,注册,登录
项目结构:
源代码:
# -*- coding: utf-8 -*-
"""
@date: 2022/01/09 17:40
@author: Anker
@python:v3.10
"""
import tkinter as tk
import tkinter.messagebox
import pymysql
# 定义要执行的创建表的SQL语句
test_sql = """
CREATE TABLE IF NOT EXISTS user(
id INT auto_increment PRIMARY KEY,
name varchar(20) not null,
password varchar(20) not null
)ENGINE=innodb DEFAULT CHARSET=utf8;
"""
# 登录窗口
window = tk.Tk()
window.title('学生考试系统')
window.geometry('800x500')
# 登录背景图片
canvas = tk.Canvas(window, height=1920, width=1080)
login_background = tk.PhotoImage(file='./view.png')
login_image = canvas.create_image(0, 0, anchor='nw', image=login_background)
canvas.pack(side='top')
# 用户名密码标签
tk.Label(window, text='用户名:', bg='yellow').place(x=300, y=200)
tk.Label(window, text='密 码:', bg='yellow').place(x=300, y=250)
# 用户名输入框
var_user_name = tk.StringVar()
entry_user_name = tk.Entry(window, textvariable=var_user_name)
entry_user_name.place(x=370, y=200)
# 密码输入框
var_user_pwd = tk.StringVar()
entry_user_pwd = tk.Entry(window, textvariable=var_user_pwd, show='*')
entry_user_pwd.place(x=370, y=250)
# 登录函数
def user_login():
# 输入框获取用户名密码
user_name = var_user_name.get()
user_password = var_user_pwd.get()
# 连接test_sql数据库
conn = pymysql.connect(host="localhost", user="root", password="123456", database="test_sql", charset="utf8")
curs = conn.cursor()
# 执行SQL语句,创建user数据表
curs.execute(test_sql)
# 执行SQL语句,从user数据表中查询name和password字段值
curs.execute("SELECT name,password FROM user")
# 将数据库查询的结果保存在result中
result = curs.fetchall()
# fetchone()函数它的返回值是单个的元组, 也就是一行记录, 如果没有结果, 那就会返回null
# fetchall()函数它的返回值是多个元组, 即返回多个行记录, 如果没有结果, 返回的是()
# assert result, "数据库无该用户信息" # 添加断言,判断数据库有无该用户信息,没有就直接断言错误
# 登录账号操作
name_list = [it[0] for it in result] # 从数据库查询的result中遍历查询元组中第一个元素name
# 判断用户名或密码不能为空
if not(user_name and user_password):
tk.messagebox.showwarning(title='警告', message='用户名或密码不能为空')
# 判断用户名和密码是否匹配
elif user_name in name_list:
if user_password == result[name_list.index(user_name)][1]:
tk.messagebox.showinfo(title='欢迎您', message=' 登录成功!\r\n当前登录账号为:' + user_name)
selection()
else:
tk.messagebox.showerror(title='错误', message='密码输入错误')
# 账号不在数据库中,则弹出是否注册的框
else:
is_signup = tk.messagebox.askyesno(title='提示', message='该账号不存在,是否现在注册?')
if is_signup:
user_register()
# 注册函数
def user_register():
# 确认注册函数
def register_confirm():
# 获取输入框内的内容
name = new_name.get()
password = new_password.get()
password_confirm = new_password_confirm.get()
# 先在本地手动创建一个test_sql数据库,然后连接该数据库
conn = pymysql.connect(host="localhost", user="root", password="123456", database="test_sql", charset="utf8")
curs = conn.cursor()
# 注册账号操作
try:
# 执行SQL语句,创建user数据表
curs.execute(test_sql)
# 向user数据表中插入语句
insert_sql = "INSERT INTO user(name, password) VALUES ('%s', '%s')" % (name, password)
# 读取user数据表中的name和password字段值
read_sql = f'''select * from user where name = "{name}" and password = "{password}" '''
user_data = curs.execute(read_sql)
# 判断注册账号和密码
if not (name and password):
tk.messagebox.showwarning(title='警告', message='注册账号或密码不能为空')
elif password != password_confirm:
tk.messagebox.showwarning(title='警告', message='两次密码输入不一致,请重新输入')
else:
if user_data.real:
tk.messagebox.showwarning(title='警告', message='该注册账号已存在')
else:
curs.execute(insert_sql)
tk.messagebox.showinfo(title='恭喜您', message=' 注册成功!\r\n注册账号为:' + name)
print("数据插入成功")
# 提交到数据库执行
conn.commit()
curs.close()
except IOError:
print("数据插入失败")
conn.rollback()
# 关闭数据库连接
conn.close()
window_sign_up.destroy()
# 注册窗口
window_sign_up = tk.Toplevel(window)
window_sign_up.geometry('350x200')
window_sign_up.title('欢迎注册')
# 注册账号及标签、输入框
new_name = tk.StringVar()
tk.Label(window_sign_up, bg='green', text='注册账号:').place(x=50, y=10)
tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)
# 注册密码及标签、输入框
new_password = tk.StringVar()
tk.Label(window_sign_up, bg='green', text='密 码:').place(x=50, y=50)
tk.Entry(window_sign_up, textvariable=new_password, show='*').place(x=150, y=50)
# 重复密码及标签、输入框
new_password_confirm = tk.StringVar()
tk.Label(window_sign_up, bg='green', text='确认密码:').place(x=50, y=90)
tk.Entry(window_sign_up, textvariable=new_password_confirm, show='*').place(x=150, y=90)
# 确认注册按钮及位置
bt_confirm_sign_up = tk.Button(window_sign_up, bg='green', text='确认注册', command=register_confirm)
bt_confirm_sign_up.place(x=150, y=130)
# 选择题函数
def selection():
def wrong():
tk.messagebox.showerror(title='错误', message='抱歉,您答错了')
def right():
tk.messagebox.showinfo(title='提示', message='恭喜您,答对了')
# 选择题窗口
window_options = tk.Toplevel(window)
window_options.geometry('350x200')
window_options.title('选择题')
# 在图形界面上创建一个标签label用以显示并放置
var = tk.StringVar() # 定义一个var用来将radiobutton的值和Label的值联系在一起.
lab = tk.Label(window_options, bg='red', fg='white', width=50)
lab.pack()
lab.config(text='第1题:两个锐角均为60度的三角形是什么三角形()' + var.get())
# 创建3个radiobutton选项,其中variable=var, value='A'表示:当鼠标选中其中一个选项,把value的值A放到变量var中,然后赋值给variable
radio1 = tk.Radiobutton(window_options, text='A:锐角三角形', variable=var, value='A', command=wrong)
radio1.pack()
radio2 = tk.Radiobutton(window_options, text='B:钝角三角形', variable=var, value='B', command=wrong)
radio2.pack()
radio3 = tk.Radiobutton(window_options, text='C:等边三角形', variable=var, value='C', command=right)
radio3.pack()
radio4 = tk.Radiobutton(window_options, text='D:直角三角形', variable=var, value='D', command=wrong)
radio4.pack()
# 注册和登录按钮
bt_register = tk.Button(window, bg='yellow', text='注册', command=user_register)
bt_register.place(x=380, y=300)
bt_login = tk.Button(window, bg='yellow', text='登录', command=user_login)
bt_login.place(x=440, y=300)
# 主循环
window.mainloop()
来源:https://blog.csdn.net/weixin_43184774/article/details/122396401


猜你喜欢
- 之前一直用python自带的IDLE写python程序,后来发现有一些限制啥的,于是下载了pycharm作为IDE去处理python新建项目
- 之前有看过一个博文写的是白社会的设计很好但运营却有些遭,因为对每一个WebGame的推出时间把握不准,会有几个应用同时上线造成影响力的冲突,
- 使用了这么就pip命令,但是一直是简单使用,很多命令都是用了查,查了用,今天把常用的命令汇总一下,方便使用。命令:pip由上图可以看到 pi
- sysbench是一个模块化的、跨平台、多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况。关于这个项目的详细介绍请看:
- 如何获取指定的标签的内容是解析网页爬取数据的必要手段,比如想获取<div class='xxx'> ...<
- 在升级批处理sql脚本的时候,由于各种编写的不规范、不可重复执行,我们通常希望在sql脚本出错后不中止,而是执行完成。虽然这些问题可通过编写
- 一、语法错误异常:大多数的异常都不会被程序处理,都以错误信息的形式展现在这里二、异常处理while True: t
- 本文实例讲述了Go语言实现定时器的方法。分享给大家供大家参考。具体实现方法如下:package mainimport ( &quo
- 有些项目可能涉及到使用多个数据库的情况,方法很简单。1.在settings中设定DATABASE比如要使用两个数据库:DATABASES =
- 对于许多想学习JavaScript的朋友来说,无疑如何选择入门的书籍是他们最头疼的问题,或许也是他们一直畏惧,甚至放弃学习JavaScrip
- 第一种: <script language=”javascript” type=”text/javascript”> windo
- 前言先前我们给出了遗传算法的解决方案,那么同样的我们,给出使用PSO的解决方案。其实对PSO算法比较了解的小伙伴应该是知道的,这个PSO其实
- ASP(Active Server Page)是Microsoft公司推出的基于PWS(Personal We
- 在自动化测试过程中,有时后会遇到元素定位方式没有问题,但是依旧抛出无法找到元素的异常的问题,通常情况下,如果元素定位没有问题,但还是无法找到
- 在国内,大部分人都是过农历生日,然后借助日历工具获取农历日期对应的阳历日期,以这一天来过生!这里还有一个痛点,即:每一年的农历生日对应的阳历
- 问题1:如何获取caller的(文件名,行号,函数名)?当新增一条log记录时,最终将调用Logger类的_log方法,这个方法首先会创建一
- 这个格式是我自创的,经常有人问我为什么,这里做个简单总结:1、分类,一个模块或者同类功能定义为一类定义,每类定义之间用段落隔开。2、分级,每
- 我的是shift+] 会打开search everywhere, 很影响操作。怎么关掉?解决:打开设置(settings)找到keymap,
- 有时候为了自动化测试网页,我们往往希望能够使用一些脚本语言控制浏览器. 通过脚本模拟一些浏览器动作,然后测试得到的结果.这里, 我们讲解一下
- 阿里云提供了基于命名空间的 V2 版 SDK,但是文档不是很完整,使用门槛比较高,于是我封装了一个 Composer 包:https://g