python连接数据库图形化形成一个学生信息管理系统
时间: 2023-07-20 17:45:24 浏览: 101
好的,我可以帮你解答这个问题。首先,你需要安装一个 Python 的数据库驱动,比如 MySQLdb 或者 PyMySQL。然后,你需要在你的数据库中创建一个学生信息表,包含学生的姓名、性别、年龄、班级等字段。
接下来,你可以使用 Python 的 tkinter 模块来创建一个图形化界面,包含添加学生、修改学生、删除学生、查询学生等功能按钮。在这些按钮被点击时,你需要编写相应的函数来与数据库进行交互,比如插入一条新的学生信息、更新一条学生信息、删除一条学生信息、查询学生信息等。
以下是一个简单的示例代码,仅供参考:
```python
import tkinter as tk
import pymysql
# 连接数据库
conn = pymysql.connect(host='localhost', user='root', password='123456', database='test', charset='utf8')
cursor = conn.cursor()
# 创建学生信息表
cursor.execute('''CREATE TABLE IF NOT EXISTS students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(20),
gender VARCHAR(10),
age INT,
class VARCHAR(20)
)''')
# 添加学生信息
def add_student():
name = name_entry.get()
gender = gender_entry.get()
age = int(age_entry.get())
class_ = class_entry.get()
cursor.execute("INSERT INTO students (name, gender, age, class) VALUES (%s, %s, %s, %s)", (name, gender, age, class_))
conn.commit()
clear_entries()
# 修改学生信息
def update_student():
id_ = int(id_entry.get())
name = name_entry.get()
gender = gender_entry.get()
age = int(age_entry.get())
class_ = class_entry.get()
cursor.execute("UPDATE students SET name=%s, gender=%s, age=%s, class=%s WHERE id=%s", (name, gender, age, class_, id_))
conn.commit()
clear_entries()
# 删除学生信息
def delete_student():
id_ = int(id_entry.get())
cursor.execute("DELETE FROM students WHERE id=%s", (id_,))
conn.commit()
clear_entries()
# 查询学生信息
def query_student():
id_ = int(id_entry.get())
cursor.execute("SELECT * FROM students WHERE id=%s", (id_,))
result = cursor.fetchone()
if result:
name_entry.delete(0, tk.END)
name_entry.insert(tk.END, result[1])
gender_entry.delete(0, tk.END)
gender_entry.insert(tk.END, result[2])
age_entry.delete(0, tk.END)
age_entry.insert(tk.END, result[3])
class_entry.delete(0, tk.END)
class_entry.insert(tk.END, result[4])
# 清空输入框
def clear_entries():
id_entry.delete(0, tk.END)
name_entry.delete(0, tk.END)
gender_entry.delete(0, tk.END)
age_entry.delete(0, tk.END)
class_entry.delete(0, tk.END)
# 创建窗口
window = tk.Tk()
window.title('学生信息管理系统')
# 添加控件
id_label = tk.Label(window, text='ID:')
id_label.grid(row=0, column=0, padx=5, pady=5)
id_entry = tk.Entry(window)
id_entry.grid(row=0, column=1, padx=5, pady=5)
name_label = tk.Label(window, text='姓名:')
name_label.grid(row=1, column=0, padx=5, pady=5)
name_entry = tk.Entry(window)
name_entry.grid(row=1, column=1, padx=5, pady=5)
gender_label = tk.Label(window, text='性别:')
gender_label.grid(row=2, column=0, padx=5, pady=5)
gender_entry = tk.Entry(window)
gender_entry.grid(row=2, column=1, padx=5, pady=5)
age_label = tk.Label(window, text='年龄:')
age_label.grid(row=3, column=0, padx=5, pady=5)
age_entry = tk.Entry(window)
age_entry.grid(row=3, column=1, padx=5, pady=5)
class_label = tk.Label(window, text='班级:')
class_label.grid(row=4, column=0, padx=5, pady=5)
class_entry = tk.Entry(window)
class_entry.grid(row=4, column=1, padx=5, pady=5)
add_button = tk.Button(window, text='添加', command=add_student)
add_button.grid(row=5, column=0, padx=5, pady=5)
update_button = tk.Button(window, text='修改', command=update_student)
update_button.grid(row=5, column=1, padx=5, pady=5)
delete_button = tk.Button(window, text='删除', command=delete_student)
delete_button.grid(row=5, column=2, padx=5, pady=5)
query_button = tk.Button(window, text='查询', command=query_student)
query_button.grid(row=5, column=3, padx=5, pady=5)
clear_button = tk.Button(window, text='清空', command=clear_entries)
clear_button.grid(row=5, column=4, padx=5, pady=5)
# 进入消息循环
window.mainloop()
# 关闭数据库连接
cursor.close()
conn.close()
```
这段代码可以创建一个基本的学生信息管理系统,你可以根据自己的需求进行修改和扩展。
阅读全文
相关推荐















