写一个学生管理系统,要求带GUI界面
时间: 2024-10-29 07:25:07 浏览: 28
创建一个学生管理系统带GUI(图形用户界面)通常使用Python中的Tkinter库或更现代的如PyQt、wxPython或Kivy等。这里我将提供一个基本的用Tkinter编写的简单示例。这个例子包含一个窗口、添加学生信息的功能以及显示所有学生的列表。
```python
import tkinter as tk
from tkinter import ttk
class Student:
def __init__(self, id, name, grade):
self.id = id
self.name = name
self.grade = grade
class StudentManagementSystem:
def __init__(self, master):
# 初始化UI
self.master = master
self.master.title("Student Management System")
# 创建表格区域
self.scrollbar = ttk.Scrollbar(self.master)
self.students_table = ttk.Treeview(self.master, yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.students_table.yview)
# 添加表头
headings = ["ID", "Name", "Grade"]
for col in headings:
self.students_table.heading(col, text=col)
# 将表头设置为不可点击
self.students_table.column("#0", stretch=tk.NO)
for i, col in enumerate(headings):
self.students_table.column(col, anchor=tk.W, width=100)
# 添加数据
self.students_data = []
# 设置表格大小
self.students_table.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
def add_student(self, event=None):
if not self.students_data:
new_id = 1
else:
new_id = max([student.id for student in self.students_data]) + 1
new_name = input("Enter student's name: ")
new_grade = input("Enter student's grade: ")
student = Student(new_id, new_name, new_grade)
self.students_data.append(student)
self.update_students_list()
def update_students_list(self):
self.students_table.delete(*self.students_table.get_children())
for student in self.students_data:
self.students_table.insert('', 'end', values=(student.id, student.name, student.grade))
# 主函数
def main():
root = tk.Tk()
s = StudentManagementSystem(root)
add_button = tk.Button(root, text="Add Student", command=s.add_student)
add_button.pack(pady=10)
root.bind("<Return>", s.add_student) # 绑定键盘回车键
root.mainloop()
if __name__ == "__main__":
main()
```
在这个例子中,我们创建了一个`Student`类表示学生,并在`StudentManagementSystem`类中实现了添加学生到树形视图的功能。当用户点击“Add Student”按钮或按下键盘上的回车键时,就会弹出对话框让用户输入新学生的信息,然后更新表格。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![tar](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)