python用tkinter打开学生信息管理系统
时间: 2023-11-20 15:52:39 浏览: 168
好的,以下是一个简单的使用tkinter实现的学生信息管理系统的示例代码:
```python
import tkinter as tk
class Student:
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender
class StudentManagementSystem:
def __init__(self):
self.students = []
def add_student(self, student):
self.students.append(student)
def remove_student(self, student):
self.students.remove(student)
def search_student(self, name):
for student in self.students:
if student.name == name:
return student
return None
class GUI:
def __init__(self, system):
self.system = system
self.window = tk.Tk()
self.window.title("学生信息管理系统")
self.window.geometry("400x300")
# 创建控件
self.name_label = tk.Label(self.window, text="姓名:")
self.name_entry = tk.Entry(self.window)
self.age_label = tk.Label(self.window, text="年龄:")
self.age_entry = tk.Entry(self.window)
self.gender_label = tk.Label(self.window, text="性别:")
self.gender_entry = tk.Entry(self.window)
self.add_button = tk.Button(self.window, text="添加", command=self.add_student)
self.remove_button = tk.Button(self.window, text="删除", command=self.remove_student)
self.search_button = tk.Button(self.window, text="查找", command=self.search_student)
self.result_label = tk.Label(self.window, text="")
# 布局控件
self.name_label.grid(row=0, column=0)
self.name_entry.grid(row=0, column=1)
self.age_label.grid(row=1, column=0)
self.age_entry.grid(row=1, column=1)
self.gender_label.grid(row=2, column=0)
self.gender_entry.grid(row=2, column=1)
self.add_button.grid(row=3, column=0)
self.remove_button.grid(row=3, column=1)
self.search_button.grid(row=4, column=0)
self.result_label.grid(row=4, column=1)
def add_student(self):
name = self.name_entry.get()
age = int(self.age_entry.get())
gender = self.gender_entry.get()
student = Student(name, age, gender)
self.system.add_student(student)
self.result_label.config(text="添加成功")
def remove_student(self):
name = self.name_entry.get()
student = self.system.search_student(name)
if student:
self.system.remove_student(student)
self.result_label.config(text="删除成功")
else:
self.result_label.config(text="查无此人")
def search_student(self):
name = self.name_entry.get()
student = self.system.search_student(name)
if student:
self.result_label.config(text=f"姓名:{student.name} 年龄:{student.age} 性别:{student.gender}")
else:
self.result_label.config(text="查无此人")
def run(self):
self.window.mainloop()
if __name__ == "__main__":
system = StudentManagementSystem()
gui = GUI(system)
gui.run()
```
这个学生信息管理系统使用了tkinter库来创建GUI界面,可以通过添加、删除和查找学生来管理学生信息。你可以将这段代码保存为一个.py文件,然后在命令行中运行它来打开学生信息管理系统。
阅读全文