python学生信息管理系统tkinter代码,需要添加,删除,查看,修改 ,以及查看所以学生信息
时间: 2024-03-10 17:43:37 浏览: 71
以下是一个基本的Python学生信息管理系统的Tkinter代码,包含添加、删除、查看、修改和查看全部学生信息的功能:
```python
import tkinter as tk
from tkinter import messagebox
class Student:
def __init__(self, name, age, gender, major):
self.name = name
self.age = age
self.gender = gender
self.major = major
class StudentManagementSystem:
def __init__(self):
self.students = []
self.window = tk.Tk()
self.window.title("学生信息管理系统")
self.name_label = tk.Label(self.window, text="姓名")
self.name_label.grid(row=0, column=0)
self.name_entry = tk.Entry(self.window)
self.name_entry.grid(row=0, column=1)
self.age_label = tk.Label(self.window, text="年龄")
self.age_label.grid(row=1, column=0)
self.age_entry = tk.Entry(self.window)
self.age_entry.grid(row=1, column=1)
self.gender_label = tk.Label(self.window, text="性别")
self.gender_label.grid(row=2, column=0)
self.gender_entry = tk.Entry(self.window)
self.gender_entry.grid(row=2, column=1)
self.major_label = tk.Label(self.window, text="专业")
self.major_label.grid(row=3, column=0)
self.major_entry = tk.Entry(self.window)
self.major_entry.grid(row=3, column=1)
self.add_button = tk.Button(self.window, text="添加", command=self.add_student)
self.add_button.grid(row=4, column=0)
self.delete_button = tk.Button(self.window, text="删除", command=self.delete_student)
self.delete_button.grid(row=4, column=1)
self.view_button = tk.Button(self.window, text="查看", command=self.view_student)
self.view_button.grid(row=4, column=2)
self.modify_button = tk.Button(self.window, text="修改", command=self.modify_student)
self.modify_button.grid(row=4, column=3)
self.view_all_button = tk.Button(self.window, text="查看全部", command=self.view_all_students)
self.view_all_button.grid(row=5, column=0)
self.quit_button = tk.Button(self.window, text="退出", command=self.window.quit)
self.quit_button.grid(row=5, column=1)
self.window.mainloop()
def add_student(self):
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
major = self.major_entry.get()
if name and age and gender and major:
student = Student(name, age, gender, major)
self.students.append(student)
messagebox.showinfo("添加成功", "学生信息添加成功")
else:
messagebox.showerror("添加失败", "请填写完整的学生信息")
self.clear_entry()
def delete_student(self):
name = self.name_entry.get()
if name:
for student in self.students:
if student.name == name:
self.students.remove(student)
messagebox.showinfo("删除成功", "学生信息删除成功")
break
else:
messagebox.showerror("删除失败", "未找到该学生信息")
else:
messagebox.showerror("删除失败", "请输入学生姓名")
self.clear_entry()
def view_student(self):
name = self.name_entry.get()
if name:
for student in self.students:
if student.name == name:
messagebox.showinfo("学生信息", f"姓名:{student.name}\n年龄:{student.age}\n性别:{student.gender}\n专业:{student.major}")
break
else:
messagebox.showerror("查看失败", "未找到该学生信息")
else:
messagebox.showerror("查看失败", "请输入学生姓名")
self.clear_entry()
def modify_student(self):
name = self.name_entry.get()
if name:
for student in self.students:
if student.name == name:
student.age = self.age_entry.get()
student.gender = self.gender_entry.get()
student.major = self.major_entry.get()
messagebox.showinfo("修改成功", "学生信息修改成功")
break
else:
messagebox.showerror("修改失败", "未找到该学生信息")
else:
messagebox.showerror("修改失败", "请输入学生姓名")
self.clear_entry()
def view_all_students(self):
if self.students:
message = ""
for student in self.students:
message += f"姓名:{student.name}\n年龄:{student.age}\n性别:{student.gender}\n专业:{student.major}\n\n"
messagebox.showinfo("所有学生信息", message)
else:
messagebox.showerror("查看失败", "没有学生信息")
self.clear_entry()
def clear_entry(self):
self.name_entry.delete(0, tk.END)
self.age_entry.delete(0, tk.END)
self.gender_entry.delete(0, tk.END)
self.major_entry.delete(0, tk.END)
if __name__ == "__main__":
sms = StudentManagementSystem()
```
这段代码创建了一个名为`StudentManagementSystem`的类,其中包含了添加、删除、查看、修改和查看全部学生信息的方法。在Tkinter窗口中,用户可以输入学生的姓名、年龄、性别和专业,然后通过单击按钮执行相应的操作。在这个例子中,学生信息是存储在一个列表中的`Student`对象中,每个对象包含了学生的姓名、年龄、性别和专业。
阅读全文