本周我在学校进行小学期内容——数据结构
数据结构我选择的个人项目为:教师信息管理系统,本次训练使用的语言为python
由添加教师信息,删除教师信息,修改教师信息,查找教师信息等模块组成
源码如下:、
import tkinter as tk from tkinter import ttk from tkinter import messagebox import datetime from tkinter import * # 添加教师信息 def add_teacher(): add_window = tk.Toplevel() add_window.title("添加教师信息") # 创建标签和文本框 id_label = tk.Label(add_window, text="教师号:") id_label.grid(row=0, column=0) id_entry = tk.Entry(add_window) id_entry.grid(row=0, column=1) name_label = tk.Label(add_window, text="姓名:") name_label.grid(row=1, column=0) name_entry = tk.Entry(add_window) name_entry.grid(row=1, column=1) id_number_label = tk.Label(add_window, text="身份证号:") id_number_label.grid(row=2, column=0) id_number_entry = tk.Entry(add_window) id_number_entry.grid(row=2, column=1) address_label = tk.Label(add_window, text="地址:") address_label.grid(row=3, column=0) address_entry = tk.Entry(add_window) address_entry.grid(row=3, column=1) phone_label = tk.Label(add_window, text="电话号码:") phone_label.grid(row=4, column=0) phone_entry = tk.Entry(add_window) phone_entry.grid(row=4, column=1) department_label = tk.Label(add_window, text="部门:") department_label.grid(row=5, column=0) department_entry = tk.Entry(add_window) department_entry.grid(row=5, column=1) salary_label = tk.Label(add_window, text="工资:") salary_label.grid(row=6, column=0) salary_entry = tk.Entry(add_window) salary_entry.grid(row=6, column=1) start_date_label = tk.Label(add_window, text="工作开始日期:") start_date_label.grid(row=7, column=0) start_date_entry = tk.Entry(add_window) start_date_entry.grid(row=7,column=1) major_label = tk.Label(add_window, text="专业:") major_label.grid(row=8, column=0) major_entry = tk.Entry(add_window) major_entry.grid(row=8, column=1) position_label = tk.Label(add_window, text="职位:") position_label.grid(row=9, column=0) position_entry = tk.Entry(add_window) position_entry.grid(row=9, column=1) remark_label = tk.Label(add_window, text="备注:") remark_label.grid(row=10, column=0) remark_entry = tk.Entry(add_window) remark_entry.grid(row=10, column=1) # 创建“确定”按钮 confirm_button = tk.Button( add_window, text="确定", command=lambda: save_teacher( id_entry.get(), name_entry.get(), id_number_entry.get(), address_entry.get(), phone_entry.get(), department_entry.get(), salary_entry.get(), start_date_entry.get(), major_entry.get(), position_entry.get(), remark_entry.get(), add_window, ) ) confirm_button.grid(row=11, column=0, pady=10) cancel_button = tk.Button( add_window, text="取消", command=add_window.destroy ) cancel_button.grid(row=11, column=1, pady=10) def save_teacher(id, name, id_number, address, phone, department, salary, start_date, major, position, remark, window): with open('teachers.txt', 'a') as f: f.write(f'{id},{name},{id_number},{address},{phone},{department},{salary},{start_date},{major},{position},{remark}\n') messagebox.showinfo("提示", "添加成功!") window.destroy() # 删除教师信息 def delete_teacher(): # 创建删除教师信息的窗口 delete_window = tk.Toplevel() delete_window.title("删除教师信息") # 创建标签和文本框 id_label = tk.Label(delete_window, text="教师编号:") id_label.grid(row=0, column=0) id_entry = tk.Entry(delete_window) id_entry.grid(row=0, column=1) # 创建“确定”按钮 confirm_button = tk.Button( delete_window, text="确定", command=lambda: confirm_delete(id_entry.get(), delete_window) ) confirm_button.grid(row=1, column=0, pady=10) # 创建“取消”按钮 cancel_button = tk.Button( delete_window, text="取消", command=delete_window.destroy ) cancel_button.grid(row=1, column=1, pady=10) # 确认删除教师信息 def confirm_delete(teacher_id, window): with open('teachers.txt', 'r') as f: lines = f.readlines() with open('teachers.txt', 'w') as f: for line in lines: if not line.startswith(teacher_id): f.write(line) messagebox.showinfo("提示", "删除成功!") # 关闭窗口 window.destroy() # 修改教师信息 def modify_teacher(): # 创建修改教师信息的窗口 modify_window = tk.Toplevel() modify_window.title("修改教师信息") # 创建标签和文本框 id_label = tk.Label(modify_window, text="教师编号:") id_label.grid(row=0, column=0) id_entry = tk.Entry(modify_window) id_entry.grid(row=0, column=1) name_label = tk.Label(modify_window, text="姓名:") name_label.grid(row=1, column=0) name_entry = tk.Entry(modify_window) name_entry.grid(row=1, column=1) id_number_label = tk.Label(modify_window, text="身份证号:") id_number_label.grid(row=2, column=0) id_number_entry = tk.Entry(modify_window) id_number_entry.grid(row=2, column=1) address_label = tk.Label(modify_window, text="地址:") address_label.grid(row=3, column=0) address_entry = tk.Entry(modify_window) address_entry.grid(row=3, column=1) phone_label = tk.Label(modify_window, text="手机号:") phone_label.grid(row=4, column=0) phone_entry = tk.Entry(modify_window) phone_entry.grid(row=4, column=1) department_label = tk.Label(modify_window, text="部门:") department_label.grid(row=5, column=0) department_entry = tk.Entry(modify_window) department_entry.grid(row=5, column=1) salary_label = tk.Label(modify_window, text="工资:") salary_label.grid(row=6, column=0) salary_entry = tk.Entry(modify_window) salary_entry.grid(row=6, column=1) start_date_label = tk.Label(modify_window, text="参加工作的时间:") start_date_label.grid(row=7, column=0) start_date_entry = tk.Entry(modify_window) start_date_entry.grid(row=7, column=1) major_label = tk.Label(modify_window, text="专业:") major_label.grid(row=8, column=0) major_entry = tk.Entry(modify_window) major_entry.grid(row=8, column=1) position_label = tk.Label(modify_window, text="职务:") position_label.grid(row=9, column=0) position_entry = tk.Entry(modify_window) position_entry.grid(row=9, column=1) remark_label = tk.Label(modify_window, text="备注:") remark_label.grid(row=10, column=0) remark_entry = tk.Entry(modify_window) remark_entry.grid(row=10, column=1) # 创建“确定”按钮 confirm_button = tk.Button( modify_window, text="确定", command=lambda: confirm_modify( id_entry.get(), name_entry.get(), id_number_entry.get(), address_entry.get(), phone_entry.get(), department_entry.get(), salary_entry.get(), start_date_entry.get(), major_entry.get(), position_entry.get(), remark_entry.get(), modify_window ) ) confirm_button.grid(row=11, column=0, pady=10) # 创建“取消”按钮 cancel_button = tk.Button( modify_window, text="取消", command=modify_window.destroy ) cancel_button.grid(row=11, column=1, pady=10) # 确认修改教师信息 def confirm_modify(teacher_id, name, id_number, address,phone, department, salary, start_date, major, position, remark, window): with open('teachers.txt', 'r') as f: lines = f.readlines() with open('teachers.txt', 'w') as f: for line in lines: if line.startswith(teacher_id): new_line = f"{teacher_id},{name},{id_number},{address},{phone},{department},{salary},{start_date},{major},{position},{remark}\n" f.write(new_line) else: f.write(line) messagebox.showinfo("提示", "修改成功!") # 关闭窗口 window.destroy() def search1(window): search1_window = tk.Toplevel(window) search1_window.title("教师编号查询") # 创建标签和文本框 id_label = tk.Label(search1_window, text="教师编号:") id_label.grid(row=0, column=0) id_entry = tk.Entry(search1_window) id_entry.grid(row=0, column=1) # 创建“确定”按钮 confirm_button = tk.Button( search1_window, text="确定", command=lambda: confirm_search1(id_entry.get(), text) ) confirm_button.grid(row=1, column=0, pady=10) cancel_button = tk.Button( search1_window, text="取消", command=search1_window.destroy ) cancel_button.grid(row=1, column=1, pady=10) # 创建结果区域,并添加滚动条 text = tk.Text(search1_window) text.grid(row=2, columnspan=2) scrollbar = tk.Scrollbar(search1_window, command=text.yview) scrollbar.grid(row=2, column=2, sticky='NS') text.config(yscrollcommand=scrollbar.set) def confirm_search1(teacher_id, text): with open('teachers.txt', 'r') as f: lines = f.readlines() # 清空文本框内容 text.delete('1.0', tk.END) for line in lines: teacher_info = line.split(',') if (not teacher_id or teacher_info[0] == teacher_id): text.insert(tk.END, line) # 教师姓名查询 def search2(window): # 创建教师姓名查询的窗口 search2_window = tk.Toplevel(window) search2_window.title("教师姓名查询") # 创建标签和文本框 name_label = tk.Label(search2_window, text="教师姓名:") name_label.grid(row=0, column=0) name_entry = tk.Entry(search2_window) name_entry.grid(row=0, column=1) # 创建“确定”按钮 confirm_button = tk.Button( search2_window, text="确定", command=lambda: confirm_search2(name_entry.get(), text) ) confirm_button.grid(row=1, column=0, pady=10) # 创建“取消”按钮 cancel_button = tk.Button( search2_window, text="取消", command=search2_window.destroy ) cancel_button.grid(row=1, column=1, pady=10) # 创建结果区域,并添加滚动条 text = tk.Text(search2_window) text.grid(row=2, columnspan=2) scrollbar = tk.Scrollbar(search2_window, command=text.yview) scrollbar.grid(row=2, column=2, sticky='NS') text.config(yscrollcommand=scrollbar.set) # 确认教师姓名查询 def confirm_search2(name, text): with open('teachers.txt', 'r') as f: lines = f.readlines() # 清空文本框内容 text.delete('1.0', tk.END) for line in lines: teacher_info = line.split(',') if (not name or teacher_info[1] == name): text.insert(tk.END, line) text.insert(tk.END, line) # 教师工作日期长短查询 import datetime def search3(window): # 创建教师工作日期长短查询的窗口 search3_window = tk.Toplevel(window) search3_window.title("教师工作日期长短查询") # 创建标签和文本框 start_date_label = tk.Label(search3_window, text="起始日期(格式:YYYY-MM-DD):") start_date_label.grid(row=0, column=0) start_date_entry = tk.Entry(search3_window) start_date_entry.grid(row=0, column=1) end_date_label = tk.Label(search3_window, text="截止日期(格式:YYYY-MM-DD):") end_date_label.grid(row=1, column=0) end_date_entry = tk.Entry(search3_window) end_date_entry.grid(row=1, column=1) # 创建“确定”按钮 confirm_button = tk.Button( search3_window, text="确定", command=lambda: confirm_search3( start_date_entry.get(), end_date_entry.get(), text ) ) confirm_button.grid(row=2, column=0, pady=10) # 创建“取消”按钮 cancel_button = tk.Button( search3_window, text="取消", command=search3_window.destroy ) cancel_button.grid(row=2, column=1, pady=10) # 创建结果区域,并添加滚动条 text = tk.Text(search3_window) text.grid(row=3, columnspan=2) scrollbar = tk.Scrollbar(search3_window, command=text.yview) scrollbar.grid(row=3, column=2, sticky='NS') text.config(yscrollcommand=scrollbar.set) # 确认教师工作日期长短查询 def confirm_search3(start_date, end_date, text): with open('teachers.txt', 'r') as f: lines = f.readlines() # 清空文本框内容 text.delete('1.0', tk.END) # 转换起始和截止日期为datetime对象 start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d') if start_date else None end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d') if end_date else None for line in lines: teacher_info = line.split(',') # 获取教师工作日期 start_date_str = teacher_info[7] # 转换教师工作日期为datetime对象 if start_date_str: start_date1 = datetime.datetime.strptime(start_date_str, '%Y-%m-%d') else: start_date1 = None if not start_date and not end_date: text.insert(tk.END, line) elif not start_date and start_date1 <= end_date: text.insert(tk.END, line) elif not end_date and start_date1 >= start_date: text.insert(tk.END, line) elif start_date1 >= start_date and start_date1 <= end_date: text.insert(tk.END, line) text.insert(tk.END, line) def search4(window): search4_window = tk.Toplevel(window) search4_window.title("教师工资范围查询") # 创建标签和文本框 min_salary_label = tk.Label(search4_window, text="最低工资:") min_salary_label.grid(row=0, column=0) min_salary_entry = tk.Entry(search4_window) min_salary_entry.grid(row=0, column=1) max_salary_label = tk.Label(search4_window, text="最高工资:") max_salary_label.grid(row=1, column=0) max_salary_entry = tk.Entry(search4_window) max_salary_entry.grid(row=1, column=1) # 创建“确定”按钮 confirm_button = tk.Button( search4_window, text="确定", command=lambda: confirm_search4( min_salary_entry.get(), max_salary_entry.get(), text, search4_window ) ) confirm_button.grid(row=2, column=0, pady=10) # 创建“取消”按钮 cancel_button = tk.Button( search4_window, text="取消", command=search4_window.destroy ) cancel_button.grid(row=2, column=1, pady=10) # 创建结果区域,并添加滚动条 text = tk.Text(search4_window) text.grid(row=3, columnspan=2) scrollbar = tk.Scrollbar(search4_window, command=text.yview) scrollbar.grid(row=3, column=2, sticky='NS') text.config(yscrollcommand=scrollbar.set) # 确认教师工资范围查询 def confirm_search4(min_salary, max_salary, text, window): with open('teachers.txt', 'r') as f: lines = f.readlines() # 清空文本框内容 text.delete('1.0', tk.END) for line in lines: teacher_info = line.strip().split(',') if len(teacher_info) <= 6: # 行数据不完整 continue salary = int(teacher_info[6]) if not min_salary and not max_salary: text.insert(tk.END, line) elif not min_salary and salary <= int(max_salary): text.insert(tk.END, line) elif not max_salary and salary >= int(min_salary): text.insert(tk.END, line) elif salary >= int(min_salary) and salary <= int(max_salary): text.insert(tk.END, line) def search5(window): search5_window = tk.Toplevel(window) search5_window.title("教师出生年月范围查询") # 创建标签和文本框 min_birth_label = tk.Label(search5_window, text="最早出生年月:") min_birth_label.grid(row=0, column=0) min_birth_entry = tk.Entry(search5_window) min_birth_entry.grid(row=0, column=1) max_birth_label = tk.Label(search5_window, text="最晚出生年月:") max_birth_label.grid(row=1, column=0) max_birth_entry = tk.Entry(search5_window) max_birth_entry.grid(row=1, column=1) # 创建“确定”按钮 confirm_button = tk.Button( search5_window, text="确定", command=lambda: confirm_search5( min_birth_entry.get(), max_birth_entry.get(), result_text, search5_window ) ) confirm_button.grid(row=2, column=0, pady=10) # 创建“取消”按钮 cancel_button = tk.Button( search5_window, text="取消", command=search5_window.destroy ) cancel_button.grid(row=2, column=1, pady=10) # 创建结果显示框 result_text = tk.Text(search5_window, height=10, width=50) result_text.grid(row=3, column=0, columnspan=2) def confirm_search5(min_birth, max_birth, result_text, window): try: min_date = datetime.datetime.strptime(min_birth, "%Y-%m-%d") max_date = datetime.datetime.strptime(max_birth, "%Y-%m-%d") with open('teachers.txt', 'r') as f: lines = f.readlines() for line in lines: teacher_info = line.split(',') ID = teacher_info[2] birthy = None if len(ID) == 18: year = int(ID[6:10]) month = int(ID[10:12]) date = int(ID[12:14]) birthy = datetime.datetime(year, month, date) if not min_date and not max_date: result_text.insert(tk.END, line) elif not min_date and birthy <= max_date: result_text.insert(tk.END, line) elif not max_date and birthy >= min_date: result_text.insert(tk.END, line) elif birthy >= min_date and birthy <= max_date: result_text.insert(tk.END, line) result_text.insert(tk.END, "教师信息查找完成!") messagebox.showinfo("提示", "搜索完成!") except ValueError: messagebox.showerror("错误", "请输入正确的日期格式!") def search6(window): search6_window = tk.Toplevel(window) search6_window.title("多条件查询教师信息") # 创建标签和文本框 teacher_id_label = tk.Label(search6_window, text="教师号:") teacher_id_label.grid(row=0, column=0) teacher_id_entry = tk.Entry(search6_window) teacher_id_entry.grid(row=0, column=1) name_label = tk.Label(search6_window, text="姓名:") name_label.grid(row=1, column=0) name_entry = tk.Entry(search6_window) name_entry.grid(row=1, column=1) start_date_label = tk.Label(search6_window, text="工作开始日期:") start_date_label.grid(row=2, column=0) start_date_entry = tk.Entry(search6_window) start_date_entry.grid(row=2, column=1) end_date_label = tk.Label(search6_window, text="工作结束日期:") end_date_label.grid(row=3, column=0) end_date_entry = tk.Entry(search6_window) end_date_entry.grid(row=3, column=1) min_salary_label = tk.Label(search6_window, text="最低工资:") min_salary_label.grid(row=4, column=0) min_salary_entry = tk.Entry(search6_window) min_salary_entry.grid(row=4, column=1) max_salary_label = tk.Label(search6_window, text="最高工资:") max_salary_label.grid(row=5, column=0) max_salary_entry = tk.Entry(search6_window) max_salary_entry.grid(row=5, column=1) min_birth_label = tk.Label(search6_window, text="最早出生年月:") min_birth_label.grid(row=6, column=0) min_birth_entry = tk.Entry(search6_window) min_birth_entry.grid(row=6, column=1) max_birth_label = tk.Label(search6_window, text="最晚出生年月:") max_birth_label.grid(row=7, column=0) max_birth_entry = tk.Entry(search6_window) max_birth_entry.grid(row=7, column=1) # 创建“确定”按钮 confirm_button = tk.Button( search6_window, text="确定", command=lambda: confirm_search6( teacher_id_entry.get(), name_entry.get(), start_date_entry.get(), end_date_entry.get(), min_salary_entry.get(), max_salary_entry.get(), min_birth_entry.get(), max_birth_entry.get(), result_text, search6_window ) ) confirm_button.grid(row=8, column=0, pady=10) # 创建“取消”按钮 cancel_button = tk.Button( search6_window, text="取消", command=search6_window.destroy ) cancel_button.grid(row=8, column=1, pady=10) # 创建结果显示框 result_text = tk.Text(search6_window, height=10, width=50) result_text.grid(row=9, column=0, columnspan=2) def confirm_search6(teacher_id, name, start_date, end_date, min_salary, max_salary, min_birth, max_birth, result_text, window): with open('teachers.txt', 'r') as f: lines = f.readlines() # 工作时间长短 start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d') if start_date else None end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d') if end_date else None # 年龄大小 min_date = datetime.datetime.strptime(min_birth, "%Y-%m-%d") max_date = datetime.datetime.strptime(max_birth, "%Y-%m-%d") for line in lines: teacher_info = line.split(',') # 获取教师工作日期 start_date_str = teacher_info[7] # 获取讲师身份证号 ID = teacher_info[2] birthy = None if len(ID) == 18: year = int(ID[6:10]) month = int(ID[10:12]) date = int(ID[12:14]) birthy = datetime.datetime(year, month, date) # 转换教师工作日期为datetime对象 if start_date_str: start_date1 = datetime.datetime.strptime(start_date_str, '%Y-%m-%d') else: start_date1 = None if ( (not teacher_id or teacher_info[0] == teacher_id) and (not name or teacher_info[1] == name) and (not start_date and not end_date or (not start_date and start_date1<= end_date) or (not end_date and start_date1 >= start_date) or (start_date1>= start_date and start_date1 <= end_date) ) and (not min_salary and not max_salary or (not min_salary and teacher_info[6] <= max_salary) or (not max_salary and teacher_info[6] >= min_salary) or (teacher_info[6] >= min_salary and teacher_info[6] <= max_salary) ) and (not min_date and not max_date or (not min_date and birthy <= max_date) or (not max_date and birthy>= min_date) or (birthy >= min_date and birthy <= max_date) ) ): result_text.insert(tk.END, line) result_text.insert(tk.END, "教师信息查找完成!") messagebox.showinfo("提示", "搜索完成!") def search_teacher(): window = tk.Tk() window.title("教师信息查找") # 创建“教师编号查询”按钮 search1_button = tk.Button( text="教师编号查询", command=lambda: search1(window) ) search1_button.pack(padx=10, pady=5) # 创建“教师姓名查询”按钮 search2_button = tk.Button( text="教师姓名查询", command=lambda: search2(window) ) search2_button.pack(padx=10, pady=5) # 创建“教师工作日期长短查询”按钮 search3_button = tk.Button( text="教师工作日期长短查询", command=lambda: search3(window) ) search3_button.pack(padx=10, pady=5) # 创建“教师工资范围查询”按钮 search4_button = tk.Button( text="教师工资范围查询", command=lambda: search4(window) ) search4_button.pack(padx=10, pady=5) # 创建“教师出生年月范围查询”按钮 search5_button= tk.Button(text="教师出生年月范围查询",command=lambda: search5(window)) search5_button.pack(padx=10, pady=5) # 创建“多条件查询”按钮 search6_button = tk.Button( text="多条件查询", command=lambda: search6(window) ) search6_button.pack(padx=10, pady=5) window.mainloop() def create_window(): window = tk.Tk() window.title("教师管理系统") # 添加教师按钮 add_btn = tk.Button(window, text="添加教师信息", command=add_teacher) add_btn.pack(pady=10) # 删除教师按钮 delete_btn = tk.Button(window, text="删除教师信息", command=delete_teacher) delete_btn.pack(pady=10) # 修改教师按钮 modify_btn = tk.Button(window, text="修改教师信息", command=modify_teacher) modify_btn.pack(pady=10) # 查找教师按钮 search_btn = tk.Button(window, text="查找教师信息", command=search_teacher) search_btn.pack(pady=10) # 退出按钮 exit_btn = tk.Button(window, text="退出", command=window.quit) exit_btn.pack(pady=10) window.mainloop() if __name__ == '__main__': create_window()
运行结果如下图所示:
标签:总结,第一周,column,window,grid,tk,暑假,entry,row From: https://www.cnblogs.com/jiacheng-712/p/17504226.html