python学生信息管理系统tkinter代码,需要添加,删除,查看,修改
时间: 2024-03-09 08:51:11 浏览: 66
学生信息管理系统(tkinter界面带登陆系统,增删改查等)-python
5星 · 资源好评率100%
下面是一个简单的学生信息管理系统的Tkinter代码,包括添加、删除、查看和修改功能。你可以根据自己的需求进行修改和完善。
```python
import tkinter as tk
class Student:
def __init__(self, name, gender, age, phone):
self.name = name
self.gender = gender
self.age = age
self.phone = phone
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def delete_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
def find_student(self, name):
for student in self.students:
if student.name == name:
return student
return None
def update_student(self, name, gender, age, phone):
student = self.find_student(name)
if student is not None:
student.gender = gender
student.age = age
student.phone = phone
class StudentManagementApp:
def __init__(self, root):
self.root = root
self.root.title("学生信息管理系统")
self.system = StudentManagementSystem()
# 添加学生组件
tk.Label(root, text="姓名").grid(row=0, column=0)
self.name_entry = tk.Entry(root)
self.name_entry.grid(row=0, column=1)
tk.Label(root, text="性别").grid(row=1, column=0)
self.gender_entry = tk.Entry(root)
self.gender_entry.grid(row=1, column=1)
tk.Label(root, text="年龄").grid(row=2, column=0)
self.age_entry = tk.Entry(root)
self.age_entry.grid(row=2, column=1)
tk.Label(root, text="电话").grid(row=3, column=0)
self.phone_entry = tk.Entry(root)
self.phone_entry.grid(row=3, column=1)
add_button = tk.Button(root, text="添加", command=self.add_student)
add_button.grid(row=4, column=0, columnspan=2)
# 删除学生组件
tk.Label(root, text="姓名").grid(row=5, column=0)
self.delete_name_entry = tk.Entry(root)
self.delete_name_entry.grid(row=5, column=1)
delete_button = tk.Button(root, text="删除", command=self.delete_student)
delete_button.grid(row=6, column=0, columnspan=2)
# 查看学生组件
view_button = tk.Button(root, text="查看", command=self.view_students)
view_button.grid(row=7, column=0, columnspan=2)
# 修改学生组件
tk.Label(root, text="姓名").grid(row=8, column=0)
self.update_name_entry = tk.Entry(root)
self.update_name_entry.grid(row=8, column=1)
tk.Label(root, text="性别").grid(row=9, column=0)
self.update_gender_entry = tk.Entry(root)
self.update_gender_entry.grid(row=9, column=1)
tk.Label(root, text="年龄").grid(row=10, column=0)
self.update_age_entry = tk.Entry(root)
self.update_age_entry.grid(row=10, column=1)
tk.Label(root, text="电话").grid(row=11, column=0)
self.update_phone_entry = tk.Entry(root)
self.update_phone_entry.grid(row=11, column=1)
update_button = tk.Button(root, text="修改", command=self.update_student)
update_button.grid(row=12, column=0, columnspan=2)
# 显示结果组件
self.result_text = tk.Text(root, width=40, height=10)
self.result_text.grid(row=13, column=0, columnspan=2)
def add_student(self):
name = self.name_entry.get()
gender = self.gender_entry.get()
age = self.age_entry.get()
phone = self.phone_entry.get()
student = Student(name, gender, age, phone)
self.system.add_student(student)
self.result_text.insert(tk.END, "添加成功\n")
def delete_student(self):
name = self.delete_name_entry.get()
self.system.delete_student(name)
self.result_text.insert(tk.END, "删除成功\n")
def view_students(self):
self.result_text.delete(1.0, tk.END)
for student in self.system.students:
self.result_text.insert(tk.END, f"姓名:{student.name}\n")
self.result_text.insert(tk.END, f"性别:{student.gender}\n")
self.result_text.insert(tk.END, f"年龄:{student.age}\n")
self.result_text.insert(tk.END, f"电话:{student.phone}\n")
self.result_text.insert(tk.END, "-" * 20 + "\n")
def update_student(self):
name = self.update_name_entry.get()
gender = self.update_gender_entry.get()
age = self.update_age_entry.get()
phone = self.update_phone_entry.get()
self.system.update_student(name, gender, age, phone)
self.result_text.insert(tk.END, "修改成功\n")
if __name__ == "__main__":
root = tk.Tk()
app = StudentManagementApp(root)
root.mainloop()
```
在这个代码中,我们定义了一个`Student`类来表示一个学生,包括姓名、性别、年龄和电话。我们还定义了一个`StudentManagementSystem`类来管理学生信息,包括添加、删除、查找和修改学生信息等功能。最后,我们使用Tkinter创建了一个学生信息管理系统的图形界面,包括添加、删除、查看和修改学生信息等功能。
阅读全文