• python使用tkinter实现学生信息管理系统(下)


    前言:

    原本我是不打算写下篇的,原因我也说了,没什么用纯粹是写着玩的,而且隔了好长时间了我也没什么印象了想要接上比较费时费力,但是我发现一件很神奇的事情,就是我觉得有用的文章没有太多人看,而这种花里胡哨的文章你们却很感兴趣,导致好多人问我要下篇,我想了想反正放假了也没什么事情,那就应广大网友的要求就把他补全吧(哈哈哈,想打我吗?)。

    参考:Python GUI项目实战(一)登录窗体的设计与实现 - 云+社区 - 腾讯云 (tencent.com)

    正文:

    我们接着上一篇的内容继续做拓展,上篇我们实现了登录,显示,关键字搜索的操作,这一篇我们来把增删改查补全,再加上一个保存的功能。至此一个简单的学生信息管理系统就都搞定了。

    老规矩先上图:

    用户名和密码在最上方,可自行修改:

    #先定义几个可能用到的常量
    user_name = "kkyyds" #凯子哥永远的神,哈哈
    password = "123456"

    查询:

     

    点击显示全部后,双击查看学生信息:

     添加:

     

    修改: 

     删除:

    保存: 

     

     

     退出保存判定,若文件未作修改则不显示,刚保存过不显示,做出修改后未保存显示选择框:

     

    明细窗体:

    增加和修改我们需要一个新的窗口进行操作,为了提高代码的可读性,我们新开一个py文件,文件名就叫明细窗口。

    1. from tkinter import *
    2. from tkinter.messagebox import showinfo
    3. from tkinter.ttk import *
    4. class DetailWindow(Toplevel):
    5. def __init__(self):
    6. super().__init__()
    7. self.config(bg="#C9C9C9")
    8. self.title("学生明细信息")
    9. self.geometry("300x450+600+150")
    10. self.resizable(0,0) # 不能改变大小
    11. self.userinfo=0
    12. # 加载控件
    13. self.setup_UI()
    14. def setup_UI(self):
    15. # 加载一个pane
    16. self.Pane_detail = PanedWindow(self,width = 290,height = 430)
    17. self.Pane_detail.place(x = 10,y = 20)
    18. self.Style02 = Style(self.Pane_detail)#指定窗体,不指定则不显示样式
    19. self.Style02.configure("TPanedwindow", background="#C9C9C9")
    20. self.Style02.configure("detail.TLabel", font=("微软雅黑", 16, "bold"), background="#C9C9C9")
    21. self.Style02.configure("detail.TButton", font=("微软雅黑", 16, "bold"))
    22. self.Style02.configure("TEntry", font=("微软雅黑", 16, "bold"), width=10)
    23. self.Style02.configure("TRadiobutton", font=("微软雅黑", 16, "bold"), background="#C9C9C9")
    24. # 添加属性
    25. # 第一排:学号
    26. self.Label_sno = Label(self.Pane_detail,text = "学号:",style="detail.TLabel")
    27. self.Label_sno.place(x=10,y=10)
    28. self.var_sno = StringVar()
    29. self.Entry_sno = Entry(self.Pane_detail,textvariable = self.var_sno,font=("微软雅黑", 16,"bold"),width = 10)
    30. self.Entry_sno.place(x=80,y=8)
    31. # 专业:
    32. self.Label_pro = Label(self.Pane_detail, text="专业:",style="detail.TLabel")
    33. self.Label_pro.place(x=10, y=60)
    34. self.var_pro = StringVar()
    35. self.Entry_pro = Entry(self.Pane_detail, textvariable=self.var_pro, font=("微软雅黑", 16, "bold"), width=13)
    36. self.Entry_pro.place(x=80, y=58)
    37. # 班级
    38. self.Label_class = Label(self.Pane_detail, text="班级:",style="detail.TLabel")
    39. self.Label_class.place(x=10, y=110)
    40. self.var_class = StringVar()
    41. self.Entry_class = Entry(self.Pane_detail, textvariable=self.var_class, font=("微软雅黑", 16, "bold"), width=7)
    42. self.Entry_class.place(x=80, y=108)
    43. # 姓名
    44. self.Label_name = Label(self.Pane_detail, text="姓名:",style="detail.TLabel")
    45. self.Label_name.place(x=10, y=160)
    46. self.var_name = StringVar()
    47. self.Entry_name = Entry(self.Pane_detail, textvariable=self.var_name, font=("微软雅黑", 16, "bold"), width=7)
    48. self.Entry_name.place(x=80, y=158)
    49. # 性别
    50. self.Label_gender = Label(self.Pane_detail,text = "性别:",style="detail.TLabel").place(x=10,y = 210)
    51. self.var_gender = IntVar()
    52. self.Radio_man = Radiobutton(self.Pane_detail,text="男",variable = self.var_gender,value = 1)
    53. self.Radio_man.place(x=80,y = 210)
    54. self.Radio_woman = Radiobutton(self.Pane_detail, text="女", variable=self.var_gender, value=2)
    55. self.Radio_woman.place(x=130, y=210)
    56. # 绩点
    57. self.Label_jidian = Label(self.Pane_detail, text="绩点:",style="detail.TLabel")
    58. self.Label_jidian.place(x=10, y=260)
    59. self.var_jidian = StringVar()
    60. self.Entry_jidian = Entry(self.Pane_detail, textvariable=self.var_jidian, font=("微软雅黑", 16, "bold"), width=7)
    61. self.Entry_jidian.place(x=80, y=258)
    62. # 手机号码
    63. self.Label_mobile = Label(self.Pane_detail, text="手机号码:",style="detail.TLabel")
    64. self.Label_mobile.place(x=10, y=310)
    65. self.var_mobile = StringVar()
    66. self.Entry_mobile = Entry(self.Pane_detail, textvariable=self.var_mobile, font=("微软雅黑", 16, "bold"), width=11)
    67. self.Entry_mobile.place(x=120, y=308)
    68. # 放置按钮
    69. self.Button_save = Button(self, text="保存", style="detail.TButton",command=self.add_student_detail)
    70. self.Button_save02 = Button(self, text="保存", style="detail.TButton",command=self.update_student_detail)
    71. def show_save(self,all_student_list):
    72. self.Button_save.place(x=80, y=388)
    73. self.all_student_list=all_student_list
    74. def tube(self,all_student_list):
    75. self.all_student_list=all_student_list
    76. def load_student_detail(self,current_student_list,flag,index):
    77. if len(current_student_list) == 0:
    78. return
    79. else:
    80. self.var_sno.set(current_student_list[0]) # 学号
    81. self.var_pro.set(current_student_list[1])
    82. self.var_class.set(current_student_list[2])
    83. self.var_name.set(current_student_list[3])
    84. if "男" in current_student_list[4]:
    85. self.var_gender.set(1)
    86. else:
    87. self.var_gender.set(2)
    88. self.var_jidian.set(current_student_list[5])
    89. self.var_mobile.set(current_student_list[6])
    90. if flag==1:
    91. # 控制控件的状态
    92. self.Button_save.place_forget()
    93. self.Entry_sno["state"] = DISABLED
    94. self.Entry_name["state"] = DISABLED
    95. self.Radio_man["state"] = DISABLED
    96. self.Radio_woman["state"] = DISABLED
    97. self.Entry_class["state"] = DISABLED
    98. self.Entry_mobile["state"] = DISABLED
    99. self.Entry_jidian["state"] = DISABLED
    100. self.Entry_pro["state"] = DISABLED
    101. else:
    102. self.index = index
    103. self.Button_save02.place(x=80, y=388)
    104. def add_student_detail(self):
    105. temp_list = []
    106. if len(str(self.Entry_sno.get()).strip()) == 0:
    107. showinfo("系统消息", "学号不能为空!")
    108. else:
    109. temp_list.append(str(self.Entry_sno.get()).strip())
    110. temp_list.append(str(self.Entry_pro.get()).strip())
    111. temp_list.append(str(self.Entry_class.get()).strip())
    112. temp_list.append(str(self.Entry_name.get()).strip())
    113. if self.var_gender.get() == 1:
    114. temp_list.append("男")
    115. else:
    116. temp_list.append("女")
    117. temp_list.append(str(self.Entry_jidian.get()).strip())
    118. temp_list.append(str(self.Entry_mobile.get()).strip())
    119. self.all_student_list.append(temp_list)
    120. self.userinfo=1
    121. # 提醒添加成功
    122. showinfo("系统消息", "学生信息添加成功")
    123. # 关闭窗体
    124. self.destroy()
    125. def update_student_detail(self):
    126. temp_list = []
    127. if len(str(self.Entry_sno.get()).strip()) == 0:
    128. showinfo("系统消息", "学号不能为空!")
    129. else:
    130. temp_list.append(str(self.Entry_sno.get()).strip())
    131. temp_list.append(str(self.Entry_pro.get()).strip())
    132. temp_list.append(str(self.Entry_class.get()).strip())
    133. temp_list.append(str(self.Entry_name.get()).strip())
    134. if self.var_gender.get() == 1:
    135. temp_list.append("男")
    136. else:
    137. temp_list.append("女")
    138. temp_list.append(str(self.Entry_jidian.get()).strip())
    139. temp_list.append(str(self.Entry_mobile.get()).strip())
    140. self.all_student_list[self.index]=temp_list
    141. self.userinfo = 1
    142. # 提醒添加成功
    143. showinfo("系统消息", "学生信息修改成功")
    144. # 关闭窗体
    145. self.destroy()
    146. if __name__ == '__main__':
    147. this_window = DetailWindow()
    148. this_window.mainloop()

    主界面:

    主界面要连接明细窗口,使用import导入明细窗口,在进行函数的书写。为了你们能够看的清晰直观,我就不进行代码的拆分了,直接全部放出,防止有的地方你们连接不上。

    1. import tkinter as tk
    2. from tkinter import messagebox
    3. from tkinter.messagebox import showinfo, askyesno
    4. from tkinter.ttk import Style, PanedWindow, Button, LabelFrame, Treeview
    5. import pandas as pd
    6. from PIL import Image, ImageTk
    7. from tkinter import Frame
    8. import time
    9. import 明细窗口
    10. #先定义几个可能用到的常量
    11. user_name = "kkyyds"
    12. password = "123456"
    13. LEFT = "left"
    14. RIGHT = "right"
    15. TOP = "top"
    16. BOTTOM = "bottom"
    17. Song = '宋体'
    18. Microsoft = '微软雅黑'
    19. class Root: # 这是第一个页面
    20. def __init__(self, window):
    21. self.window = window
    22. self.window.title("教学管理系统")
    23. self.window.geometry("1000x600+250+100")
    24. self.window.resizable(0,0) # 窗体大小不允许变,两个参数分别代表x轴和y轴
    25. self.frame = Frame(self.window)
    26. self.frame.config(bg="#C9C9C9")
    27. self.frame.pack()
    28. self.img_lable()
    29. self.lable()
    30. self.clock()
    31. self.login()
    32. def lable(self):
    33. tk.Label(self.frame, text="新版教学管理系统", font=(Song, 20), bg="#C9C9C9").place(relx=0.73, rely=0.45, relheight=0.05,
    34. relwidth=0.25)
    35. tk.Label(self.frame, text="清华大学", font=("楷体", 30), bg="#C9C9C9").place(relx=0.73, rely=0.35, relheight=0.08,
    36. relwidth=0.25)
    37. tk.Label(self.frame, text="作者: CSDN@星空的你", font=("楷体", 10), bg="#C9C9C9").place(relx=0.73, rely=0.85,
    38. relheight=0.08, relwidth=0.25)
    39. def img_lable(self):
    40. # photo1=tk.PhotoImage(file = '大学.png')#仅支持png和gif
    41. img2 = Image.open("大学.jpg")
    42. img1 = Image.open("清华大学.png")
    43. img2 = img2.resize((700, 600)) # 规定图片大小
    44. img1 = img1.resize((300, 200)) # 规定图片大小
    45. photo2 = ImageTk.PhotoImage(img2) # 使用神器PIL库可以设置照片大小并且可以支持jpg格式等
    46. photo1 = ImageTk.PhotoImage(img1) # 使用神器PIL库可以设置照片大小并且可以支持jpg格式等
    47. label2 = tk.Label(self.frame, image=photo2, borderwidth=0)
    48. label1 = tk.Label(self.frame, image=photo1, borderwidth=0)
    49. label2.img = photo2 # to keep the reference for the image.不保存会显示空白
    50. label1.img = photo1 # to keep the reference for the image.不保存会显示空白
    51. label2.grid(row=0, column=0)
    52. label1.grid(row=0, column=1, sticky="n")#n就是北North表示最上方
    53. def login(self):
    54. # 将俩个标签分别布置在第一行、第二行
    55. tk.Label(self.frame, text="账号:", font=(Song, 15), bg="#C9C9C9").place(relx=0.7, rely=0.55, relheight=0.04,
    56. relwidth=0.1)
    57. tk.Label(self.frame, text="密码:", font=(Song, 15), bg="#C9C9C9").place(relx=0.7, rely=0.62, relheight=0.04,
    58. relwidth=0.1)
    59. # 创建输入框控件
    60. self.e1 = tk.Entry(self.frame)
    61. # 以 * 的形式显示密码
    62. self.e2 = tk.Entry(self.frame, show='*')
    63. self.e1.place(relx=0.8, rely=0.55, relheight=0.04, relwidth=0.18)
    64. self.e2.place(relx=0.8, rely=0.62, relheight=0.04, relwidth=0.18)
    65. tk.Button(self.frame, text="登录", width=20, command=self.check).place(relx=0.7, rely=0.7, relheight=0.06,
    66. relwidth=0.1)
    67. tk.Button(self.frame, text="退出", width=20, command=self.window.quit).place(relx=0.9, rely=0.7, relheight=0.06,
    68. relwidth=0.1)
    69. def check(self):
    70. if self.e1.get() == user_name and self.e2.get() == password:
    71. messagebox.showinfo(title="登陆成功", message=f"欢迎回来,{user_name}!")
    72. self.frame.destroy()
    73. Home(self.window)
    74. return True
    75. else:
    76. messagebox.showwarning(title="登录失败", message="账号或密码错误")
    77. self.e2.delete(0, tk.END)
    78. return False
    79. def clock(self):
    80. # 获取时间的函数
    81. def gettime():
    82. # 获取当前时间
    83. dstr.set(time.strftime("%H:%M:%S"))
    84. # 每隔 1s 调用一次 gettime()函数来获取时间
    85. self.frame.after(1000, gettime)
    86. # 生成动态字符串
    87. dstr = tk.StringVar()
    88. # 利用 textvariable 来实现文本变化
    89. tk.Label(self.frame, textvariable=dstr, fg='green', font=("微软雅黑", 10), bg="#C9C9C9").place(relx=0.9, rely=0.93,
    90. relheight=0.08,
    91. relwidth=0.1)
    92. tk.Label(self.frame, text="time:", fg='green', font=("微软雅黑", 10), bg="#C9C9C9").place(relx=0.875, rely=0.93,
    93. relheight=0.08,
    94. relwidth=0.05)
    95. # 调用生成时间的函数
    96. gettime()
    97. action_flag=1
    98. class Home():
    99. def __init__(self, window):
    100. self.window = window
    101. self.window.title(f"当前管理员为{user_name}")
    102. self.setup_UI()
    103. self.readExcel()
    104. self.query_result_list = []
    105. self.flag = action_flag
    106. self.update=0
    107. self.window.protocol("WM_DELETE_WINDOW", self.close_window)
    108. def readExcel(self):
    109. self.head=pd.read_excel("学生信息.xlsx").columns.tolist()
    110. df = pd.read_excel("学生信息.xlsx",usecols="A:G",dtype=str)
    111. self.all_student_list = df.values.tolist()#把每一行存入一个列表再把每个列表存入列表
    112. def del_Entry_content(self):
    113. self.Entry_sno.delete(0, tk.END)
    114. self.Entry_name.delete(0, tk.END)
    115. self.Entry_profess.delete(0, tk.END)
    116. self.Entry_class.delete(0, tk.END)
    117. def show_all(self):
    118. self.clear_Tree()
    119. # 把所有条件文本框清空
    120. self.Entry_sno.delete(0, tk.END)
    121. self.Entry_name.delete(0, tk.END)
    122. self.Entry_profess.delete(0, tk.END)
    123. self.Entry_class.delete(0, tk.END)
    124. self.load_treeview(self.all_student_list)
    125. def load_treeview(self,current_list):
    126. for index in range(len(current_list)):
    127. self.Tree.insert("", index, values=(current_list[index][0],
    128. current_list[index][1],
    129. current_list[index][2],
    130. current_list[index][3],
    131. current_list[index][4],
    132. current_list[index][5],
    133. current_list[index][6]))
    134. def get_query_result(self):
    135. query_condition = []
    136. query_condition.append(self.Entry_sno.get().strip()) # 采集学号信息
    137. query_condition.append(self.Entry_name.get().strip()) # 采集姓名信息
    138. query_condition.append(self.Entry_profess.get().strip())
    139. query_condition.append(self.Entry_class.get().strip())
    140. # 遍历List获取符合条件的学生信息
    141. for item in self.all_student_list:
    142. if query_condition[0] in item[0] and query_condition[1] in item[3] and \
    143. query_condition[2] in item[1] and query_condition[3] in item[2]:
    144. # 满足条件的学生
    145. self.query_result_list.append(item)
    146. # 把结果加载的TreeView中
    147. self.clear_Tree()
    148. self.load_treeview(self.query_result_list)
    149. self.query_result_list.clear()
    150. def clear_Tree(self):
    151. for i in self.Tree.get_children():
    152. self.Tree.delete(i)
    153. def setup_UI(self):
    154. # 设定Style
    155. self.Style01 = Style()
    156. self.Style01.configure("TPanedwindow", background="#C9C9C9")
    157. self.Style01.configure("TButton", width=10, font=(Song, 15,))
    158. # 上边:labe
    159. self.Pane_top = PanedWindow(self.window,width=980, height=85, style="TPanedwindow").place(x=10, y=5)
    160. tk.Label(self.Pane_top, text="学生信息管理系统", bg='#C9C9C9', font=("微软雅黑", 40), width=30).place(x=15, y=10)
    161. # 左边:按钮区域,创建一个容器
    162. self.Pane_left = PanedWindow(self.window,width=195, height=500, style="TPanedwindow").place(x=10, y=95) # 这种写法下方是使用绝对距离
    163. self.Pane_right = PanedWindow(self.window,width=780, height=500, style="TPanedwindow")
    164. self.Pane_right.place(x=210, y=95) # 这种写法下方是相对距离,明明写法含义都一样,结果却不一样简直莫名其妙。。。不是frame的原因,因为你把它挪上去在结尾添加下方frame位置会改变
    165. # 添加左边按钮
    166. self.Button_add = Button(self.Pane_left, text="添加学生", style="TButton",command=self.add_student).place(x=50, y=120)
    167. self.Button_update = Button(self.Pane_left, text="修改学生", style="TButton",command=self.update_student).place(x=50, y=160)
    168. self.Button_delete = Button(self.Pane_left, text="删除学生", style="TButton",command=self.delete_student).place(x=50, y=200)
    169. self.Button_modify = Button(self.Pane_left, text="保存文件", style="TButton",command=self.save_excel).place(x=50, y=260)
    170. # 添加右边按钮
    171. # LabelFrame
    172. self.LabelFrame_query = LabelFrame(self.Pane_right, text="学生信息查询", width=770, height=40)
    173. self.LabelFrame_query.place(x=5, y=5)
    174. # 添加控件
    175. y1 = 1
    176. y2 = -2
    177. self.Label_sno = tk.Label(self.LabelFrame_query, text="学号:")
    178. self.Label_sno.place(x=5, y=y1)
    179. self.Entry_sno = tk.Entry(self.LabelFrame_query, width=12)
    180. self.Entry_sno.place(x=40, y=y2)
    181. self.Label_name = tk.Label(self.LabelFrame_query, text="姓名:")
    182. self.Label_name.place(x=125, y=y1)
    183. self.Entry_name = tk.Entry(self.LabelFrame_query, width=12)
    184. self.Entry_name.place(x=160, y=y2)
    185. self.Label_profess = tk.Label(self.LabelFrame_query, text="专业:")
    186. self.Label_profess.place(x=245, y=y1)
    187. self.Entry_profess = tk.Entry(self.LabelFrame_query, width=14)
    188. self.Entry_profess.place(x=280, y=y2)
    189. self.Label_class = tk.Label(self.LabelFrame_query, text="班级:")
    190. self.Label_class.place(x=380, y=y1)
    191. self.Entry_class = tk.Entry(self.LabelFrame_query, width=14)
    192. self.Entry_class.place(x=415, y=y2)
    193. self.Button_query = tk.Button(self.LabelFrame_query, text="查询", width=4,command=self.get_query_result)
    194. self.Button_query.place(x=520, y=y1 - 9)
    195. self.Button_query = tk.Button(self.LabelFrame_query, text="清除", width=4, command=self.del_Entry_content)
    196. self.Button_query.place(x=560, y=y1 - 9)
    197. self.Button_all = tk.Button(self.LabelFrame_query, text="清空全部", width=8, command=self.clear_Tree)
    198. self.Button_all.place(x=630, y=y2 - 8)
    199. self.Button_all = tk.Button(self.LabelFrame_query, text="显示全部", width=8,command=self.show_all)
    200. self.Button_all.place(x=700, y=y2 - 8)
    201. # 添加TreeView控件
    202. self.Tree = Treeview(self.Pane_right, columns=("sno", "专业", "班级", "names",
    203. "gender", "绩点", "mobile"),
    204. show="headings", height=21)
    205. # 设置每一个列的宽度和对齐的方式
    206. self.Tree.column("sno", width=120, anchor="center")
    207. self.Tree.column("names", width=100, anchor="center")
    208. self.Tree.column("gender", width=70, anchor="center")
    209. self.Tree.column("mobile", width=125, anchor="center")
    210. self.Tree.column("专业", width=140, anchor="center")
    211. self.Tree.column("班级", width=140, anchor="center")
    212. self.Tree.column("绩点", width=70, anchor="center")
    213. # 设置每个列的标题
    214. self.Tree.heading("sno", text="学号")
    215. self.Tree.heading("names", text="姓名")
    216. self.Tree.heading("gender", text="性别")
    217. self.Tree.heading("mobile", text="手机号码")
    218. self.Tree.heading("专业", text="专业")
    219. self.Tree.heading("班级", text="班级")
    220. self.Tree.heading("绩点", text="绩点")
    221. self.Tree.place(x=5, y=50)
    222. self.Tree.bind("<Double-1>", self.view_student)
    223. def show_window(self):
    224. self.detail_window = 明细窗口.DetailWindow()
    225. def add_student(self):
    226. self.show_window()
    227. self.detail_window.show_save(self.all_student_list)
    228. self.window.wait_window(self.detail_window)#等待窗口被销毁
    229. if self.detail_window.userinfo == 1:
    230. self.show_all()
    231. self.update=1
    232. else:
    233. return
    234. def view_student(self,event):#此处event不可省略,删除后果自负
    235. # 获取Tree表格双击某一行的数据,selection()如果没有指定参数,则表明以列表形式返回所有的item
    236. item = self.Tree.selection()[0] # 获取双击某一行的项目标识符
    237. #一行数据所组成的列表
    238. current_student_list = self.Tree.item(item, "values")
    239. self.show_window()
    240. self.detail_window.load_student_detail(current_student_list,1,-1)
    241. def update_student(self):
    242. try:
    243. item = self.Tree.selection()[0] # 获取双击某一行的项目标识符
    244. except:
    245. showinfo("系统消息", "请选择要修改的学生")
    246. return
    247. # 一行数据所组成的列表
    248. current_student_list = self.Tree.item(item, "values")
    249. # 遍历获得完整学生明细信息
    250. index=self.all_student_list.index(list(current_student_list))
    251. self.show_window()
    252. self.detail_window.tube(self.all_student_list)
    253. self.detail_window.load_student_detail(current_student_list,2,index)
    254. self.window.wait_window(self.detail_window)#等待窗口被销毁
    255. if self.detail_window.userinfo == 1:
    256. self.show_all()
    257. self.update=1
    258. else:
    259. return
    260. def delete_student(self):
    261. try:
    262. item = self.Tree.selection()[0] # 获取双击某一行的项目标识符
    263. except:
    264. showinfo("系统消息", "请选择要删除的学生")
    265. return
    266. # 一行数据所组成的列表
    267. current_student_list = self.Tree.item(item, "values")
    268. # 遍历获得完整学生明细信息
    269. index=self.all_student_list.index(list(current_student_list))
    270. # 询问是否删除
    271. choose = askyesno("删除确认", "确定要删除该学生【学号:" + current_student_list[0] + ",姓名:" +
    272. current_student_list[3] + "】的信息吗?")
    273. if choose:
    274. # 执行删除动作
    275. del self.all_student_list[index]
    276. self.show_all()
    277. showinfo("系统消息", "删除成功!")
    278. self.update=1
    279. else:
    280. return
    281. def save_excel(self):
    282. if (self.update == 0):
    283. showinfo("系统消息", "当前文件未作修改,无需保存")
    284. return
    285. try:
    286. dic = {}
    287. for i in range(0, len(self.head)):
    288. lst = []
    289. for student in self.all_student_list:
    290. lst.append(student[i])
    291. dic[self.head[i]] = lst
    292. df = pd.DataFrame(dic)
    293. df.to_excel("学生信息.xlsx", index=False)
    294. # 提醒
    295. showinfo("系统消息", "保存成功")
    296. self.update=0
    297. except:
    298. showinfo("系统消息", "写入文件出现异常")
    299. def close_window(self):
    300. if(self.update==0):
    301. self.window.destroy()
    302. return
    303. # 给用户提示:是否要保存数据
    304. choose = askyesno("关闭前提醒", "关闭窗体前是否要将修改写入文件")
    305. if choose:
    306. try:
    307. dic = {}
    308. for i in range(0, len(self.head)):
    309. lst = []
    310. for student in self.all_student_list:
    311. lst.append(student[i])
    312. dic[self.head[i]] = lst
    313. df = pd.DataFrame(dic)
    314. df.to_excel("学生信息.xlsx", index=False)
    315. # 提醒
    316. showinfo("系统消息", "所有的修改已经写入到文件")
    317. # 关闭
    318. self.window.destroy()
    319. except:
    320. showinfo("系统消息", "写入文件出现异常")
    321. else:
    322. self.window.destroy()
    323. if __name__ == '__main__':
    324. root = tk.Tk()
    325. Root(root)
    326. root.mainloop()

    需要项目文件吗?

    tkinter版教务系统-Python文档类资源-CSDN文库

    如果收费了,记得跟我说,我把它调回来。

    这么善良的作者值得你的点赞和收藏吗?

  • 相关阅读:
    重读《纳瓦尔宝典》精彩语录及感悟篇(二)
    [答疑]角色和状态的区别
    前端模块化导入导出
    大一学生HTML期末作业_ 季奥林匹克运动会 8页 无js 带表单 带报告5200字
    Leetcode 289. Game of Life
    基于pam实现的批量执行命令工具-Cyberark
    Vuex源码分析,实现简易版的Vuex
    哪种网站适合物理服务器
    动态规划(蓝桥杯 C++ 题目 代码 注解)
    面试官这一套 Framework 连环炮;看看你能撑到第几步?
  • 原文地址:https://blog.csdn.net/Starry_error/article/details/125331254