用python和tkinter库编写一个学生管理系统,实现增删查改
时间: 2024-03-24 16:41:16 浏览: 61
好的,我可以帮你编写一个基础的学生管理系统,使用Python和Tkinter库实现增删查改功能。下面是代码实现:
```python
import tkinter as tk
class Student:
def __init__(self, name, age, gender, score):
self.name = name
self.age = age
self.gender = gender
self.score = score
class StudentManagementSystem:
def __init__(self, master):
self.master = master
self.master.title("学生管理系统")
# 创建列表框
self.listbox = tk.Listbox(self.master, width=50)
self.listbox.grid(row=0, column=0, rowspan=6)
# 创建标签和输入框
tk.Label(self.master, text="姓名").grid(row=0, column=1)
self.name_entry = tk.Entry(self.master)
self.name_entry.grid(row=0, column=2)
tk.Label(self.master, text="年龄").grid(row=1, column=1)
self.age_entry = tk.Entry(self.master)
self.age_entry.grid(row=1, column=2)
tk.Label(self.master, text="性别").grid(row=2, column=1)
self.gender_entry = tk.Entry(self.master)
self.gender_entry.grid(row=2, column=2)
tk.Label(self.master, text="成绩").grid(row=3, column=1)
self.score_entry = tk.Entry(self.master)
self.score_entry.grid(row=3, column=2)
# 创建按钮
tk.Button(self.master, text="添加", command=self.add_student).grid(row=4, column=1)
tk.Button(self.master, text="删除", command=self.delete_student).grid(row=4, column=2)
tk.Button(self.master, text="修改", command=self.modify_student).grid(row=5, column=1)
tk.Button(self.master, text="查询", command=self.search_student).grid(row=5, column=2)
# 创建学生列表
self.students = []
# 添加学生
def add_student(self):
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
score = self.score_entry.get()
student = Student(name, age, gender, score)
self.students.append(student)
self.listbox.insert(tk.END, f"{name} {age} {gender} {score}")
self.name_entry.delete(0, tk.END)
self.age_entry.delete(0, tk.END)
self.gender_entry.delete(0, tk.END)
self.score_entry.delete(0, tk.END)
# 删除学生
def delete_student(self):
selection = self.listbox.curselection()
if selection:
index = selection[0]
self.listbox.delete(index)
self.students.pop(index)
# 修改学生
def modify_student(self):
selection = self.listbox.curselection()
if selection:
index = selection[0]
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
score = self.score_entry.get()
self.students[index].name = name
self.students[index].age = age
self.students[index].gender = gender
self.students[index].score = score
self.listbox.delete(index)
self.listbox.insert(index, f"{name} {age} {gender} {score}")
# 查询学生
def search_student(self):
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
score = self.score_entry.get()
for i, student in enumerate(self.students):
if (not name or student.name == name) and \
(not age or student.age == age) and \
(not gender or student.gender == gender) and \
(not score or student.score == score):
self.listbox.selection_clear(0, tk.END)
self.listbox.selection_set(i)
self.listbox.activate(i)
break
if __name__ == '__main__':
root = tk.Tk()
app = StudentManagementSystem(root)
root.mainloop()
```
使用方法:
1. 运行代码,打开学生管理系统窗口。
2. 在输入框中输入学生的姓名、年龄、性别和成绩,并点击“添加”按钮,即可添加学生信息到列表框中。
3. 选中列表框中的学生信息,点击“删除”按钮,即可删除该学生信息。
4. 选中列表框中的学生信息,修改输入框中的信息,点击“修改”按钮,即可修改该学生信息。
5. 在输入框中输入要查询的学生信息,点击“查询”按钮,即可选中符合条件的学生信息。
阅读全文