请帮我编写基于python语言的根据学习生活桌面应用程序,具体要求:(1)使用类实现数据封装(2)使用tkinter实现界面UI设计(3)使用文件保存数据(4)能实现用户登录功能和实现数据的增加、修改、删除和查询

时间: 2024-01-27 15:06:35 浏览: 23
好的,我会为您解答这个问题。首先,您需要了解Python中的类和Tkinter库,以及如何使用文件保存数据。接下来,我们将一步步实现这个桌面应用程序。 1. 首先,创建一个名为“Student”的类,用于封装学生的信息,包括姓名、年龄、学号和成绩等。代码如下: ```python class Student: def __init__(self, name, age, student_id, score): self.name = name self.age = age self.student_id = student_id self.score = score ``` 2. 然后,我们使用Tkinter库创建一个用户界面,包括登录界面和主界面。登录界面包括用户名和密码的输入框,以及登录按钮。主界面包括菜单栏、添加、修改、删除和查询按钮,以及一个用于显示学生信息的表格。代码如下: ```python import tkinter as tk from tkinter import ttk class Application(tk.Tk): def __init__(self): super().__init__() # 创建登录界面 self.title("登录") self.geometry("200x100") self.username_label = tk.Label(self, text="用户名") self.username_label.pack() self.username_entry = tk.Entry(self) self.username_entry.pack() self.password_label = tk.Label(self, text="密码") self.password_label.pack() self.password_entry = tk.Entry(self, show="*") self.password_entry.pack() self.login_button = tk.Button(self, text="登录", command=self.login) self.login_button.pack() # 创建主界面 self.main_menu = tk.Menu(self) self.config(menu=self.main_menu) self.file_menu = tk.Menu(self.main_menu, tearoff=False) self.main_menu.add_cascade(label="文件", menu=self.file_menu) self.add_button = tk.Button(self, text="添加", command=self.add_student) self.add_button.pack() self.modify_button = tk.Button(self, text="修改", command=self.modify_student) self.modify_button.pack() self.delete_button = tk.Button(self, text="删除", command=self.delete_student) self.delete_button.pack() self.query_button = tk.Button(self, text="查询", command=self.query_student) self.query_button.pack() self.student_table = ttk.Treeview(self, columns=("name", "age", "student_id", "score"), show="headings") self.student_table.pack() self.student_table.heading("name", text="姓名") self.student_table.heading("age", text="年龄") self.student_table.heading("student_id", text="学号") self.student_table.heading("score", text="成绩") def login(self): # 处理登录逻辑 pass def add_student(self): # 处理添加学生逻辑 pass def modify_student(self): # 处理修改学生逻辑 pass def delete_student(self): # 处理删除学生逻辑 pass def query_student(self): # 处理查询学生逻辑 pass ``` 3. 接下来,我们使用文件保存学生信息。学生信息将保存在名为“students.txt”的文件中。每行包含一个学生的信息,以逗号分隔。代码如下: ```python class Application(tk.Tk): def __init__(self): super().__init__() # ... # 加载学生信息 self.load_students() def load_students(self): try: with open("students.txt", "r") as f: for line in f.readlines(): name, age, student_id, score = line.strip().split(",") student = Student(name, age, student_id, score) self.add_student_to_table(student) except FileNotFoundError: pass def save_students(self): with open("students.txt", "w") as f: for student in self.students: f.write(f"{student.name},{student.age},{student.student_id},{student.score}\n") ``` 4. 最后,我们实现登录、添加、修改、删除和查询功能。登录功能验证用户名和密码是否正确,添加、修改、删除和查询功能则分别显示不同的对话框,让用户输入相应的信息。代码如下: ```python import tkinter.messagebox as messagebox class Application(tk.Tk): def __init__(self): super().__init__() # ... def login(self): username = self.username_entry.get() password = self.password_entry.get() if username == "admin" and password == "123456": self.title("学生管理系统") self.geometry("600x400") self.username_label.destroy() self.username_entry.destroy() self.password_label.destroy() self.password_entry.destroy() self.login_button.destroy() self.add_button.pack(side="left") self.modify_button.pack(side="left") self.delete_button.pack(side="left") self.query_button.pack(side="left") else: messagebox.showerror("错误", "用户名或密码错误") def add_student(self): dialog = AddStudentDialog(self) self.wait_window(dialog) def modify_student(self): selected = self.student_table.focus() if not selected: messagebox.showwarning("警告", "请选择一个学生") return student = self.students[self.student_table.index(selected)] dialog = ModifyStudentDialog(self, student) self.wait_window(dialog) def delete_student(self): selected = self.student_table.focus() if not selected: messagebox.showwarning("警告", "请选择一个学生") return student = self.students[self.student_table.index(selected)] if messagebox.askyesno("确认", f"确定要删除学生 {student.name} 吗?"): self.remove_student(student) def query_student(self): dialog = QueryStudentDialog(self) self.wait_window(dialog) def add_student_to_table(self, student): self.student_table.insert("", "end", values=(student.name, student.age, student.student_id, student.score)) self.students.append(student) self.save_students() def modify_student_in_table(self, index, student): self.student_table.item(self.student_table.get_children()[index], values=(student.name, student.age, student.student_id, student.score)) self.students[index] = student self.save_students() def remove_student_from_table(self, index): self.student_table.delete(self.student_table.get_children()[index]) del self.students[index] self.save_students() class AddStudentDialog(tk.Toplevel): def __init__(self, parent): super().__init__(parent) self.title("添加学生") self.name_label = tk.Label(self, text="姓名") self.name_label.pack() self.name_entry = tk.Entry(self) self.name_entry.pack() self.age_label = tk.Label(self, text="年龄") self.age_label.pack() self.age_entry = tk.Entry(self) self.age_entry.pack() self.student_id_label = tk.Label(self, text="学号") self.student_id_label.pack() self.student_id_entry = tk.Entry(self) self.student_id_entry.pack() self.score_label = tk.Label(self, text="成绩") self.score_label.pack() self.score_entry = tk.Entry(self) self.score_entry.pack() self.add_button = tk.Button(self, text="添加", command=self.add_student) self.add_button.pack() def add_student(self): name = self.name_entry.get() age = self.age_entry.get() student_id = self.student_id_entry.get() score = self.score_entry.get() if not name or not age or not student_id or not score: messagebox.showwarning("警告", "请填写完整信息") return try: age = int(age) student_id = int(student_id) score = int(score) except ValueError: messagebox.showerror("错误", "年龄、学号和成绩必须是整数") return if not 0 <= age <= 150: messagebox.showerror("错误", "年龄必须在0至150之间") return if not 100000 <= student_id <= 999999: messagebox.showerror("错误", "学号必须是6位数字") return if not 0 <= score <= 100: messagebox.showerror("错误", "成绩必须在0至100之间") return student = Student(name, age, student_id, score) parent.add_student_to_table(student) self.destroy() class ModifyStudentDialog(tk.Toplevel): def __init__(self, parent, student): super().__init__(parent) self.title("修改学生") self.name_label = tk.Label(self, text="姓名") self.name_label.pack() self.name_entry = tk.Entry(self, text=student.name) self.name_entry.pack() self.age_label = tk.Label(self, text="年龄") self.age_label.pack() self.age_entry = tk.Entry(self, text=student.age) self.age_entry.pack() self.student_id_label = tk.Label(self, text="学号") self.student_id_label.pack() self.student_id_entry = tk.Entry(self, text=student.student_id) self.student_id_entry.pack() self.score_label = tk.Label(self, text="成绩") self.score_label.pack() self.score_entry = tk.Entry(self, text=student.score) self.score_entry.pack() self.modify_button = tk.Button(self, text="修改", command=self.modify_student) self.modify_button.pack() def modify_student(self): name = self.name_entry.get() age = self.age_entry.get() student_id = self.student_id_entry.get() score = self.score_entry.get() if not name or not age or not student_id or not score: messagebox.showwarning("警告", "请填写完整信息") return try: age = int(age) student_id = int(student_id) score = int(score) except ValueError: messagebox.showerror("错误", "年龄、学号和成绩必须是整数") return if not 0 <= age <= 150: messagebox.showerror("错误", "年龄必须在0至150之间") return if not 100000 <= student_id <= 999999: messagebox.showerror("错误", "学号必须是6位数字") return if not 0 <= score <= 100: messagebox.showerror("错误", "成绩必须在0至100之间") return student = Student(name, age, student_id, score) parent.modify_student_in_table(parent.students.index(self.student), student) self.destroy() class QueryStudentDialog(tk.Toplevel): def __init__(self, parent): super().__init__(parent) self.title("查询学生") self.query_label = tk.Label(self, text="查询条件") self.query_label.pack() self.query_entry = tk.Entry(self) self.query_entry.pack() self.query_button = tk.Button(self, text="查询", command=self.query_student) self.query_button.pack() self.result_table = ttk.Treeview(self, columns=("name", "age", "student_id", "score"), show="headings") self.result_table.pack() self.result_table.heading("name", text="姓名") self.result_table.heading("age", text="年龄") self.result_table.heading("student_id", text="学号") self.result_table.heading("score", text="成绩") def query_student(self): query = self.query_entry.get() results = [] for student in parent.students: if query in student.name or query in str(student.age) or query in str(student.student_id) or query in str(student.score): results.append(student) for child in self.result_table.get_children(): self.result_table.delete(child) for student in results: self.result_table.insert("", "end", values=(student.name, student.age, student.student_id, student.score)) ```

相关推荐

最新推荐

recommend-type

基于springboot+vue+MySQL实现的在线考试系统+源代码+文档

web期末作业设计网页 基于springboot+vue+MySQL实现的在线考试系统+源代码+文档
recommend-type

318_面向物联网机器视觉的目标跟踪方法设计与实现的详细信息-源码.zip

提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
recommend-type

FPGA Verilog 计算信号频率,基础时钟100Mhz,通过锁相环ip核生成200Mhz检测时钟,误差在10ns

结合等精度测量原理和原理示意图可得:被测时钟信号的时钟频率fx的相对误差与被测时钟信号无关;增大“软件闸门”的有效范围或者提高“标准时钟信号”的时钟频率fs,可以减小误差,提高测量精度。 实际闸门下被测时钟信号周期数为X,设被测信号时钟周期为Tfx,它的时钟频率fx = 1/Tfx,由此可得等式:X * Tfx = X / fx = Tx(实际闸门)。 其次,将两等式结合得到只包含各自时钟周期计数和时钟频率的等式:X / fx = Y / fs = Tx(实际闸门),等式变换,得到被测时钟信号时钟频率计算公式:fx = X * fs / Y。 最后,将已知量标准时钟信号时钟频率fs和测量量X、Y带入计算公式,得到被测时钟信号时钟频率fx。
recommend-type

校园二手商品交易系统三.wmv

校园二手商品交易系统三.wmv
recommend-type

基于Spring Security的OAuth2.1和OIDC1.0认证服务器设计源码

本源码提供了一个基于Spring Security框架的OAuth2.1和OIDC1.0认证服务器实现。项目包含102个文件,主要使用Java(51个文件)、JSP(12个文件)、XML(10个文件)、HTML(9个文件)等编程语言和标记语言开发。此外,还包括了properties、txt、ddl、css、ico等格式的文件。这个认证服务器项目旨在实现现代的身份验证和授权机制,支持OAuth 2.1和OpenID Connect 1.0协议,适用于需要在应用程序中实现安全认证的开发者。
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柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

hive中 的Metastore

Hive中的Metastore是一个关键的组件,它用于存储和管理Hive中的元数据。这些元数据包括表名、列名、表的数据类型、分区信息、表的存储位置等信息。Hive的查询和分析都需要Metastore来管理和访问这些元数据。 Metastore可以使用不同的后端存储来存储元数据,例如MySQL、PostgreSQL、Oracle等关系型数据库,或者Hadoop分布式文件系统中的HDFS。Metastore还提供了API,使得开发人员可以通过编程方式访问元数据。 Metastore的另一个重要功能是跟踪表的版本和历史。当用户对表进行更改时,Metastore会记录这些更改,并且可以让用户回滚到
recommend-type

JSBSim Reference Manual

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