编制一个有 GUI 界面的 Python 成绩记录簿,每个学生信息包括:学号,姓名,Python 成绩。具体功能如下: (1)输入学生信息并保存到文件中 (2)读取文件并显示输出所有学生的成绩; (3)按学号或姓名查询成绩; (4)添加成绩记录; (5)修改指定姓名或学号的学生的成绩并可存盘; (6)显示输出 60 分以下,60-79,80-89,90 分以上各分数段的学生信息. 分组要求:2 ~ 2人 (学生提交作品时需要关联同组成员,组内成员作品共享) 基于项目实施:否(学生必须在本平台创建项目,项目管理员可以提交作品)
时间: 2024-04-03 13:33:42 浏览: 151
好的,我可以帮你回答这个问题。这是一个编写 Python 成绩记录簿的问题,需要实现 GUI 界面和以下功能:
1. 输入学生信息并保存到文件中
2. 读取文件并显示输出所有学生的成绩
3. 按学号或姓名查询成绩
4. 添加成绩记录
5. 修改指定姓名或学号的学生的成绩并可存盘
6. 显示输出 60 分以下,60-79,80-89,90 分以上各分数段的学生信息。
对于这个问题,可以使用 Python 的 tkinter 模块来实现 GUI 界面,使用文件读写操作来保存和读取学生信息,使用列表和字典来存储学生信息和成绩记录,并根据输入的查询条件来查找和修改学生信息和成绩记录。以下是可能的解决方案:
```python
import tkinter as tk
import tkinter.messagebox as messagebox
import os
class StudentRecord:
def __init__(self, id, name, score):
self.id = id
self.name = name
self.score = score
class StudentRecordBook:
def __init__(self):
self.records = []
def add_record(self, record):
self.records.append(record)
def remove_record(self, id):
for record in self.records:
if record.id == id:
self.records.remove(record)
return True
return False
def update_record(self, id, name, score):
for record in self.records:
if record.id == id:
record.name = name
record.score = score
return True
return False
def find_record_by_id(self, id):
for record in self.records:
if record.id == id:
return record
return None
def find_record_by_name(self, name):
for record in self.records:
if record.name == name:
return record
return None
def filter_records_by_score(self, score_range):
filtered_records = []
for record in self.records:
if score_range == 'below60' and record.score < 60:
filtered_records.append(record)
elif score_range == '60to79' and record.score >= 60 and record.score <= 79:
filtered_records.append(record)
elif score_range == '80to89' and record.score >= 80 and record.score <= 89:
filtered_records.append(record)
elif score_range == 'above90' and record.score >= 90:
filtered_records.append(record)
return filtered_records
def load_from_file(self, filename):
self.records = []
if os.path.exists(filename):
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
fields = line.strip().split(',')
id = fields[0]
name = fields[1]
score = int(fields[2])
record = StudentRecord(id, name, score)
self.add_record(record)
def save_to_file(self, filename):
with open(filename, 'w') as f:
for record in self.records:
f.write('{},{},{}\n'.format(record.id, record.name, record.score))
class GUI:
def __init__(self, master):
self.master = master
self.master.title('Python 成绩记录簿')
self.master.geometry('600x400')
self.book = StudentRecordBook()
self.book.load_from_file('students.txt')
self.id_label = tk.Label(self.master, text='学号:')
self.id_label.grid(row=0, column=0)
self.id_entry = tk.Entry(self.master)
self.id_entry.grid(row=0, column=1)
self.name_label = tk.Label(self.master, text='姓名:')
self.name_label.grid(row=1, column=0)
self.name_entry = tk.Entry(self.master)
self.name_entry.grid(row=1, column=1)
self.score_label = tk.Label(self.master, text='成绩:')
self.score_label.grid(row=2, column=0)
self.score_entry = tk.Entry(self.master)
self.score_entry.grid(row=2, column=1)
self.add_button = tk.Button(self.master, text='添加', command=self.add_record)
self.add_button.grid(row=3, column=0)
self.remove_button = tk.Button(self.master, text='删除', command=self.remove_record)
self.remove_button.grid(row=3, column=1)
self.update_button = tk.Button(self.master, text='修改', command=self.update_record)
self.update_button.grid(row=3, column=2)
self.search_button = tk.Button(self.master, text='查询', command=self.search_record)
self.search_button.grid(row=4, column=0)
self.filter_button = tk.Button(self.master, text='筛选', command=self.filter_records)
self.filter_button.grid(row=4, column=1)
self.listbox = tk.Listbox(self.master)
self.listbox.grid(row=5, column=0, columnspan=3)
self.load_records()
def load_records(self):
self.listbox.delete(0, tk.END)
for record in self.book.records:
self.listbox.insert(tk.END, '{}\t{}\t{}'.format(record.id, record.name, record.score))
def add_record(self):
id = self.id_entry.get()
name = self.name_entry.get()
score = int(self.score_entry.get())
record = StudentRecord(id, name, score)
self.book.add_record(record)
self.book.save_to_file('students.txt')
self.load_records()
def remove_record(self):
id = self.id_entry.get()
if self.book.remove_record(id):
self.book.save_to_file('students.txt')
self.load_records()
else:
messagebox.showerror('错误', '没有找到学号为 {} 的学生记录'.format(id))
def update_record(self):
id = self.id_entry.get()
name = self.name_entry.get()
score = int(self.score_entry.get())
if self.book.update_record(id, name, score):
self.book.save_to_file('students.txt')
self.load_records()
else:
messagebox.showerror('错误', '没有找到学号为 {} 的学生记录'.format(id))
def search_record(self):
id = self.id_entry.get()
name = self.name_entry.get()
if id:
record = self.book.find_record_by_id(id)
if record:
self.name_entry.delete(0, tk.END)
self.name_entry.insert(0, record.name)
self.score_entry.delete(0, tk.END)
self.score_entry.insert(0, record.score)
else:
messagebox.showerror('错误', '没有找到学号为 {} 的学生记录'.format(id))
elif name:
record = self.book.find_record_by_name(name)
if record:
self.id_entry.delete(0, tk.END)
self.id_entry.insert(0, record.id)
self.score_entry.delete(0, tk.END)
self.score_entry.insert(0, record.score)
else:
messagebox.showerror('错误', '没有找到姓名为 {} 的学生记录'.format(name))
else:
messagebox.showerror('错误', '请输入学号或姓名')
def filter_records(self):
below60 = self.book.filter_records_by_score('below60')
between60and79 = self.book.filter_records_by_score('60to79')
between80and89 = self.book.filter_records_by_score('80to89')
above90 = self.book.filter_records_by_score('above90')
messagebox.showinfo('60 分以下', ', '.join([record.name for record in below60]))
messagebox.showinfo('60-79 分', ', '.join([record.name for record in between60and79]))
messagebox.showinfo('80-89 分', ', '.join([record.name for record in between80and89]))
messagebox.showinfo('90 分以上', ', '.join([record.name for record in above90]))
if __name__ == '__main__':
root = tk.Tk()
gui = GUI(root)
root.mainloop()
```
在这个解决方案中,我们定义了一个 StudentRecord 类来表示学生记录,包括学号、姓名和成绩,以及一个 StudentRecordBook 类来表示学生记录簿,包括添加、删除、修改、查询和筛选学生记录的方法,以及从文件中加载和保存学生记录的方法。我们还定义了一个 GUI 类来实现 tkinter 界面,包括输入学生信息、添加、删除、修改、查询和筛选学生记录的按钮,以及显示学生记录的 Listbox 组件。在程序运行时,我们首先从文件中加载学生记录,然后根据用户的操作来更新学生记录簿和界面。
阅读全文