使用python语言中的Tkinter模块创建GUI应用程序开发一个拥有图形化界面的学生信息管理系统,实现基本的增加、删除、修改、查询、排序等功能并使用文件存储相关信息。把代码发给我
时间: 2023-12-20 17:04:40 浏览: 87
好的,这是一个比较复杂的项目,我可以提供一个示例代码,你可以参考一下实现自己的学生信息管理系统。
```
import tkinter as tk
import json
class Student:
def __init__(self, name, age, gender, grade):
self.name = name
self.age = age
self.gender = gender
self.grade = grade
class StudentInfoSystem:
def __init__(self):
self.students = []
self.load_data()
self.window = tk.Tk()
self.window.title("学生信息管理系统")
self.window.geometry("400x300")
self.create_widgets()
self.window.mainloop()
def load_data(self):
try:
with open("students.json", "r") as f:
self.students = json.load(f)
except:
pass
def save_data(self):
with open("students.json", "w") as f:
json.dump(self.students, f)
def create_widgets(self):
# 创建标签
tk.Label(self.window, text="姓名").grid(row=0, column=0)
tk.Label(self.window, text="年龄").grid(row=1, column=0)
tk.Label(self.window, text="性别").grid(row=2, column=0)
tk.Label(self.window, text="成绩").grid(row=3, column=0)
# 创建输入框
self.name_entry = tk.Entry(self.window)
self.name_entry.grid(row=0, column=1)
self.age_entry = tk.Entry(self.window)
self.age_entry.grid(row=1, column=1)
self.gender_entry = tk.Entry(self.window)
self.gender_entry.grid(row=2, column=1)
self.grade_entry = tk.Entry(self.window)
self.grade_entry.grid(row=3, column=1)
# 创建按钮
tk.Button(
self.window, text="添加", command=self.add_student
).grid(row=4, column=0)
tk.Button(
self.window, text="删除", command=self.delete_student
).grid(row=4, column=1)
tk.Button(
self.window, text="修改", command=self.update_student
).grid(row=4, column=2)
tk.Button(
self.window, text="查询", command=self.query_student
).grid(row=4, column=3)
tk.Button(
self.window, text="排序", command=self.sort_student
).grid(row=4, column=4)
# 创建列表框
self.listbox = tk.Listbox(self.window)
self.listbox.grid(row=5, column=0, columnspan=5)
# 显示所有学生信息
self.show_students()
def add_student(self):
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
grade = self.grade_entry.get()
student = Student(name, age, gender, grade)
self.students.append(student.__dict__)
self.show_students()
self.save_data()
def delete_student(self):
selected = self.listbox.curselection()
if selected:
student = self.students[selected[0]]
self.students.remove(student)
self.show_students()
self.save_data()
def update_student(self):
selected = self.listbox.curselection()
if selected:
student = self.students[selected[0]]
student["name"] = self.name_entry.get()
student["age"] = self.age_entry.get()
student["gender"] = self.gender_entry.get()
student["grade"] = self.grade_entry.get()
self.show_students()
self.save_data()
def query_student(self):
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
grade = self.grade_entry.get()
result = []
for student in self.students:
if (
name in student["name"]
and age in student["age"]
and gender in student["gender"]
and grade in student["grade"]
):
result.append(student)
self.students = result
self.show_students()
def sort_student(self):
self.students.sort(key=lambda x: x["grade"])
self.show_students()
def show_students(self):
self.listbox.delete(0, tk.END)
for student in self.students:
self.listbox.insert(tk.END, student["name"] + " " + student["age"] + " " + student["gender"] + " " + student["grade"])
if __name__ == "__main__":
app = StudentInfoSystem()
```
这段代码使用了Python内置的json模块来读写学生信息,将学生信息存储在一个列表中,并提供了增加、删除、修改、查询、排序等功能。你可以根据自己的需求进行修改和扩展。
阅读全文