Python + Tkinter连接本地MySQL数据库简单实现注册登录

yizhihongxing

Python + Tkinter 连接本地 MySQL 数据库简单实现注册登录的步骤如下:

1.安装必要的软件
在本地计算机上安装 MySQL 数据库,并安装 Python 包管理器 pip。

2.使用pip安装需要的包
打开终端或命令行窗口,使用 pip 安装以下必要的 Python 包:

  • mysql-connector-python:用于连接 MySQL 数据库
  • bcrypt:用于密码的哈希和字符串的比较
  • tkinter:用于创建简单的 GUI 界面

安装方法为:在终端或命令行窗口中使用以下命令安装:

pip install mysql-connector-python bcrypt tkinter

3.创建数据库和表格
使用 MySQL Workbench 创建名为 "users" 的数据库,并在其中创建 "accounts" 表格,用于存储用户的用户名和哈希密码。

表格应该有以下两个列:

  • username varchar(255) PRIMARY KEY
  • password varchar(255)

密码列应该使用 VARCHAR 255 数据类型,以便存储哈希密码。

4.编写 Python 代码
使用 Python 编写代码,连接到本地 MySQL 数据库,并创建 Tkinter 窗口,实现注册和登录功能。

示例代码 1:注册功能

from tkinter import *
import mysql.connector
import bcrypt

# 创建Tkinter窗口
root = Tk()
root.title("Registration Form")

# 连接到MySQL数据库
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="password",
  database="users"
)

# 创建账户表
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE IF NOT EXISTS accounts (username VARCHAR(255) PRIMARY KEY, password VARCHAR(255))")

# 处理表单提交
def submit():
  # 获取表单提交数据
  username = username_entry.get()
  password = password_entry.get()

  # 将密码哈希
  hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())

  # 将用户名和哈希密码插入到账户表
  sql = "INSERT INTO accounts (username, password) VALUES (%s, %s)"
  val = (username, hashed_password)
  mycursor.execute(sql, val)
  mydb.commit()

  # 显示注册成功信息
  message_label.config(text="Registration Success")

# 创建表单元素
username_label = Label(root, text="Username:")
username_label.grid(row=0, column=0)

username_entry = Entry(root)
username_entry.grid(row=0, column=1)

password_label = Label(root, text="Password:")
password_label.grid(row=1, column=0)

password_entry = Entry(root, show="*")
password_entry.grid(row=1, column=1)

submit_button = Button(root, text="Submit", command=submit)
submit_button.grid(row=2, column=1)

message_label = Label(root, text="")
message_label.grid(row=3, columnspan=2)

root.mainloop()

示例代码 2:登录功能

from tkinter import *
import mysql.connector
import bcrypt

# 创建Tkinter窗口
root = Tk()
root.title("Login Form")

# 连接到MySQL数据库
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="password",
  database="users"
)

# 处理表单提交
def submit():
  # 获取表单提交数据
  username = username_entry.get()
  password = password_entry.get()

  # 查询数据库中存储的哈希密码
  mycursor = mydb.cursor()
  sql = "SELECT password FROM accounts WHERE username = %s"
  val = (username,)
  mycursor.execute(sql, val)
  result = mycursor.fetchone()

  # 如果用户存在且密码正确
  if result is not None and bcrypt.checkpw(password.encode('utf-8'), result[0].encode('utf-8')):
    message_label.config(text="Login Success")
  else:
    message_label.config(text="Login Failed")

# 创建表单元素
username_label = Label(root, text="Username:")
username_label.grid(row=0, column=0)

username_entry = Entry(root)
username_entry.grid(row=0, column=1)

password_label = Label(root, text="Password:")
password_label.grid(row=1, column=0)

password_entry = Entry(root, show="*")
password_entry.grid(row=1, column=1)

submit_button = Button(root, text="Submit", command=submit)
submit_button.grid(row=2, column=1)

message_label = Label(root, text="")
message_label.grid(row=3, columnspan=2)

root.mainloop()

以上是 Python + Tkinter 连接本地 MySQL 数据库简单实现注册登录的详细攻略,如有不清楚之处可以随时提问。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python + Tkinter连接本地MySQL数据库简单实现注册登录 - Python技术站

(0)
上一篇 2023年6月13日
下一篇 2023年6月13日

相关文章

  • 简单介绍Python中的JSON使用

    下面我将详细讲解如何在Python中使用JSON,分以下几个方面进行介绍: JSON简介 使用Python中的JSON模块 示例说明 总结 1. JSON简介 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于阅读和编写。它通过键值对的方式表示数据,使用大括号包含对象,使用方括号包含数组。 下面是一个简单的JSO…

    python 2023年6月2日
    00
  • python的dict,set,list,tuple应用详解

    Python的dict, set, list, tuple应用详解 在Python中,dict, set, list, tuple是常用的数据结构,本文将详细讲解它们的使用方法,并提供两个示例说明。 dict 是Python中的字典,它可以存储键值对(key-value pair)。我们可以使用花括号({})或者dict()函数来创建一个字。例如,下面的代码…

    python 2023年5月13日
    00
  • Python生成8位随机字符串的方法分析

    Python生成8位随机字符串的方法分析 在Python中,我们可以通过多种方式生成随机字符串。但是,我们需要生成特定长度的随机字符串时,也需要了解不同方法的优缺点。在本文中,我们将讲解Python生成8位随机字符串的方法分析。 方法一:使用Python内置的secrets库 import secrets import string alphabet = s…

    python 2023年5月20日
    00
  • python学习笔记:字典的使用示例详解

    Python学习笔记:字典的使用示例详解 本文介绍了Python字典的使用方法,包括字典的创建、添加、更新、删除、遍历、排序等操作。同时还给出了两个字典使用的具体例子。 创建字典 在Python中,字典的创建使用{}或者dict()即可。 # 使用{}创建字典 dict1 = {‘name’: ‘Tom’, ‘age’: 23, ‘gender’: ‘mal…

    python 2023年6月3日
    00
  • VBA中操作Excel常用方法总结

    VBA中操作Excel常用方法总结 一、引用Excel对象 在VBA中,操作Excel前需要引用Excel对象。在代码中添加以下引用: Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Set xlApp = New Excel…

    python 2023年5月13日
    00
  • python映射列表实例分析

    下面我将详细讲解“Python映射列表实例分析”的攻略。 1. 映射类型 映射类型是Python的一类数据类型,它是一种键值对(key-value)结构的数据类型,它将键映射到值。Python中常用的映射类型有字典(dict)和集合(set),其中字典是最常用的映射类型之一。 2. Python列表类型 Python列表(list)是一种有序的数据结构,它可…

    python 2023年6月6日
    00
  • 使用Python的Flask框架来搭建第一个Web应用程序

    使用Python的Flask框架搭建Web应用程序,一般需要完成以下步骤: 1. 安装Flask 使用pip安装Flask,可以使用以下命令: pip install Flask 2. 编写Flask应用程序 在Python文件中编写Flask应用程序,在其中设定路由和视图函数,建立与用户端的http连接。 示例如下: from flask import F…

    python 2023年5月13日
    00
  • 支持 Python Tkinter PhotoImage 文件格式吗?

    【问题标题】:Python Tkinter PhotoImage file formats supported?支持 Python Tkinter PhotoImage 文件格式吗? 【发布时间】:2023-04-02 04:45:01 【问题描述】: 我很欣赏这是一个非常新手的问题,但我只想检查 Tkinter Photoimage 类,它是否只能从文件中…

    Python开发 2023年4月8日
    00
合作推广
合作推广
分享本页
返回顶部