首先,需要安装 Python 和 MySQL,并安装 MySQL 的 Python 驱动程序。然后,可以使用 Python 中的 tkinter 库创建 GUI 界面。以下是一个简单的示例:

import tkinter as tk

# 创建窗口
root = tk.Tk()
root.title('数据管理软件')

# 创建标签
label_username = tk.Label(root, text='用户名:')
label_username.pack()
entry_username = tk.Entry(root)
entry_username.pack()

label_password = tk.Label(root, text='密码:')
label_password.pack()
entry_password = tk.Entry(root, show='*')
entry_password.pack()

# 创建按钮
button_login = tk.Button(root, text='登录')
button_login.pack()

button_register = tk.Button(root, text='注册')
button_register.pack()

# 运行窗口
root.mainloop()

接下来,可以使用 MySQL 的 Python 驱动程序来连接数据库并执行查询、插入、删除和更新操作。以下是一个简单的示例:

import mysql.connector

# 连接数据库
mydb = mysql.connector.connect(
  host='localhost',
  user='yourusername',
  password='yourpassword',
  database='mydatabase'
)

# 执行查询操作
mycursor = mydb.cursor()
mycursor.execute('SELECT * FROM customers')
myresult = mycursor.fetchall()
for x in myresult:
  print(x)

# 执行插入操作
mycursor = mydb.cursor()
sql = 'INSERT INTO customers (name, address) VALUES (%s, %s)'
val = ('John', 'Highway 21')
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, 'record inserted.')

# 执行删除操作
mycursor = mydb.cursor()
sql = 'DELETE FROM customers WHERE address = 'Mountain 21''
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, 'record(s) deleted')

# 执行更新操作
mycursor = mydb.cursor()
sql = 'UPDATE customers SET address = 'Canyon 123' WHERE address = 'Highway 21''
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, 'record(s) affected')

最后,可以将 GUI 界面和数据库操作结合起来,创建一个完整的数据管理软件。以下是一个简单的示例:

import tkinter as tk
import mysql.connector

# 连接数据库
mydb = mysql.connector.connect(
  host='localhost',
  user='yourusername',
  password='yourpassword',
  database='mydatabase'
)

# 登录界面
def login_page():
    # 销毁之前的组件
    for widget in root.winfo_children():
        widget.destroy()

    # 创建新的组件
    label_username = tk.Label(root, text='用户名:')
    label_username.pack()
entry_username = tk.Entry(root)
entry_username.pack()

    label_password = tk.Label(root, text='密码:')
    label_password.pack()
entry_password = tk.Entry(root, show='*')
entry_password.pack()

    button_login = tk.Button(root, text='登录', command=login)
    button_login.pack()

    button_register = tk.Button(root, text='注册', command=register_page)
    button_register.pack()

# 注册界面
def register_page():
    # 销毁之前的组件
    for widget in root.winfo_children():
        widget.destroy()

    # 创建新的组件
    label_username = tk.Label(root, text='用户名:')
    label_username.pack()
entry_username = tk.Entry(root)
entry_username.pack()

    label_password = tk.Label(root, text='密码:')
    label_password.pack()
entry_password = tk.Entry(root, show='*')
entry_password.pack()

    button_register = tk.Button(root, text='注册', command=register)
    button_register.pack()

    button_back = tk.Button(root, text='返回', command=login_page)
    button_back.pack()

# 登录函数
def login():
    # 获取用户名和密码
    username = entry_username.get()
    password = entry_password.get()

    # 查询数据库,验证用户名和密码
    mycursor = mydb.cursor()
    sql = 'SELECT * FROM users WHERE username = %s AND password = %s'
    val = (username, password)
    mycursor.execute(sql, val)
    myresult = mycursor.fetchone()
    if myresult:
        # 登录成功,跳转到主界面
        main_page()
    else:
        # 登录失败,显示错误信息
        tk.messagebox.showerror('错误', '用户名或密码错误')

# 注册函数
def register():
    # 获取用户名和密码
    username = entry_username.get()
    password = entry_password.get()

    # 插入数据到数据库
    mycursor = mydb.cursor()
    sql = 'INSERT INTO users (username, password) VALUES (%s, %s)'
    val = (username, password)
    mycursor.execute(sql, val)
    mydb.commit()

    # 注册成功,跳转到登录界面
    login_page()

# 主界面
def main_page():
    # 销毁之前的组件
    for widget in root.winfo_children():
        widget.destroy()

    # 创建新的组件
    label_title = tk.Label(root, text='数据管理软件')
    label_title.pack()

    button_add = tk.Button(root, text='添加数据', command=add_data_page)
    button_add.pack()

    button_delete = tk.Button(root, text='删除数据', command=delete_data_page)
    button_delete.pack()

    button_search = tk.Button(root, text='查找数据', command=search_data_page)
    button_search.pack()

    button_update = tk.Button(root, text='更新数据', command=update_data_page)
    button_update.pack()

# 添加数据界面
def add_data_page():
    # 销毁之前的组件
    for widget in root.winfo_children():
        widget.destroy()

    # 创建新的组件
    label_name = tk.Label(root, text='姓名:')
    label_name.pack()
entry_name = tk.Entry(root)
entry_name.pack()

    label_address = tk.Label(root, text='地址:')
    label_address.pack()
entry_address = tk.Entry(root)
entry_address.pack()

    button_add = tk.Button(root, text='添加', command=add_data)
    button_add.pack()

    button_back = tk.Button(root, text='返回', command=main_page)
    button_back.pack()

# 添加数据函数
def add_data():
    # 获取姓名和地址
    name = entry_name.get()
    address = entry_address.get()

    # 插入数据到数据库
    mycursor = mydb.cursor()
    sql = 'INSERT INTO customers (name, address) VALUES (%s, %s)'
    val = (name, address)
    mycursor.execute(sql, val)
    mydb.commit()

    # 添加成功,跳转到主界面
    main_page()

# 删除数据界面
def delete_data_page():
    # 销毁之前的组件
    for widget in root.winfo_children():
        widget.destroy()

    # 创建新的组件
    label_name = tk.Label(root, text='姓名:')
    label_name.pack()
entry_name = tk.Entry(root)
entry_name.pack()

    button_delete = tk.Button(root, text='删除', command=delete_data)
    button_delete.pack()

    button_back = tk.Button(root, text='返回', command=main_page)
    button_back.pack()

# 删除数据函数
def delete_data():
    # 获取姓名
    name = entry_name.get()

    # 删除数据
    mycursor = mydb.cursor()
    sql = 'DELETE FROM customers WHERE name = %s'
    val = (name,)
    mycursor.execute(sql, val)
    mydb.commit()

    # 删除成功,跳转到主界面
    main_page()

# 查找数据界面
def search_data_page():
    # 销毁之前的组件
    for widget in root.winfo_children():
        widget.destroy()

    # 创建新的组件
    label_name = tk.Label(root, text='姓名:')
    label_name.pack()
entry_name = tk.Entry(root)
entry_name.pack()

    button_search = tk.Button(root, text='查找', command=search_data)
    button_search.pack()

    button_back = tk.Button(root, text='返回', command=main_page)
    button_back.pack()

# 查找数据函数
def search_data():
    # 获取姓名
    name = entry_name.get()

    # 查询数据
    mycursor = mydb.cursor()
    sql = 'SELECT * FROM customers WHERE name = %s'
    val = (name,)
    mycursor.execute(sql, val)
    myresult = mycursor.fetchone()
    if myresult:
        # 显示查询结果
        tk.messagebox.showinfo('查询结果', f'姓名:{myresult[0]}
地址:{myresult[1]}')
    else:
        # 查询失败,显示错误信息
        tk.messagebox.showerror('错误', '未找到该姓名的数据')

# 更新数据界面
def update_data_page():
    # 销毁之前的组件
    for widget in root.winfo_children():
        widget.destroy()

    # 创建新的组件
    label_name = tk.Label(root, text='姓名:')
    label_name.pack()
entry_name = tk.Entry(root)
entry_name.pack()

    label_address = tk.Label(root, text='地址:')
    label_address.pack()
entry_address = tk.Entry(root)
entry_address.pack()

    button_update = tk.Button(root, text='更新', command=update_data)
    button_update.pack()

    button_back = tk.Button(root, text='返回', command=main_page)
    button_back.pack()

# 更新数据函数
def update_data():
    # 获取姓名和地址
    name = entry_name.get()
    address = entry_address.get()

    # 更新数据
    mycursor = mydb.cursor()
    sql = 'UPDATE customers SET address = %s WHERE name = %s'
    val = (address, name)
    mycursor.execute(sql, val)
    mydb.commit()

    # 更新成功,跳转到主界面
    main_page()

# 创建窗口
root = tk.Tk()
root.title('数据管理软件')

# 显示登录界面
login_page()

# 运行窗口
root.mainloop()

这个示例程序实现了一个简单的数据管理软件,包括登录、注册、添加数据、删除数据、查找数据和更新数据等功能。可以根据需要进行修改和扩展。

使用 Python 和 Tkinter 创建数据管理软件

原文地址: https://www.cveoy.top/t/topic/ofe4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录