请帮我编写基于python语言的根据学习生活桌面应用程序,具体要求:(1)使用类实现数据封装(2)使用tkinter实现界面UI设计(3)使用文件保存数据(4)能实现用户登录功能和实现数据的增加、修改、删除和查询
时间: 2024-01-27 11:06:35 浏览: 133
好的,我会为您解答这个问题。首先,您需要了解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))
```
阅读全文