用python做学生管理系统
时间: 2023-07-18 13:12:27 浏览: 61
好的,那么我们来介绍一下Python实现学生管理系统的一般流程和步骤:
1. 设计数据库:我们需要设计一个数据库来存储学生的信息,包括姓名、学号、性别、班级、联系方式等等。
2. 连接数据库:使用Python中的MySQLdb或者其他数据库连接库,连接到我们设计好的数据库中。
3. 创建表格:在数据库中创建一个表格,用于存储学生的信息。
4. 实现功能:实现增加学生、删除学生、查询学生等基本功能,并将数据保存到数据库中。
5. 实现界面:使用Python的GUI库,如Tkinter等,实现一个用户友好的界面,方便用户进行操作。
下面是一个简单的示例代码,实现了学生管理系统的基本功能:
```python
import MySQLdb
from tkinter import *
# 连接到数据库
db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="students")
# 创建一个表格
cursor = db.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS student(name varchar(255), id int, gender varchar(10), class varchar(255), phone varchar(255))")
# 增加学生
def add_student():
name = name_entry.get()
id = id_entry.get()
gender = gender_entry.get()
class_ = class_entry.get()
phone = phone_entry.get()
cursor.execute("INSERT INTO student(name, id, gender, class, phone) VALUES (%s, %s, %s, %s, %s)", (name, id, gender, class_, phone))
db.commit()
name_entry.delete(0, END)
id_entry.delete(0, END)
gender_entry.delete(0, END)
class_entry.delete(0, END)
phone_entry.delete(0, END)
# 删除学生
def delete_student():
id = id_entry.get()
cursor.execute("DELETE FROM student WHERE id = %s", (id,))
db.commit()
id_entry.delete(0, END)
# 查询学生
def query_student():
id = id_entry.get()
cursor.execute("SELECT * FROM student WHERE id = %s", (id,))
results = cursor.fetchall()
for row in results:
name = row[0]
gender = row[2]
class_ = row[3]
phone = row[4]
result_label.config(text="姓名:%s\n性别:%s\n班级:%s\n联系方式:%s" % (name, gender, class_, phone))
# 创建界面
root = Tk()
root.title("学生管理系统")
root.geometry("400x300")
name_label = Label(root, text="姓名")
name_label.pack()
name_entry = Entry(root)
name_entry.pack()
id_label = Label(root, text="学号")
id_label.pack()
id_entry = Entry(root)
id_entry.pack()
gender_label = Label(root, text="性别")
gender_label.pack()
gender_entry = Entry(root)
gender_entry.pack()
class_label = Label(root, text="班级")
class_label.pack()
class_entry = Entry(root)
class_entry.pack()
phone_label = Label(root, text="联系方式")
phone_label.pack()
phone_entry = Entry(root)
phone_entry.pack()
add_button = Button(root, text="增加学生", command=add_student)
add_button.pack()
delete_button = Button(root, text="删除学生", command=delete_student)
delete_button.pack()
query_button = Button(root, text="查询学生", command=query_student)
query_button.pack()
result_label = Label(root, text="")
result_label.pack()
root.mainloop()
# 关闭数据库连接
db.close()
```
以上是一个简单的Python实现学生管理系统的示例代码,你可以根据自己的需求进行修改和扩展。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)