python学生管理系统tkinter版
时间: 2023-06-10 21:07:20 浏览: 135
好的,Python学生管理系统tkinter版的开发可以分为以下几个步骤:
1. 设计界面:使用tkinter模块创建窗口和各种控件,包括标签、文本框、按钮等。
2. 编写功能:实现数据的增删改查功能,包括添加学生信息、删除学生信息、修改学生信息和查询学生信息。
3. 数据存储:将学生信息存储在文件或数据库中,以便程序下次启动时可以读取。
以下是一个简单的Python学生管理系统tkinter版的示例代码:
```python
import tkinter as tk
from tkinter import ttk
import csv
class StudentManagementSystem:
def __init__(self, window):
self.window = window
self.window.title("学生管理系统")
# 创建标签
tk.Label(self.window, text="姓名").grid(row=0, column=0)
tk.Label(self.window, text="性别").grid(row=1, column=0)
tk.Label(self.window, text="年龄").grid(row=2, column=0)
tk.Label(self.window, text="成绩").grid(row=3, column=0)
# 创建文本框
self.name_entry = tk.Entry(self.window)
self.name_entry.grid(row=0, column=1)
self.gender_entry = tk.Entry(self.window)
self.gender_entry.grid(row=1, column=1)
self.age_entry = tk.Entry(self.window)
self.age_entry.grid(row=2, column=1)
self.grade_entry = tk.Entry(self.window)
self.grade_entry.grid(row=3, column=1)
# 创建按钮
ttk.Button(self.window, text="添加", command=self.add_student).grid(row=4, column=0)
ttk.Button(self.window, text="删除", command=self.delete_student).grid(row=4, column=1)
ttk.Button(self.window, text="修改", command=self.edit_student).grid(row=4, column=2)
ttk.Button(self.window, text="查询", command=self.search_student).grid(row=4, column=3)
# 创建表格
self.tree = ttk.Treeview(self.window, columns=("姓名", "性别", "年龄", "成绩"))
self.tree.grid(row=5, column=0, columnspan=4)
self.tree.heading("#0", text="ID")
self.tree.heading("#1", text="姓名")
self.tree.heading("#2", text="性别")
self.tree.heading("#3", text="年龄")
self.tree.heading("#4", text="成绩")
# 加载数据
self.load_data()
def add_student(self):
# 获取文本框中的内容
name = self.name_entry.get()
gender = self.gender_entry.get()
age = self.age_entry.get()
grade = self.grade_entry.get()
# 将学生信息添加到表格中
self.tree.insert("", "end", values=(name, gender, age, grade))
# 将学生信息保存到文件中
with open("students.csv", "a", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow([name, gender, age, grade])
# 清空文本框
self.name_entry.delete(0, "end")
self.gender_entry.delete(0, "end")
self.age_entry.delete(0, "end")
self.grade_entry.delete(0, "end")
def delete_student(self):
# 获取选中的学生的ID
selected_item = self.tree.selection()[0]
student_id = self.tree.item(selected_item)["text"]
# 从表格中删除学生信息
self.tree.delete(selected_item)
# 从文件中删除学生信息
with open("students.csv", "r") as csvfile:
reader = csv.reader(csvfile)
rows = [row for row in reader if row[0] != student_id]
with open("students.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerows(rows)
def edit_student(self):
# 获取选中的学生的ID
selected_item = self.tree.selection()[0]
student_id = self.tree.item(selected_item)["text"]
# 获取文本框中的内容
name = self.name_entry.get()
gender = self.gender_entry.get()
age = self.age_entry.get()
grade = self.grade_entry.get()
# 更新表格中的学生信息
self.tree.item(selected_item, values=(name, gender, age, grade))
# 更新文件中的学生信息
with open("students.csv", "r") as csvfile:
reader = csv.reader(csvfile)
rows = [row if row[0] != student_id else [student_id, name, gender, age, grade] for row in reader]
with open("students.csv", "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerows(rows)
def search_student(self):
# 获取文本框中的内容
name = self.name_entry.get()
gender = self.gender_entry.get()
age = self.age_entry.get()
grade = self.grade_entry.get()
# 清空表格
for row in self.tree.get_children():
self.tree.delete(row)
# 加载符合条件的学生信息
with open("students.csv", "r") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if not(name and name != row[1]):
if not(gender and gender != row[2]):
if not(age and age != row[3]):
if not(grade and grade != row[4]):
self.tree.insert("", "end", text=row[0], values=row[1:])
def load_data(self):
# 清空表格
for row in self.tree.get_children():
self.tree.delete(row)
# 加载学生信息
with open("students.csv", "r") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
self.tree.insert("", "end", text=row[0], values=row[1:])
# 创建窗口并运行程序
window = tk.Tk()
app = StudentManagementSystem(window)
window.mainloop()
```
在这个示例中,我们使用了tkinter模块创建了一个窗口和各种控件,并实现了学生信息的增删改查功能。学生信息保存在CSV文件中,每行记录包含学生的姓名、性别、年龄和成绩。通过这个示例,你可以学习到如何使用tkinter模块开发GUI应用程序和如何处理文件读写等操作。
阅读全文