编制一个有GUI界面的Python成绩记录簿,每个学生信息包括:学号,姓名,Python成绩。具体功能如下: (1)输入学生信息并保存到文件中 (2)读取文件并显示输出所有学生的成绩; (3)按学号或姓名查询成绩 (4)添加成绩记录; (5)修改指定姓名或学号的学生的成绩并可存盘 (6)显示输出60分以下,60-79,80-89,90分以上各分数段的学生信息
时间: 2023-06-27 15:01:45 浏览: 324
好的,我可以帮你编写这个Python成绩记录簿的GUI程序。我们可以使用Tkinter模块来实现GUI界面,使用文件来保存学生信息和成绩记录。具体实现步骤如下:
1. 导入Tkinter模块和文件操作模块
```python
import tkinter as tk
from tkinter import messagebox
import os
```
2. 定义主窗口和相关控件
```python
root = tk.Tk()
root.title("Python成绩记录簿")
# 定义标签控件和输入框控件
label_id = tk.Label(root, text="学号:")
entry_id = tk.Entry(root)
label_name = tk.Label(root, text="姓名:")
entry_name = tk.Entry(root)
label_score = tk.Label(root, text="Python成绩:")
entry_score = tk.Entry(root)
# 定义按钮控件
button_add = tk.Button(root, text="添加记录", command=add_record)
button_query = tk.Button(root, text="查询记录", command=query_record)
button_modify = tk.Button(root, text="修改记录", command=modify_record)
button_show = tk.Button(root, text="显示记录", command=show_record)
button_show_stat = tk.Button(root, text="显示统计", command=show_stat)
```
3. 定义相关函数
(1)添加记录函数
```python
def add_record():
# 获取输入框内容
id = entry_id.get()
name = entry_name.get()
score = entry_score.get()
# 判断输入框是否为空
if not id or not name or not score:
messagebox.showwarning("警告", "请完整填写学生信息和成绩!")
return
# 判断成绩是否是数字
try:
score = int(score)
except ValueError:
messagebox.showwarning("警告", "成绩必须是数字!")
return
# 判断学号是否已存在
if check_id_exists(id):
messagebox.showwarning("警告", "该学号已存在,请重新输入!")
return
# 将学生信息和成绩记录保存到文件中
with open("students.txt", "a") as f:
f.write("{},{},{}\n".format(id, name, score))
# 清空输入框内容
entry_id.delete(0, tk.END)
entry_name.delete(0, tk.END)
entry_score.delete(0, tk.END)
messagebox.showinfo("提示", "添加记录成功!")
```
(2)查询记录函数
```python
def query_record():
# 获取输入框内容
id = entry_id.get()
name = entry_name.get()
# 判断输入框是否为空
if not id and not name:
messagebox.showwarning("警告", "请输入学号或姓名!")
return
# 查询学生信息和成绩记录
records = []
with open("students.txt", "r") as f:
for line in f:
fields = line.strip().split(",")
if (id and fields[0] == id) or (name and fields[1] == name):
records.append(fields)
# 显示查询结果
if len(records) == 0:
messagebox.showinfo("提示", "未查询到任何记录!")
else:
messagebox.showinfo("查询结果", "\n".join(["学号:{},姓名:{},Python成绩:{}".format(record[0], record[1], record[2]) for record in records]))
```
(3)修改记录函数
```python
def modify_record():
# 获取输入框内容
id = entry_id.get()
name = entry_name.get()
score = entry_score.get()
# 判断输入框是否为空
if not id and not name:
messagebox.showwarning("警告", "请输入学号或姓名!")
return
if not score:
messagebox.showwarning("警告", "请输入成绩!")
return
# 判断成绩是否是数字
try:
score = int(score)
except ValueError:
messagebox.showwarning("警告", "成绩必须是数字!")
return
# 查询学生信息和成绩记录
records = []
with open("students.txt", "r") as f:
for line in f:
fields = line.strip().split(",")
if (id and fields[0] == id) or (name and fields[1] == name):
records.append(fields)
# 显示查询结果并修改记录
if len(records) == 0:
messagebox.showinfo("提示", "未查询到任何记录!")
elif len(records) == 1:
# 修改成绩记录
records[0][2] = str(score)
# 保存修改后的记录到文件中
with open("students.txt", "w") as f:
for record in records:
f.write("{},{},{}\n".format(record[0], record[1], record[2]))
messagebox.showinfo("提示", "修改记录成功!")
else:
messagebox.showwarning("警告", "查询到多条记录,请输入唯一的学号或姓名!")
```
(4)显示记录函数
```python
def show_record():
# 读取所有学生信息和成绩记录
records = []
with open("students.txt", "r") as f:
for line in f:
fields = line.strip().split(",")
records.append(fields)
# 显示所有记录
if len(records) == 0:
messagebox.showinfo("提示", "未查询到任何记录!")
else:
messagebox.showinfo("所有记录", "\n".join(["学号:{},姓名:{},Python成绩:{}".format(record[0], record[1], record[2]) for record in records]))
```
(5)显示统计函数
```python
def show_stat():
# 读取所有学生信息和成绩记录
records = []
with open("students.txt", "r") as f:
for line in f:
fields = line.strip().split(",")
records.append(fields)
# 统计各分数段的学生数量
under_60, between_60_79, between_80_89, above_90 = 0, 0, 0, 0
for record in records:
if int(record[2]) < 60:
under_60 += 1
elif 60 <= int(record[2]) <= 79:
between_60_79 += 1
elif 80 <= int(record[2]) <= 89:
between_80_89 += 1
else:
above_90 += 1
# 显示统计结果
messagebox.showinfo("统计结果", "60分以下:{}人\n60-79分:{}人\n80-89分:{}人\n90分以上:{}人".format(under_60, between_60_79, between_80_89, above_90))
```
(6)检查学号是否已存在函数
```python
def check_id_exists(id):
with open("students.txt", "r") as f:
for line in f:
fields = line.strip().split(",")
if fields[0] == id:
return True
return False
```
4. 将控件添加到主窗口中
```python
label_id.grid(row=0, column=0)
entry_id.grid(row=0, column=1)
label_name.grid(row=1, column=0)
entry_name.grid(row=1, column=1)
label_score.grid(row=2, column=0)
entry_score.grid(row=2, column=1)
button_add.grid(row=3, column=0)
button_query.grid(row=3, column=1)
button_modify.grid(row=4, column=0)
button_show.grid(row=4, column=1)
button_show_stat.grid(row=5, columnspan=2)
```
5. 运行主窗口
```python
root.mainloop()
```
完整代码如下:
阅读全文