这是一个比较综合性的程序,要求能运用GUI 和面向对象的方法来编程,同时涉及到数据的读写(文本或mysql)和保存。工
程结构: 学生明细:
================================GUI界面设计===============================
========================运行界面============================
=======================实现代码=========================
1.GUI-- basedetail.py
from tkinter import *
from tkinter.ttk import *
class BaseDetail(Toplevel): # 弹出窗体模板
def __init__(self):
super().__init__()
self.geometry("600x600+700+150")
self.resizable(0, 0)
self.iconbitmap() # 添加一个弹窗
self["bg"] = "lightgray"
# 准备一个Style
self.Style01 = Style()
self.Style01.configure("Title.TLabel", font=("微软雅黑", 26, "bold"), forground="Navy", background="lightblue")
self.Style01.configure("TPanedwindow", background="SkyBlue")
self.Style01.configure("TButton", font=("微软雅黑", 10, "bold"), forground="Navy")
# 加载top_banner
self.Login_image = PhotoImage(file="../file/stu_top_banner01.jpg")
self.Label_image = Label(self, image=self.Login_image)
self.Label_image.place(x=0, y=0)
# 加载一个标题label
self.Label_Title = Label(self, text="", style="Title.TLabel")
self.Label_Title.place(x=20, y=30)
# 添加一个pane
self.Pane_detail = Panedwindow(self, width=588, height=420)
self.Pane_detail.place(x=6, y=110)
# 添加保存按钮
self.Button_save = Button(self, text="保存", width=10, command=self.commit)
self.Button_save.place(x=300, y=550)
# 添加关闭按钮
self.Button_close = Button(self, text="关闭", width=10, command=self.close_gui)
self.Button_close.place(x=450, y=550)
def close_gui(self):
self.destroy()
def commit(self):
pass
if __name__ == '__main__':
basedeatil = BaseDetail()
basedeatil.mainloop()
2. GUI-- maingui.py
from tkinter import *
from tkinter.ttk import *
from fileoperator import *
from studentdetail import *
from tkinter.messagebox import *
from basedetail import *
from teacherdetail import *
class MainGUI(Tk):
def __init__(self):
super().__init__()
self.title("学生教师信息管理系统")
self.geometry("1400x600+100+100")
self.resizable(0, 0)
# 实例化File 读取学生和教师信息到系统中
self.file_info = File()
self.student_all = self.file_info.list_student_all
self.teacher_all = self.file_info.list_teacher_all
self.student_current = []
self.flag = 0
# 定义一个Style
self.Style01 = Style()
self.Style01.configure("TLabel", font=("微软雅黑", 14, "bold"), foreground="Navy")
# 学生和教师的标签
self.Label_student = Label(self, text="学生信息列表:")
self.Label_student.place(x=40, y=60)
self.Label_teacher = Label(self, text="教师信息列表")
self.Label_teacher.place(x=720, y=60)
# 添加学生表格
self.student_gui()
# 添加教师表格
self.teacher_gui()
# ==========学生明细按钮===============
# 添加按钮
self.Button_add_student = Button(self, text="添加", width=10, command=self.add_student)
self.Button_add_student.place(x=400, y=550)
# 修改按钮
self.Button_add_student = Button(self, text="修改", width=10, command=self.update_student)
self.Button_add_student.place(x=500, y=550)
# 删除按钮
self.Button_add_student = Button(self, text="删除", width=10, command=self.delete_student)
self.Button_add_student.place(x=600, y=550)
# ==========教师明细按钮===============
# 添加按钮
self.Button_add_teacher = Button(self, text="添加", width=10, command=self.add_teacher)
self.Button_add_teacher.place(x=1080, y=550)
# 修改按钮
self.Button_add_teacher = Button(self, text="修改", width=10, command=self.update_teacher)
self.Button_add_teacher.place(x=1180, y=550)
# 删除按钮
self.Button_add_teacher = Button(self, text="删除", width=10, command=self.delete_teacher)
self.Button_add_teacher.place(x=1280, y=550)
# 双击表格某一行触发的事件
self.Tree_student.bind("<Double-1>", self.view_student)
# 双击表格某一行触发的事件
self.Tree_teacher.bind("<Double-1>", self.view_teacher)
# 把窗体关闭按钮的行为转化为方法
self.protocol("WM_DELETE_WINDOW", self.close_windows)
self.load_student_treeview()
self.load_teacher_treeview()
def student_gui(self): # 添加treeview控件
self.Tree_student = Treeview(self, column=("sno", "sname", "gender", "profession", "mobile", "email"),
show="headings", height=20)
# 设置每一列对其方式
self.Tree_student.column("sno", width=80, anchor="center")
self.Tree_student.column("sname", width=80, anchor="center")
self.Tree_student.column("gender", width=60, anchor="center")
self.Tree_student.column("profession", width=120, anchor="center")
self.Tree_student.column("mobile", width=150, anchor="center")
self.Tree_student.column("email", width=150, anchor="center")
# 设置每列的标题
self.Tree_student.heading("sno", text="学号")
self.Tree_student.heading("sname", text="姓名")
self.Tree_student.heading("gender", text="性别")
self.Tree_student.heading("profession", text="专业")
self.Tree_student.heading("mobile", text="手机号码")
self.Tree_student.heading("email", text="邮箱地址")
# 展示表格
self.Tree_student.place(x=35, y=95)
def teacher_gui(self):
# 添加treeview控件
self.Tree_teacher= Treeview(self, columns=("tid", "name", "gender", "title", "college", "mobile"),
show="headings", height=20)
# 设置每一列对其方式
self.Tree_teacher.column("tid", width=80, anchor="center")
self.Tree_teacher.column("name", width=80, anchor="center")
self.Tree_teacher.column("gender", width=60, anchor="center")
self.Tree_teacher.column("title", width=120, anchor="center")
self.Tree_teacher.column("college", width=150, anchor="center")
self.Tree_teacher.column("mobile", width=150, anchor="center")
# 设置每列的标题
self.Tree_teacher.heading("tid", text="员工号")
self.Tree_teacher.heading("name", text="姓名")
self.Tree_teacher.heading("gender", text="性别")
self.Tree_teacher.heading("title", text="职称")
self.Tree_teacher.heading("college", text="毕业院校")
self.Tree_teacher.heading("mobile", text="手机号码")
# 展示表格
self.Tree_teacher.place(x=720, y=95)
def load_student_treeview(self):
"""
将student info 导入 treeview
:return:
"""
# 先把当前 treeview 内容清空
for index in self.Tree_student.get_children():
self.Tree_student.delete(index)
# 加载list 中的数据
if len(self.student_all) == 0:
showinfo("系统消息:", "没有任何学生信息加载到表格!!")
else:
for index in range(len(self.student_all)):
self.Tree_student.insert("", index, values=(self.student_all[index][0], self.student_all[index][1],
self.student_all[index][2], self.student_all[index][6],
self.student_all[index][4], self.student_all[index][5]))
def load_teacher_treeview(self):
"""
将 teacher info 导入 treeview
:return:
"""
# 先把当前 treeview 内容清空
for index in self.Tree_teacher.get_children():
self.Tree_teacher.delete(index)
# 加载list 中的数据
if len(self.teacher_all) == 0:
showinfo("系统消息:", "没有任何老师信息加载到表格!!")
else:
for index in range(len(self.teacher_all)):
self.Tree_teacher.insert("", index, values=(self.teacher_all[index][0], self.teacher_all[index][1],
self.teacher_all[index][2], self.teacher_all[index][6],
self.teacher_all[index][7], self.teacher_all[index][4]))
def view_student(self, event):
"""
双击某一行加载明细信息
:return:
"""
# 修改flag
self.flag = 1
# 获取点击的当前行
item = self.Tree_student.selection()[0] # 获得双击行的ID
currtent_student = self.Tree_student.item(item, "values") # 根据ID 取出Values
for item in self.student_all:
if currtent_student[0] == item[0]:
self.student_current = item
# 以查看的方法加载
StudentDetail(self.flag, self.student_all, self.student_current)
# detail = StudentDetail(self.flag, self.student_all, self.student_current)
def update_student(self):
"""
修改某一行加载明细信息
:return:
"""
# 修改flag
self.flag = 2
# 获取点击的当前行
item = self.Tree_student.selection()[0]
currtent_student = self.Tree_student.item(item, "values") # 根据ID 取出Values
for item in self.student_all:
if currtent_student[0] == item[0]:
self.student_current = item
# 以查看的方法加载
detail = StudentDetail(self.flag, self.student_all, self.student_current)
# 接收反馈并刷新表格
self.wait_window(detail)
# 接收 返回的值
get_return_number = detail.userinfo
# 如果值为1 刷新
if get_return_number == 1:
self.load_student_treeview()
def add_student(self):
"""
新增学生明细
:return:
"""
# 修改flag
self.flag = 3
# 加载添加的页面
detail = StudentDetail(self.flag, self.student_all, self.student_current)
self.wait_window(detail)
# 接收返回的值
get_return_number = detail.userinfo
# 判断值为1 刷新
if get_return_number == 1:
self.load_student_treeview()
def delete_student(self):
"""
删除学生信息
:return:
"""
# 获取双击行的数据
item = self.Tree_student.selection()[0]
Temp_student_list = self.Tree_student.item(item, "values")
student_object = ""
for item in self.student_all:
if Temp_student_list[0] == item[0]:
# 封装到student
student_object = Student(item[0], item[1], item[2], item[3],
item[4], item[5], item[6], item[7])
# 实例化studentservices
current_student_services = StudentServices(self.student_all, student_object)
# 询问是否删除
choose = askyesno("删除确认", "确认要删除学生信息【学号:" + student_object.sno + "姓名:" + student_object.name + "】信息吗?" )
if choose:
# 执行删除操作
# for index in range(len(self.student_all)):
# if self.student_all[index][0] == Temp_student_list[0]:
# self.student_all.pop(index)
# break
current_student_services.delete()
showinfo("系统消息", "删除成功!")
# 更新表格
self.load_student_treeview()
else:
return 0
def close_windows(self):
showinfo("系统提醒:", "关闭窗体")
# 询问是否保存修改
choose = askyesno("关闭前的提醒", "关闭窗体前是否要降修改写入到文件?")
if choose:
try:
self.file_info.write_to_file()
showinfo("系统提示:", "所有的修改意见写入到文件!")
self.destroy()
except Exception as e:
showinfo("系统消息:", e)
else:
showinfo("系统提示:", "所有修改不会写入文件!")
self.destroy()
def view_teacher(self, event):
"""
双击某一行加载明细信息
:return:
"""
# 1) 修改flag
self.flag = 4
# 获取点击的当前行
item = self.Tree_teacher.selection()[0] # 获得双击行的ID
current_teacher = self.Tree_teacher.item(item, "values") # 根据ID 取出Values
for item in self.teacher_all:
if current_teacher[0] == item[0]:
self.teacher_current = item
# 以查看的方法加载
detail = TeacherDetail(self.flag, self.teacher_all, self.teacher_current)
def update_teacher(self):
"""
修改某一行加载明细信息
:return:
"""
# 修改某一行加载明细信息
self.flag = 5
# 获取点击的当前行
item = self.Tree_teacher.selection()[0]
currtent_teacher = self.Tree_teacher.item(item, "values") # 根据ID 取出Values
for item in self.teacher_all:
if currtent_teacher[0] == item[0]:
self.teacher_current = item
# 以查看的方法加载
detail = TeacherDetail(self.flag, self.teacher_all, self.teacher_current)
# 接收反馈并刷新表格
self.wait_window(detail)
# 接收 返回的值
get_return_number = detail.userinfo
# 如果值为1 刷新
if get_return_number == 1:
self.load_teacher_treeview()
def add_teacher(self):
detail = StudentDetail(6)
def delete_teacher(self):
"""
删除选定的某一行
:return:
"""
pass
if __name__ == '__main__':
maingui = MainGUI()
maingui.mainloop()
3. GUI--studentdetail.py
"""
实现学生明细的GUI
由BaseDetail 派生
"""
"""
查看:只读 , 修改Title="查看学生明细====", --只有关闭按钮
触发: 双击 某一行数据
修改:学号,职工号不能修改,修改Title="修改学生明细===="其他可以
触发操作:选中某一行数据, 然后点击修改按钮
添加:所有的输入框为空,修改Title="添加学生明细====" 都可以添加
触发操作:
点击添加按钮
"""
from basedetail import *
from tkinter.messagebox import *
from person import *
from studentservices import *
class StudentDetail(BaseDetail):
# def __init__(self, flag, current_student="", list_student_all=""):
# def __init__(self, flag, current_student: list, list_student_all: list):
def __init__(self, flag, list_student_all: list, current_student: list):
super().__init__()
self.flag = flag # 用flag 来标记查看, 添加,修改 三种界面状态
self.current_student = current_student
self.list_student_all = list_student_all
# 修改Style
self.Style01.configure("TLabel", font=("微软雅黑", 14), forground="Navy")
self.Style01.configure("TRadiobutton", font=("微软雅黑", 14, "bold"), foreground="blue", background="lightcyan")
self.Label_Title = Label(self, text="===学生明细===", style="Title.TLabel")
self.Label_Title.place(x=20, y=30)
# 调用
self.student_detail()
# 根据flag 值初始化GUI
self.load_gui_by_flag()
def student_detail(self):
# 添加StudentDetail 内容
# ====学号====
self.Label_sno = Label(self.Pane_detail, text="学号:")
self.Label_sno.place(x=140, y=30)
self.var_sno = StringVar()
self.Entry_sno = Entry(self.Pane_detail, textvariable=self.var_sno, font=("微软雅黑", 14, "bold"), width=10)
self.Entry_sno.place(x=220, y=28)
# 姓名
self.Label_sname = Label(self.Pane_detail, text="姓名:")
self.Label_sname.place(x=140, y=70)
self.var_name = StringVar()
self.Entry_sname = Entry(self.Pane_detail, textvariable=self.var_name, font=("微软雅黑", 14, "bold"), width=10)
self.Entry_sname.place(x=220, y=68)
# 性别
self.Label_gender = Label(self.Pane_detail, text="性别:")
self.Label_gender.place(x=140, y=120)
self.var_gender = IntVar()
self.Radio_man = Radiobutton(self.Pane_detail, text="男", variable=self.var_gender, value=1)
self.Radio_man.place(x=220, y=118)
self.Radio_women = Radiobutton(self.Pane_detail, text="女", variable=self.var_gender, value=2)
self.Radio_women.place(x=280, y=118)
# ====出生日期========
self.Label_Birthday = Label(self.Pane_detail, text="出生日期:")
self.Label_Birthday.place(x=102, y=170)
self.var_birthday = StringVar()
self.Entry_Birthday = Entry(self.Pane_detail, textvariable=self.var_birthday, font=("微软雅黑", 14, "bold"), width=16)
self.Entry_Birthday.place(x=220, y=168)
# ==== 手机号码========
self.Label_mobile = Label(self.Pane_detail, text="手机号码:")
self.Label_mobile.place(x=102, y=218)
self.var_mobile = StringVar()
self.Entry_mobile = Entry(self.Pane_detail, textvariable=self.var_mobile, font=("微软雅黑", 14, "bold"), width=16)
self.Entry_mobile.place(x=220, y=218)
# ====邮箱地址========
self.Label_email = Label(self.Pane_detail, text="邮箱地址:")
self.Label_email.place(x=102, y=268)
self.var_email = StringVar()
self.Entry_email = Entry(self.Pane_detail, textvariable=self.var_email, font=("微软雅黑", 14, "bold"), width=16)
self.Entry_email.place(x=220, y=268)
# ====所学专业========
self.Label_profession = Label(self.Pane_detail, text="所学专业:")
self.Label_profession.place(x=102, y=318)
self.var_profession = StringVar()
self.Entry_profession = Entry(self.Pane_detail, textvariable=self.var_profession, font=("微软雅黑", 14, "bold"), width=16)
self.Entry_profession.place(x=220, y=318)
# 入学时间
self.Label_stu_time = Label(self.Pane_detail, text="入学时间:")
self.Label_stu_time.place(x=102, y=368)
self.var_stu_time = StringVar()
self.Entry_stu_time = Entry(self.Pane_detail, textvariable=self.var_stu_time, font=("微软雅黑", 14, "bold"), width=16)
self.Entry_stu_time.place(x=220, y=368)
def load_gui_by_flag(self):
"""
根据flag 值加载不同的GUI 状态
:return:
"""
if self.flag == 1:
# 1)查看的窗口状态
self.Label_Title["text"] = "==查看学生明细=="
# 2) 隐藏保存按钮
self.Button_save.place_forget()
# 3) 填充数据
self.var_sno.set(self.current_student[0])
self.var_name.set(self.current_student[1])
if "男" in self.current_student[2]:
self.var_gender.set(1)
else:
self.var_gender.set(2)
self.var_birthday.set(self.current_student[3])
self.var_mobile.set(self.current_student[4])
self.var_email.set(self.current_student[5])
self.var_profession.set(self.current_student[6])
self.var_stu_time.set(self.current_student[7])
# 4)禁用Entry
self.Entry_sno["state"] = DISABLED
self.Entry_sname["state"] = DISABLED
self.Radio_man["state"] = DISABLED
self.Radio_women["state"] = DISABLED
self.Entry_Birthday["state"] = DISABLED
self.Entry_mobile["state"] = DISABLED
self.Entry_email["state"] = DISABLED
self.Entry_profession["state"] = DISABLED
self.Entry_stu_time["state"] = DISABLED
elif self.flag == 2:
# 1)修改的窗口明细
self.Label_Title["text"] = "==修改学生明细=="
# 2) 隐藏保存按钮
# self.Button_save.place_forget()
# 3) 填充数据
self.var_sno.set(self.current_student[0])
self.var_name.set(self.current_student[1])
if "男" in self.current_student[2]:
self.var_gender.set(1)
else:
self.var_gender.set(2)
self.var_birthday.set(self.current_student[3])
self.var_mobile.set(self.current_student[4])
self.var_email.set(self.current_student[5])
self.var_profession.set(self.current_student[6])
self.var_stu_time.set(self.current_student[7])
# 4)禁用Entry
self.Entry_sno["state"] = DISABLED
elif self.flag == 3:
# 添加的窗口明细
self.Label_Title["text"] = "==添加学生明细=="
def commit(self):
# super().commit()
"""
点击保存按钮响应的事件
:return:
"""
# showinfo("系统消息", "你点击了保存按钮!")
super().commit()
if self.flag == 3: # 添加模块
# 添加额外功能
# 1) 封装当前的学生
current_student = self.get_current_student()
current_studentservices = StudentServices(self.list_student_all, current_student)
# 2 校验学生
return_number = current_studentservices.check()
if return_number == 2:
showinfo("系统消息", "学号不符合要求!")
elif return_number == 3:
showinfo("系统消息", "姓名不符合要求!")
elif return_number == 4:
showinfo("系统消息", "手机号码不符合要求!")
elif return_number == 5:
showinfo("系统消息", "邮箱地址不符合要求!")
elif return_number == 1:
showinfo("系统消息", "都符合要求!")
# 3) 添加功能
current_studentservices.add()
# 提示添加成功
showinfo("系统消息", "学生信息添加成功")
# 4) 反馈信号给主窗体
self.userinfo = 1
# 5) 关闭
self.destroy()
elif self.flag == 2: # 修改模块
# 修改功能
# 1) 封装当前的学生
current_student = self.get_current_student()
current_studentservices = StudentServices(self.list_student_all, current_student)
# 2 校验学生
return_number = current_studentservices.check()
if return_number == 2:
showinfo("系统消息", "学号不符合要求!")
elif return_number == 3:
showinfo("系统消息", "姓名不符合要求!")
elif return_number == 4:
showinfo("系统消息", "手机号码不符合要求!")
elif return_number == 5:
showinfo("系统消息", "邮箱地址不符合要求!")
elif return_number == 1:
showinfo("系统消息", "都符合要求!")
# 3) 修改功能
current_studentservices.update()
# 提示添加成功
showinfo("系统消息", "学生信息修改成功")
# 4) 反馈信号给主窗体
self.userinfo = 1
# 5) 关闭
self.destroy()
def get_current_student(self):
"""
把当前填入的信息封装到student 类
:return:
"""
sno = self.var_sno.get()
name = self.var_name.get()
gender = ""
if self.var_gender.get() == 1:
gender = "男"
elif self.var_gender.get() == 2:
gender = "女"
birthday = self.var_birthday.get()
mobile = self.var_mobile.get()
email = self.var_email.get()
profession = self.var_profession.get()
study_time = self.var_stu_time.get()
# 封装
current_student = Student(sno, name, gender, birthday, mobile, email, profession, study_time)
# 返回
return current_student
if __name__ == '__main__':
student = StudentDetail(1)
student.mainloop()
4. GUI--teacherdetail.py
"""
实现老师明细的GUI
由BaseDetail 派生
"""
from basedetail import *
class TeacherDetail(BaseDetail):
def __init__(self, flag, list_teacher_all: list, current_teacher: list):
super().__init__()
self.flag = flag
self.list_teacher_all = list_teacher_all
self.current_teacher = current_teacher
# 修改Style
self.Style01.configure("TLabel", font=("微软雅黑", 12), forground="Navy")
self.Style01.configure("TRadiobutton", font=("微软雅黑", 12, "bold"), foreground="blue", background="lightcyan")
# 添加StudentDetail 内容
self.Label_Title = Label(self, text="===教师明细===", style="Title.TLabel")
self.Label_Title.place(x=20, y=30)
# 执行
self.teacher_detail()
# 根据flag 值来初始化 GUI界面
self.load_gui_by_flag()
def teacher_detail(self):
# ====职工号====
self.Label_tid = Label(self.Pane_detail, text="职工号:")
self.Label_tid.place(x=120, y=10)
self.var_tid = StringVar()
self.Entry_tid = Entry(self.Pane_detail, textvariable=self.var_tid, font=("微软雅黑", 12, "bold"), width=10)
self.Entry_tid.place(x=220, y=8)
# 姓名
self.Label_sname = Label(self.Pane_detail, text="姓名:")
self.Label_sname.place(x=140, y=55)
self.var_name = StringVar()
self.Entry_sname = Entry(self.Pane_detail, textvariable=self.var_name, font=("微软雅黑", 12, "bold"), width=10)
self.Entry_sname.place(x=220, y=53)
# 性别
self.Label_gender = Label(self.Pane_detail, text="性别:")
self.Label_gender.place(x=140, y=100)
self.var_gender = IntVar()
self.Radio_man = Radiobutton(self.Pane_detail, text="男", variable=self.var_gender, value=1)
self.Radio_man.place(x=220, y=98)
self.Radio_women = Radiobutton(self.Pane_detail, text="女", variable=self.var_gender, value=2)
self.Radio_women.place(x=280, y=98)
# ====出生日期========
self.Label_Birthday = Label(self.Pane_detail, text="出生日期:")
self.Label_Birthday.place(x=102, y=145)
self.var_birthday = StringVar()
self.Entry_Birthday = Entry(self.Pane_detail, textvariable=self.var_birthday, font=("微软雅黑", 12, "bold"), width=16)
self.Entry_Birthday.place(x=220, y=143)
# ==== 手机号码========
self.Label_mobile = Label(self.Pane_detail, text="手机号码:")
self.Label_mobile.place(x=102, y=190)
self.var_mobile = StringVar()
self.Entry_mobile = Entry(self.Pane_detail, textvariable=self.var_mobile, font=("微软雅黑", 12, "bold"), width=16)
self.Entry_mobile.place(x=220, y=188)
# ====邮箱地址========
self.Label_email = Label(self.Pane_detail, text="邮箱地址:")
self.Label_email.place(x=102, y=235)
self.var_email = StringVar()
self.Entry_email = Entry(self.Pane_detail, textvariable=self.var_email, font=("微软雅黑", 15, "bold"), width=22)
self.Entry_email.place(x=220, y=233)
# ====职称========
self.Label_title = Label(self.Pane_detail, text="职称:")
self.Label_title.place(x=130, y=280)
self.var_title = StringVar()
self.Entry_title = Entry(self.Pane_detail, textvariable=self.var_title, font=("微软雅黑", 15, "bold"), width=16)
self.Entry_title.place(x=220, y=278)
# 毕业学校
self.Label_college = Label(self.Pane_detail, text="毕业学校:")
self.Label_college.place(x=102, y=325)
self.var_college = StringVar()
self.Entry_college = Entry(self.Pane_detail, textvariable=self.var_college, font=("微软雅黑", 15, "bold"), width=16)
self.Entry_college.place(x=220, y=323)
# 入职时间
self.Label_work_time = Label(self.Pane_detail, text="入职时间:")
self.Label_work_time.place(x=102, y=370)
self.var_work_time = StringVar()
self.Entry_work_time = Entry(self.Pane_detail, textvariable=self.var_work_time, font=("微软雅黑", 15, "bold"), width=16)
self.Entry_work_time.place(x=220, y=368)
def load_gui_by_flag(self):
"""
根据
:return:
"""
if self.flag == 4:
# 1)查看的窗口状态
self.Label_Title["text"] = "==查看教师明细=="
# 2) 隐藏保存按钮
self.Button_save.place_forget()
# 3) 填充数据
self.var_tid.set(self.current_teacher[0])
self.var_name.set(self.current_teacher[1])
if "男" in self.current_teacher[2]:
self.var_gender.set(1)
else:
self.var_gender.set(2)
self.var_birthday.set(self.current_teacher[3])
self.var_mobile.set(self.current_teacher[4])
self.var_email.set(self.current_teacher[5])
self.var_title.set(self.current_teacher[6])
self.var_college.set(self.current_teacher[7])
self.var_work_time.set(self.current_teacher[8])
# 4)禁用Entry
self.Entry_tid["state"] = DISABLED
self.Entry_sname["state"] = DISABLED
self.Radio_man["state"] = DISABLED
self.Radio_women["state"] = DISABLED
self.Entry_Birthday["state"] = DISABLED
self.Entry_mobile["state"] = DISABLED
self.Entry_email["state"] = DISABLED
self.Entry_title["state"] = DISABLED
self.Entry_college["state"] = DISABLED
self.Entry_work_time["state"] = DISABLED
elif self.flag == 5:
# 1) 修改的窗口明细
self.Label_Title["text"] = "==修改教师明细=="
# 2) 隐藏保存按钮
# self.Button_save.place_forget()
# 3) 填充数据
self.var_tid.set(self.current_teacher[0])
self.var_name.set(self.current_teacher[1])
if "男" in self.current_teacher[2]:
self.var_gender.set(1)
else:
self.var_gender.set(2)
self.var_birthday.set(self.current_teacher[3])
self.var_mobile.set(self.current_teacher[4])
self.var_email.set(self.current_teacher[5])
self.var_title.set(self.current_teacher[6])
self.var_college.set(self.current_teacher[7])
self.var_work_time.set(self.current_teacher[8])
# 4)禁用Entry
self.Entry_tid["state"] = DISABLED
elif self.flag == 6:
# 添加的窗口明细
self.Label_Title["text"] = "==添加教师明细=="
if __name__ == '__main__':
teacher = TeacherDetail()
teacher.mainloop()
5. Services--check.py
import re
class Check:
def check_sno(self, sno):
pattern = re.compile("^95\d{4}$")
match_result = pattern.match(sno)
if match_result is None:
return False
else:
return True
def check_name(self, name:str):
# 校验是否小于10 并大于2
if len(name) <=10 and len(name) >=2:
for item in name:
if item < "\u4E00" and item > "\u9FA5":
return False
else:
return True
else:
return False
def check_gender(self, gender:str):
if gender.strip() in ["男", "女"]:
return True
else:
return False
def check_mobile(self, mobile):
pattern = re.compile(r"^1[3578]\d{9}$")
match_result = pattern.match(mobile)
if match_result is None:
return False
else:
return True
def check_email(self, email):
pattern = re.compile(r"\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}")
match_result = pattern.match(email)
if match_result is None:
return False
else:
return True
if __name__ == '__main__':
sno = input("请输入学号:")
check = Check()
print(check.check_sno(sno))
6. Services--fileoperator.py
"""
主要实现读取文件内容,保存到文件
"""
import os
class File:
def __init__(self):
# 记录学生信息的文件路径
self.student_path = "../file/student.txt"
# 记录教师信息的文件路径
self.teacher_path = "../file/teacher.txt"
# 定义存储所有学生list
self.list_student_all = []
# 定义存储所有教师list
self.list_teacher_all = []
self.read_from_file()
def read_from_file(self):
# 读取学生信息
try:
with open(self.student_path, mode="r") as fd_student:
current_line = fd_student.readline()
while current_line:
# 通过空格进行分割
student_list = current_line.split(",")
# 添加 学生姓名
self.list_student_all.append(student_list)
# 将一行信息添加
# self.list_student_all.append(temp_list)
# 读取下一行
current_line = fd_student.readline()
# print(self.list_student_all)
except Exception as e:
print(e)
# ==========读取教师信息=================
try:
with open(self.teacher_path, mode="r", encoding="utf-8") as fd_teacher:
current_line = fd_teacher.readline()
while current_line:
# 通过空格进行分割
teacher_list = current_line.split(",")
# 添加 学生姓名
self.list_teacher_all.append(teacher_list)
# 将一行信息添加
# self.list_student_all.append(temp_list)
# 读取下一行
current_line = fd_teacher.readline()
# print(self.list_student_all)
except Exception as e:
print(e)
def write_to_file(self):
# 写入学生信息
try:
with open(self.student_path, mode="w") as fd_student:
fd_student.write("")
with open(self.student_path, mode="a") as fd_student:
for item in self.list_student_all:
temp = ",".join(item)
temp = temp.replace("\n", "") + "\n"
fd_student.write(temp)
except Exception as e:
print(e)
# 写入教师信息
try:
with open(self.teacher_path, mode="w") as fd_teacher:
fd_teacher.write("")
with open(self.teacher_path, mode="a") as fd_teacher:
for item in self.list_teacher_all:
temp = ",".join(item)
temp = temp.replace("\n", "") + "\n"
fd_teacher.write(temp)
except Exception as e:
print(e)
if __name__ == '__main__':
file = File()
print(file.list_student_all)
print("=======================================")
print(file.list_teacher_all)
7. Services--person.py
"""
实体类:
Person: name, gender, birthday, mobile, email
Student(Person):sno, profession, stud_time
Teacher(Person):tid, title, college, work_time
"""
class Person:
def __init__(self, name, gender,birthday, mobile, email):
self.name = name
self.gender = gender
self.birthday = birthday
self.mobile = mobile
self.email = email
# 派生类
class Student(Person):
def __init__(self, sno, name, gender, birthday, mobile, email, profession, study_time):
super().__init__(name, gender, birthday, mobile, email) # 初始化父类
self.sno = sno
self.profession = profession
self.study_time = study_time
class Teacher(Person):
def __init__(self, tid, name, gender, birthday, mobile, email, title, college, work_time):
super().__init__(name, gender, birthday, mobile, email)
self.tid = tid
self.title = title
self.college = college
self.work_time = work_time
8.Services--services.py
"""
抽象类:
抽象方法:Add, update, delete, check
"""
from abc import *
class Services(metaclass=ABCMeta):
@abstractmethod
def add(self): pass
@abstractmethod
def update(self): pass
@abstractmethod
def delete(self): pass
@abstractmethod
def check(self): pass
9. Services---studentservices.py
from check import *
from services import *
from person import *
class StudentServices(Services, Check):
def __init__(self, list_all: list, student_current: Student):
Services.__init__(self)
Check.__init__(self)
self.list_all = list_all
self.student_current = student_current # 一行学生信息
def check(self):
"""
校验输入是否有效
:return:
"""
# return_number = 0
if not self.check_sno(self.student_current.sno):
return_number = 2
elif not self.check_name(self.student_current.name):
return_number = 3
elif self.check_mobile(self.student_current.mobile) is not True:
return_number = 4
elif self.check_email(self.student_current.email) is not True:
return_number = 5
else:
return_number = 1
# 返回
return return_number
def add(self):
"""
添加
:return:
"""
current_student_list = []
current_student_list.append(self.student_current.sno)
current_student_list.append(self.student_current.name)
current_student_list.append(self.student_current.gender)
current_student_list.append(self.student_current.birthday)
current_student_list.append(self.student_current.mobile)
current_student_list.append(self.student_current.email)
current_student_list.append(self.student_current.profession)
current_student_list.append(self.student_current.study_time)
# 当前学生添加到集合中
self.list_all.append(current_student_list)
def update(self):
# 通过循环遍历list, 修改信息
for index in range(len(self.list_all)):
if self.list_all[index][0] == self.student_current.sno:
self.list_all[index][1] = self.student_current.name
self.list_all[index][2] = self.student_current.gender
self.list_all[index][3] = self.student_current.birthday
self.list_all[index][4] = self.student_current.mobile
self.list_all[index][5] = self.student_current.email
self.list_all[index][6] = self.student_current.profession
self.list_all[index][7] = self.student_current.study_time
# 将修改的信息添加到集合中
# return self.list_all
def delete(self):
# 通过循环遍历, 删除信息
for index in range(len(self.list_all)):
if self.list_all[index][0] == self.student_current.sno:
self.list_all.pop(index)
"""
实现学生操作的功能,继承Services 和Check
"""