编制一个有GUI界面的Python成绩记录簿,每个学生信息包括:学号,姓名,Python成绩。具体功能如下: (1)输入学生信息并保存到文件中 (2)读取文件并显示输出所有学生的成绩; (3)按学号或姓名查询成绩 (4)添加成绩记录; (5)修改指定姓名或学号的学生的成绩并可存盘 (6)显示输出60分以下,60-79,80-89,90分以上各分数段的学生信息

时间: 2023-06-27 19:01:45 浏览: 131
好的,我可以帮你编写这个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() ``` 完整代码如下:

相关推荐

最新推荐

recommend-type

Python实现GUI学生信息管理系统

主要介绍了Python实现GUI学生信息管理系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

python 实现简单的计算器(gui界面)

运行效果: 完整代码 from tkinter import * def click(num): global op op=op+str(num) iptext.set(op) def evaluate(): global op output=str(eval&#40;op&#41;) iptext.set(output) ...ipa
recommend-type

Python PyQt5模块实现窗口GUI界面代码实例

主要介绍了Python PyQt5模块实现窗口GUI界面代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

python GUI库图形界面开发之PyQt5树形结构控件QTreeWidget详细使用方法与实例

主要介绍了python GUI库图形界面开发PyQt5树形结构控件QTreeWidget详细使用方法与实例,需要的朋友可以参考下
recommend-type

python GUI库图形界面开发之PyQt5拖放控件实例详解

主要介绍了python GUI库图形界面开发之PyQt5使用拖放控件实例详解,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。