Python使用tkinter模块实现GUI界面的学生信息管理系统流程分步详解
作者:Marshal_A
本文只有代码,介绍了有关GUI界面的学生信息管理系统的实现。
已经过调试没有很大问题。
如有错误,还请批评指正。
1.导入tkinter模块
import tkinter as tk from tkinter import messagebox
2.定义一个全局变量储存学生信息
# 用来存储学生信息的总列表[学号(6位)、姓名、专业、年龄(17~25)、班级(序号)、电话(11位)] # [ID Name Major Age Class Telephone] Info = []
3.为了使学生信息的数据持久化,可以将信息写入文件此处命名为'Student_Info.txt'
# 定义一个方法用于使用w模式写入文件:传入已经存好变更好信息的列表,然后遍历写入txt文档 def WriteTxt_w_Mode(Student_List): # w:只写入模式,文件不存在则建立,将文件里边的内容先删除再写入 with open("Student_Info.txt","w",encoding="utf-8") as f: for i in range(0,len(Student_List)): Info_dict = Student_List[i] # 最后一行不写入换行符'\n' if i == len(Student_List) - 1: f.write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}".format \ (Info_dict["ID"],Info_dict["Name"],Info_dict["Major"],Info_dict["Age"],Info_dict["Class"],Info_dict["Telephone"])) else: f.write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n".format \ (Info_dict["ID"],Info_dict["Name"],Info_dict["Major"],Info_dict["Age"],Info_dict["Class"],Info_dict["Telephone"]))
4.读取保存的文件信息
# 定义一个方法用于读取文件 def ReadTxt() -> list: # 临时列表 Temp_List1 = [] Temp_List2 = [] # 打开同目录下的文件 f = open("./Student_Info.txt",'r', encoding="utf-8") # 遍历,读取文件每一行信息 for i in f: a = str(i) b = a.replace('\n', '') Temp_List1.append(b.split("\t")) # 将读写的信息并入临时列表 while len(Temp_List2) < len(Temp_List1): for j in range(0,len(Temp_List1)): ID = Temp_List1[j][0] Name = Temp_List1[j][1] Major = Temp_List1[j][2] Age = Temp_List1[j][3] Class = Temp_List1[j][4] Telephone = Temp_List1[j][5] Info_dict = {"ID":ID, "Name":Name, "Major":Major, "Age":Age, "Class":Class, "Telephone":Telephone } Temp_List2.append(Info_dict) # 关闭文件 f.close() # 将含有学生信息的临时列表返回 return Temp_List2
5.为了使每次运行程序可以使用保存的学生信息,将文件信息导入到全局变量里
# 用于将文件里的信息存入全局变量Info中 try: for i in ReadTxt(): Info.append(i) except: pass
6.用一系列方法检查输入是否规范
# 定于一个方法,用于检查学号是否规范 def is_ID(ID): return len(ID) == 6 and ID.isdigit() # 定于一个方法,用于检查年龄是否规范 def is_Age(Age): return Age.isdigit() and 17 <= int(Age) and int(Age) <= 25 # 定于一个方法,用于检查班级是否规范 def is_Class(Class): return Class.isdigit() and int(Class) > 0 # 定于一个方法,用于检查电话是否规范 def is_Telephone(Telephone): return len(Telephone) == 11 and Telephone.isdigit()
7.完成相关操作后将有提示信息
# 提示信息 def Tip_Add(): messagebox.showinfo("提示信息","添加成功") def Tip_Search(): messagebox.showinfo("提示信息","查询成功") def Tip_Del(): messagebox.showinfo("提示信息","删除成功") def Tip_Mod(): messagebox.showinfo("提示信息","修改成功") def Tip_Add_ID_Repeat(): messagebox.showinfo("提示信息","此学号已经存在,请勿重复添加!") def Tip_Del_ID_None(): messagebox.showinfo("提示信息","此学号不存在,请重新输入!") def Tip_Search_None(): messagebox.showinfo("提示信息","未查询到有关学生信息!") def Tip_Add_ID(): messagebox.showinfo("提示信息","学号格式有误,请重新输入!") def Tip_Add_Age(): messagebox.showinfo("提示信息","年龄格式有误,请重新输入!") def Tip_Add_Class(): messagebox.showinfo("提示信息","班级格式有误,请重新输入!") def Tip_Add_Telephone(): messagebox.showinfo("提示信息","电话格式有误,请重新输入!")
8.添加学生信息的主方法
# 1.添加学生信息的主方法 def Add_Student_Info(ID, Name, Major, Age, Class, Telephone): # 导入信息 global Info # 检查输入信息是否规范 if not is_ID(ID): Tip_Add_ID() return for i in Info: if ID == i['ID']: Tip_Add_ID_Repeat() return if not is_Age(Age): Tip_Add_Age() return if not is_Class(Class): Tip_Add_Class() return if not is_Telephone(Telephone): Tip_Add_Telephone() return # 用字典整合学生信息 Info_dict = {'ID':ID, 'Name':Name, 'Major':Major, 'Age':Age, 'Class':Class, 'Telephone':Telephone} # 将字典存入总列表 Info.append(Info_dict) # 添加成功 Tip_Add() # 将信息写入文件 WriteTxt_w_Mode(Info)
9.删除学生信息的主方法
# 2.删除学生信息的主方法 def Del_Student_Info(ID): # 检查输入信息是否规范 if not is_ID(ID): Tip_Add_ID() return # 用于指示是否删除的状态指标 Flag = True # 导入信息 global Info # 遍历,删除学生信息 for i in Info: if ID == i["ID"]: Info.remove(i) Flag = False break if Flag: Tip_Del_ID_None() return # 删除成功 Tip_Del() # 将删除后的信息写入文件 WriteTxt_w_Mode(Info)
10.打开修改学生信息窗口的方法
# 3.修改学生信息,ID符合条件则打开修改学生信息的窗口 def Mod_Student_Info(ID): if not is_ID(ID): Tip_Add_ID() return # 用于指示是否打开窗口的指标 Flag = True global Info for i in Info: if ID == i["ID"]: Window_Mod_Input(ID) Flag = False break if Flag: Tip_Del_ID_None() return
11.修改学生信息的主方法
# 3.修改学生信息的主方法 def Mod_Student_Info_1(ID, Name, Major, Age, Class, Telephone): # 检查输入信息是否规范 if not is_ID(ID): Tip_Add_ID() return if not is_Age(Age): Tip_Add_Age() return if not is_Class(Class): Tip_Add_Class() return if not is_Telephone(Telephone): Tip_Add_Telephone() return # 导入信息 global Info # 遍历,修改学生信息 for i in Info: if i["ID"] == ID: i["Name"] = Name i["Major"] = Major i["Age"] = Age i["Class"] = Class i["Telephone"] = Telephone # 修改成功 Tip_Mod() # 将修改后的信息写入文件 WriteTxt_w_Mode(Info)
12.查找学生信息的主方法
# 4.查找学生信息的主方法 def Search_Student_Info(ID, Name, Major, Age, Class, Telephone): # 检查输入是否规范,规范的和空字符串通过 if len(ID) != 0 and not is_ID(ID): Tip_Add_ID() return if len(Age) != 0 and not is_Age(Age): Tip_Add_Age() return if len(Class) != 0 and not is_Class(Class): Tip_Add_Class() return if len(Telephone) != 0 and not is_Telephone(Telephone): Tip_Add_Telephone() return # 导入信息 global Info # 临时列表 List = [] # 用来指示是否查找到学生的信息指标 Flag = False # 遍历,根据输入的部分信息找到符合条件的学生 for i in Info: if (i["ID"]==ID or len(ID)==0)and \ (i["Name"]==Name or len(Name)==0)and \ (i["Major"]==Major or len(Major)==0)and \ (i["Age"]==Age or len(Age)==0)and \ (i["Class"]==Class or len(Class)==0)and \ (i["Telephone"]==Telephone or len(Telephone)==0)and \ (len(ID)!=0 or len(Name)!=0 or len(Major)!=0 or len(Age)!=0 or len(Class)!=0 or len(Telephone)!=0 ): List.append(i) if len(List) != 0: Flag = True # 在主界面打印符合条件学生信息 Print_Student_Info(List) # 是否查找成功 if Flag: Tip_Search() else: Tip_Search_None()
13.显示所有学生信息的主方法
# 5.显示所有学生的主方法 def Print_Student_Info(Student_Info): # 定义一个字符串用于存储想要输出显示的内容 str_out = "" # 学生信息为空将返回 if Student_Info is None: result.set("学生信息为空") return if len(Student_Info) == 0: result.set("无学生信息") return # 学生信息不为空 if len(Student_Info) != 0: str_out += "学生信息如下:\n" # 显示信息标题 str_out += ("{:^7}".format("学生学号") + "{:^7}".format("学生姓名") + "{:^7}".format("学生专业") + "{:^7}".format("学生年龄") + "{:^5}".format("班级") + "{:^9}".format("电话号码") + "\n") for i in range(0, len(Student_Info)): # 格式化字符串 str_out += ("{:^}".format(Student_Info[i].get("ID")) + ' '*(11-Len_Str(Student_Info[i].get("ID"))) + "{:^}".format(Student_Info[i].get("Name")) +' '*(11-Len_Str(Student_Info[i].get("Name")))+ "{:^}".format(Student_Info[i].get("Major")) +' '*(13-Len_Str(Student_Info[i].get("Major")))+ "{:^}".format(Student_Info[i].get("Age")) +' '*(10-Len_Str(Student_Info[i].get("Age")))+ "{:^}".format(Student_Info[i].get("Class")) +' '*(5-Len_Str(Student_Info[i].get("Class")))+ "{:^}".format(Student_Info[i].get("Telephone")) +' '*(11-Len_Str(Student_Info[i].get("Telephone")))+ "\n") # 在主界面显示学生信息 result.set(str_out)
13.1 问题一:格式化字符排版问题
>>>在字符串长度的计算中,中文和西文的字符串长度不一致,这会导致排版出错<<<
解决方法一:可以自定义字符串计算函数,用西文空格填补
# 定义一个方法,用于判断是否为中文字符 def is_Chinese(ch): if ch >= '\u4e00' and ch <='\u9fa5': return True else: return False # 定义一个方法,用于计算中西文混合字符串的字符串长度 def Len_Str(string): count = 0 for line in string: if is_Chinese(line): count = count + 2 else: count = count + 1 return count
解决方法二:如果只是纯中文文本,可以用chr(12288),用中文空格填补(但在中西文混合的字符串仍然会出现排版不齐的情况)
14.添加学生信息的窗口
# 添加学生信息的窗口 def Window_Add(): # 实例化对象,创建root的子窗口window window = tk.Toplevel(root) # 窗口名字 window.title("添加学生信息") # 窗口大小 window.geometry('500x500') # 关于学号的 label 和 entry Txt_ID = tk.StringVar() Txt_ID.set("") Label_Line1 = tk.Label(window, text = "学 号 (6 位):", font = ('Arial', 10), width = 15).place(x = 75, y = 50, anchor = 'nw') Entry_Line1 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_ID, width = 20) Entry_Line1.place(x = 200, y = 50, anchor = 'nw') # 关于姓名的 label 和 entry Txt_Name = tk.StringVar() Txt_Name.set("") Label_Line2 = tk.Label(window, text = "姓 名:", font = ('Arial', 10), width = 15).place(x = 75, y = 100, anchor = 'nw') Entry_Line2 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Name, width = 20) Entry_Line2.place(x = 200, y = 100, anchor = 'nw') # 关于专业的 label 和 entry Txt_Major = tk.StringVar() Txt_Major.set("") Label_Line3 = tk.Label(window, text = "专 业:", font = ('Arial', 10), width = 15).place(x = 75, y = 150, anchor = 'nw') Entry_Line3 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Major, width = 20) Entry_Line3.place(x = 200, y = 150, anchor = 'nw') # 关于年龄的 label 和 entry Txt_Age = tk.StringVar() Txt_Age.set("") Label_Line4 = tk.Label(window, text = "年 龄 (17~21):", font = ('Arial', 10), width = 15).place(x = 75, y = 200, anchor = 'nw') Entry_Line4 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Age, width = 20) Entry_Line4.place(x = 200, y = 200, anchor = 'nw') # 关于班级的 label 和 entry Txt_Class = tk.StringVar() Txt_Class.set("") Label_Line5 = tk.Label(window, text = "班 级 (序号):", font = ('Arial', 10), width = 15).place(x = 75, y = 250, anchor = 'nw') Entry_Line5 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Class, width = 20) Entry_Line5.place(x = 200, y = 250, anchor = 'nw') # 关于电话的 label 和 entry Txt_Telephone = tk.StringVar() Txt_Telephone.set("") Label_Line6 = tk.Label(window, text = "电 话 (11位):", font = ('Arial', 10), width = 15).place(x = 75, y = 300, anchor = 'nw') Entry_Line6 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Telephone, width = 20) Entry_Line6.place(x = 200, y = 300, anchor = 'nw') # 关于"确认"组件,此处绑定函数Add_Student_Info用于添加学生信息 Button1_Yes = tk.Button(window, text = '确认', bg = 'silver', font = ('Arial', 12), command = lambda:Add_Student_Info(Txt_ID.get(), Txt_Name.get(), Txt_Major.get(),Txt_Age.get(),Txt_Class.get(),Txt_Telephone.get()), width = 10) Button1_Yes.place(x = 75, y = 400, anchor = 'nw') # 关于"取消"组件,此处绑定函数destroy()用于关闭窗口 Button2_No = tk.Button(window, text = '取消', bg = 'silver', font = ('Arial', 12), command = lambda:window.destroy(), width = 10) Button2_No.place(x = 325, y = 400, anchor = 'nw') # 窗口显示 window.mainloop()
14.1 问题二:不能读取到输入框内容
>>> 在使用get()函数时,如Txt_ID.get()不能读取到输入框的内容
解决方法:创立窗口时定义为主窗口的子窗口,如果为window=tk.TK(),将运行错误
window = tk.Toplevel(root)
14.2 问题三: Tk.Button中command指令无法执行有传参的函数
>>>在Button中使用command时,如果绑定的方法有参数的传递,不能写成这样:
command = func(int a, int b)
>>> 如果写成这样,那么在调试时会直接执行,即不用点击按钮就执行
解决方法:使用lambda
15.删除学生信息的窗口
# 删除学生信息的窗口 def Window_Del(): # 创建root的子窗口 window = tk.Toplevel(root) window.title("删除学生信息") window.geometry('500x300') # 关于学号的 label 和 entry Txt_ID = tk.StringVar() Txt_ID.set("") Label_Line1 = tk.Label(window, text = "学 号 (6 位):", font = ('Arial', 10), width = 15).place(x = 75, y = 100, anchor = 'nw') Entry_Line1 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_ID, width = 20) Entry_Line1.place(x = 200, y = 100, anchor = 'nw') # 关于"确认"组件,此处绑定函数Del_Student_Info用于删除学生信息 Button1_Yes = tk.Button(window, text = '确认', bg = 'silver', font = ('Arial', 12), command = lambda:Del_Student_Info(Txt_ID.get()), width = 10) Button1_Yes.place(x = 75, y = 200, anchor = 'nw') # 关于"取消"组件,此处绑定函数destroy()用于关闭窗口 Button2_No = tk.Button(window, text = '取消', bg = 'silver', font = ('Arial', 12), command = lambda:window.destroy(), width = 10) Button2_No.place(x = 325, y = 200, anchor = 'nw') # tk.StringVar()用于接收用户输入 result = tk.StringVar() result.set(">>>请先通过'查询学生信息'查询待删除学生的学号<<<") # 在界面中显示文本框,打印result的信息 Show_result = tk.Label(window, bg = "white", fg = "black", font = ("宋体", 12), bd = '0', anchor = 'nw', textvariable = result) Show_result.place(x = "50", y = "50", width = "400", height = "50") # 显示窗口 window.mainloop()
16.修改学生信息的窗口
# 修改学生信息的窗口 def Window_Mod(): # 创建root的子窗口 window = tk.Toplevel(root) window.title("修改学生信息") window.geometry('500x300') # 关于学号的 label 和 entry Txt_ID = tk.StringVar() Txt_ID.set("") Label_Line1 = tk.Label(window, text = "学 号 (6 位):", font = ('Arial', 10), width = 15).place(x = 75, y = 100, anchor = 'nw') Entry_Line1 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_ID, width = 20) Entry_Line1.place(x = 200, y = 100, anchor = 'nw') # 关于"确认"组件,此处绑定函数Mod_Student_Info用于修改学生信息 Button1_Yes = tk.Button(window, text = '确认', bg = 'silver', font = ('Arial', 12), command = lambda:Mod_Student_Info(Txt_ID.get()), width = 10) Button1_Yes.place(x = 75, y = 200, anchor = 'nw') # 关于"取消"组件,此处绑定函数destroy()用于关闭窗口 Button2_No = tk.Button(window, text = '取消', bg = 'silver', font = ('Arial', 12), command = lambda:window.destroy(), width = 10) Button2_No.place(x = 325, y = 200, anchor = 'nw') # 在界面中显示文本框,打印result的信息 result = tk.StringVar() result.set(">>>请先通过'查询学生信息'查询待修改学生的学号<<<") Show_result = tk.Label(window, bg = "white", fg = "black", font = ("宋体", 12), bd = '0', anchor = 'nw', textvariable = result) Show_result.place(x = "50", y = "50", width = "400", height = "50") #显示窗口 window.mainloop()
17.输入修改学生信息的窗口
# 输入修改学生信息的窗口 def Window_Mod_Input(ID): # 创建root的子窗口 window = tk.Toplevel(root) window.title("修改学生信息") window.geometry('500x500') # 关于姓名的 label 和 entry Txt_Name = tk.StringVar() Txt_Name.set("") Label_Line2 = tk.Label(window, text = "姓 名:", font = ('Arial', 10), width = 15).place(x = 75, y = 100, anchor = 'nw') Entry_Line2 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Name, width = 20) Entry_Line2.place(x = 200, y = 100, anchor = 'nw') # 关于专业的 label 和 entry Txt_Major = tk.StringVar() Txt_Major.set("") Label_Line3 = tk.Label(window, text = "专 业:", font = ('Arial', 10), width = 15).place(x = 75, y = 150, anchor = 'nw') Entry_Line3 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Major, width = 20) Entry_Line3.place(x = 200, y = 150, anchor = 'nw') # 关于年龄的 label 和 entry Txt_Age = tk.StringVar() Txt_Age.set("") Label_Line4 = tk.Label(window, text = "年 龄 (17~21):", font = ('Arial', 10), width = 15).place(x = 75, y = 200, anchor = 'nw') Entry_Line4 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Age, width = 20) Entry_Line4.place(x = 200, y = 200, anchor = 'nw') # 关于班级的 label 和 entry Txt_Class = tk.StringVar() Txt_Class.set("") Label_Line5 = tk.Label(window, text = "班 级 (序号):", font = ('Arial', 10), width = 15).place(x = 75, y = 250, anchor = 'nw') Entry_Line5 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Class, width = 20) Entry_Line5.place(x = 200, y = 250, anchor = 'nw') # 关于电话的 label 和 entry Txt_Telephone = tk.StringVar() Txt_Telephone.set("") Label_Line6 = tk.Label(window, text = "电 话 (11位):", font = ('Arial', 10), width = 15).place(x = 75, y = 300, anchor = 'nw') Entry_Line6 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Telephone, width = 20) Entry_Line6.place(x = 200, y = 300, anchor = 'nw') # 关于"确认"组件,此处绑定函数Mod_Student_Info_1用于修改学生信息 Button1_Yes = tk.Button(window, text = '确认', bg = 'silver', font = ('Arial', 12), command = lambda:Mod_Student_Info_1(ID, Txt_Name.get(), Txt_Major.get(),Txt_Age.get(),Txt_Class.get(),Txt_Telephone.get()), width = 10) Button1_Yes.place(x = 75, y = 400, anchor = 'nw') # 关于"取消"组件,此处绑定函数destroy()用于关闭窗口 Button2_No = tk.Button(window, text = '取消', bg = 'silver', font = ('Arial', 12), command = lambda:window.destroy(), width = 10) Button2_No.place(x = 325, y = 400, anchor = 'nw') # 在界面中显示文本框,打印result的信息 result = tk.StringVar() result.set(" >>>请输入修改后的信息<<<") Show_result = tk.Label(window, bg = "white", fg = "black", font = ("宋体", 12), bd = '0', anchor = 'nw', textvariable = result) Show_result.place(x = "50", y = "50", width = "400", height = "50") # 显示窗口 window.mainloop()
18.查找学生信息的窗口
# 查找学生信息的窗口 def Window_Ser(): # 创建root的子窗口 window = tk.Toplevel(root) window.title("查询学生信息") window.geometry('500x500') # 关于学号的 label 和 entry Txt_ID = tk.StringVar() Txt_ID.set("") Label_Line1 = tk.Label(window, text = "学 号 (6 位):", font = ('Arial', 10), width = 15).place(x = 75, y = 100, anchor = 'nw') Entry_Line1 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_ID, width = 20) Entry_Line1.place(x = 200, y = 100, anchor = 'nw') # 关于姓名的 label 和 entry Txt_Name = tk.StringVar() Txt_Name.set("") Label_Line2 = tk.Label(window, text = "姓 名:", font = ('Arial', 10), width = 15).place(x = 75, y = 150, anchor = 'nw') Entry_Line2 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Name, width = 20) Entry_Line2.place(x = 200, y = 150, anchor = 'nw') # 关于专业的 label 和 entry Txt_Major = tk.StringVar() Txt_Major.set("") Label_Line3 = tk.Label(window, text = "专 业:", font = ('Arial', 10), width = 15).place(x = 75, y = 200, anchor = 'nw') Entry_Line3 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Major, width = 20) Entry_Line3.place(x = 200, y = 200, anchor = 'nw') # 关于年龄的 label 和 entry Txt_Age = tk.StringVar() Txt_Age.set("") Label_Line4 = tk.Label(window, text = "年 龄 (17~21):", font = ('Arial', 10), width = 15).place(x = 75, y = 250, anchor = 'nw') Entry_Line4 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Age, width = 20) Entry_Line4.place(x = 200, y = 250, anchor = 'nw') # 关于班级的 label 和 entry Txt_Class = tk.StringVar() Txt_Class.set("") Label_Line5 = tk.Label(window, text = "班 级 (序号):", font = ('Arial', 10), width = 15).place(x = 75, y = 300, anchor = 'nw') Entry_Line5 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Class, width = 20) Entry_Line5.place(x = 200, y = 300, anchor = 'nw') # 关于电话的 label 和 entry Txt_Telephone = tk.StringVar() Txt_Telephone.set("") Label_Line6 = tk.Label(window, text = "电 话 (11位):", font = ('Arial', 10), width = 15).place(x = 75, y = 350, anchor = 'nw') Entry_Line6 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Telephone, width = 20) Entry_Line6.place(x = 200, y = 350, anchor = 'nw') # 关于"确认"组件,此处绑定函数Search_Student_Info用于修改学生信息 Button1_Yes = tk.Button(window, text = '确认', bg = 'silver', font = ('Arial', 12), command = lambda:Search_Student_Info(Txt_ID.get(), Txt_Name.get(), Txt_Major.get(),Txt_Age.get(),Txt_Class.get(),Txt_Telephone.get()), width = 10) Button1_Yes.place(x = 75, y = 400, anchor = 'nw') # 关于"取消"组件,此处绑定函数destroy()用于关闭窗口 Button2_No = tk.Button(window, text = '取消', bg = 'silver', font = ('Arial', 12), command = lambda:window.destroy(), width = 10) Button2_No.place(x = 325, y = 400, anchor = 'nw') # 在界面中显示文本框,打印result的信息 result = tk.StringVar() result.set(" >>>请输入待查找学生的部分信息(可不全填)<<<") Show_result = tk.Label(window, bg = "white", fg = "black", font = ("宋体", 12), bd = '0', anchor = 'nw', textvariable = result) Show_result.place(x = "50", y = "50", width = "400", height = "50") # 显示窗口 window.mainloop()
19.退出管理系统的窗口
# 退出管理系统的窗口 def Window_Exit(): # 创建root的子窗口 window = tk.Toplevel() window.title("退出管理系统") window.geometry('400x300') # 关于"确认"组件,此处绑定函数destroy()用于关闭主窗口 Button1_Yes = tk.Button(window, text = '确认', bg = 'silver', font = ('Arial', 12), command = lambda:root.destroy(), width = 10) Button1_Yes.place(x = 50, y = 200, anchor = 'nw') # 关于"取消"组件,此处绑定函数destroy()用于关闭窗口 Button2_No = tk.Button(window, text = '取消', bg = 'silver', font = ('Arial', 12), command = lambda:window.destroy(), width = 10) Button2_No.place(x = 250, y = 200, anchor = 'nw') # 在界面中显示文本框,打印result的信息 result = tk.StringVar() result.set(" >>>您确认离开系统吗?<<<") Show_result = tk.Label(window, bg = "white", fg = "black", font = ("宋体", 15), bd = '0', anchor = 'nw', textvariable = result) Show_result.place(x = "50", y = "75", width = "300", height = "50") # 显示窗口 window.mainloop()
20.主窗口
# 如果该文件是程序运行的主文件,将会运行以下代码 if __name__ == '__main__': # 创建主窗口 root = tk.Tk() root.title("学生信息管理系统 V1.1") root.geometry('900x400') # 关于"添加学生信息"组件,此处绑定函数Search_Student_Info用于修改学生信息 Button1_Add = tk.Button(root, text = '添 加 学 生 信 息', bg = 'silver', font = ('Arial', 12), command = Window_Add, width = 20) Button1_Add.place(x = 50, y = 50, anchor = 'nw') Button2_Del = tk.Button(root, text = '删 除 学 生 信 息', bg = 'silver', font = ('Arial', 12), command = Window_Del, width = 20) Button2_Del.place(x = 50, y = 100, anchor = 'nw') Button3_Mod = tk.Button(root, text = '修 改 学 生 信 息', bg = 'silver', font = ('Arial', 12), command = Window_Mod, width = 20) Button3_Mod.place(x = 50, y = 150, anchor = 'nw') Button4_Ser = tk.Button(root, text = '查 询 学 生 信 息', bg = 'silver', font = ('Arial', 12), command = Window_Ser, width = 20) Button4_Ser.place(x = 50, y = 200, anchor = 'nw') Button5_Show = tk.Button(root, text = '显 示 学 生 信 息', bg = 'silver', font = ('Arial', 12), command = lambda:Print_Student_Info(Info), width = 20) Button5_Show.place(x = 50, y = 250, anchor = 'nw') Button6_Exit = tk.Button(root, text = '退 出 管 理 系 统', bg = 'silver', font = ('Arial', 12), command = Window_Exit, width = 20) Button6_Exit.place(x = 50, y = 300, anchor = 'nw') result = tk.StringVar() result.set(">>>欢迎使用学生信息管理系统<<<\n Edition: V1.1 \n @Author: Marshal\nComplete Time: 2022/10/14") Show_result = tk.Label(root, bg = "white", fg = "black", font = ("宋体", 12), bd = '0', anchor = 'nw', textvariable = result) Show_result.place(x = "300", y = "50", width = "520", height = "300") root.mainloop()
>>>以下是总代码<<<
# -*- coding: utf-8 -*- """ Created on Wed Oct 5 09:34:15 2022 @author: Marshal """ # 使用tkinter模块实现GUI界面 import tkinter as tk from tkinter import messagebox # 用来存储学生信息的总列表[学号(6位)、姓名、专业、年龄(17~25)、班级(序号)、电话(11位)] # [ID Name Major Age Class Telephone] Info = [] # 定义一个方法用于使用w模式写入文件:传入已经存好变更好信息的列表,然后遍历写入txt文档 def WriteTxt_w_Mode(Student_List): # w:只写入模式,文件不存在则建立,将文件里边的内容先删除再写入 with open("Student_Info.txt","w",encoding="utf-8") as f: for i in range(0,len(Student_List)): Info_dict = Student_List[i] # 最后一行不写入换行符'\n' if i == len(Student_List) - 1: f.write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}".format \ (Info_dict["ID"],Info_dict["Name"],Info_dict["Major"],Info_dict["Age"],Info_dict["Class"],Info_dict["Telephone"])) else: f.write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\n".format \ (Info_dict["ID"],Info_dict["Name"],Info_dict["Major"],Info_dict["Age"],Info_dict["Class"],Info_dict["Telephone"])) # 定义一个方法用于读取文件 def ReadTxt() -> list: # 临时列表 Temp_List1 = [] Temp_List2 = [] # 打开同目录下的文件 f = open("./Student_Info.txt",'r', encoding="utf-8") # 遍历,读取文件每一行信息 for i in f: a = str(i) b = a.replace('\n', '') Temp_List1.append(b.split("\t")) # 将读写的信息并入临时列表 while len(Temp_List2) < len(Temp_List1): for j in range(0,len(Temp_List1)): ID = Temp_List1[j][0] Name = Temp_List1[j][1] Major = Temp_List1[j][2] Age = Temp_List1[j][3] Class = Temp_List1[j][4] Telephone = Temp_List1[j][5] Info_dict = {"ID":ID, "Name":Name, "Major":Major, "Age":Age, "Class":Class, "Telephone":Telephone } Temp_List2.append(Info_dict) # 关闭文件 f.close() # 将含有学生信息的临时列表返回 return Temp_List2 # 定于一个方法,用于检查学号是否规范 def is_ID(ID): return len(ID) == 6 and ID.isdigit() # 定于一个方法,用于检查年龄是否规范 def is_Age(Age): return Age.isdigit() and 17 <= int(Age) and int(Age) <= 25 # 定于一个方法,用于检查班级是否规范 def is_Class(Class): return Class.isdigit() and int(Class) > 0 # 定于一个方法,用于检查电话是否规范 def is_Telephone(Telephone): return len(Telephone) == 11 and Telephone.isdigit() # 定义一个方法,用于判断是否为中文字符 def is_Chinese(ch): if ch >= '\u4e00' and ch <='\u9fa5': return True else: return False # 定义一个方法,用于计算中西文混合字符串的字符串长度 def Len_Str(string): count = 0 for line in string: if is_Chinese(line): count = count + 2 else: count = count + 1 return count # 提示信息 def Tip_Add(): messagebox.showinfo("提示信息","添加成功") def Tip_Search(): messagebox.showinfo("提示信息","查询成功") def Tip_Del(): messagebox.showinfo("提示信息","删除成功") def Tip_Mod(): messagebox.showinfo("提示信息","修改成功") def Tip_Add_ID_Repeat(): messagebox.showinfo("提示信息","此学号已经存在,请勿重复添加!") def Tip_Del_ID_None(): messagebox.showinfo("提示信息","此学号不存在,请重新输入!") def Tip_Search_None(): messagebox.showinfo("提示信息","未查询到有关学生信息!") def Tip_Add_ID(): messagebox.showinfo("提示信息","学号格式有误,请重新输入!") def Tip_Add_Age(): messagebox.showinfo("提示信息","年龄格式有误,请重新输入!") def Tip_Add_Class(): messagebox.showinfo("提示信息","班级格式有误,请重新输入!") def Tip_Add_Telephone(): messagebox.showinfo("提示信息","电话格式有误,请重新输入!") # 1.添加学生信息的主方法 def Add_Student_Info(ID, Name, Major, Age, Class, Telephone): # 导入信息 global Info # 检查输入信息是否规范 if not is_ID(ID): Tip_Add_ID() return for i in Info: if ID == i['ID']: Tip_Add_ID_Repeat() return if not is_Age(Age): Tip_Add_Age() return if not is_Class(Class): Tip_Add_Class() return if not is_Telephone(Telephone): Tip_Add_Telephone() return # 用字典整合学生信息 Info_dict = {'ID':ID, 'Name':Name, 'Major':Major, 'Age':Age, 'Class':Class, 'Telephone':Telephone} # 将字典存入总列表 Info.append(Info_dict) # 添加成功 Tip_Add() # 将信息写入文件 WriteTxt_w_Mode(Info) # 2.删除学生信息的主方法 def Del_Student_Info(ID): # 检查输入信息是否规范 if not is_ID(ID): Tip_Add_ID() return # 用于指示是否删除的状态指标 Flag = True # 导入信息 global Info # 遍历,删除学生信息 for i in Info: if ID == i["ID"]: Info.remove(i) Flag = False break if Flag: Tip_Del_ID_None() return # 删除成功 Tip_Del() # 将删除后的信息写入文件 WriteTxt_w_Mode(Info) # 3.修改学生信息,ID符合条件则打开修改学生信息的窗口 def Mod_Student_Info(ID): if not is_ID(ID): Tip_Add_ID() return # 用于指示是否打开窗口的指标 Flag = True global Info for i in Info: if ID == i["ID"]: Window_Mod_Input(ID) Flag = False break if Flag: Tip_Del_ID_None() return # 3.修改学生信息的主方法 def Mod_Student_Info_1(ID, Name, Major, Age, Class, Telephone): # 检查输入信息是否规范 if not is_ID(ID): Tip_Add_ID() return if not is_Age(Age): Tip_Add_Age() return if not is_Class(Class): Tip_Add_Class() return if not is_Telephone(Telephone): Tip_Add_Telephone() return # 导入信息 global Info # 遍历,修改学生信息 for i in Info: if i["ID"] == ID: i["Name"] = Name i["Major"] = Major i["Age"] = Age i["Class"] = Class i["Telephone"] = Telephone # 修改成功 Tip_Mod() # 将修改后的信息写入文件 WriteTxt_w_Mode(Info) # 4.查找学生信息的主方法 def Search_Student_Info(ID, Name, Major, Age, Class, Telephone): # 检查输入是否规范,规范的和空字符串通过 if len(ID) != 0 and not is_ID(ID): Tip_Add_ID() return if len(Age) != 0 and not is_Age(Age): Tip_Add_Age() return if len(Class) != 0 and not is_Class(Class): Tip_Add_Class() return if len(Telephone) != 0 and not is_Telephone(Telephone): Tip_Add_Telephone() return # 导入信息 global Info # 临时列表 List = [] # 用来指示是否查找到学生的信息指标 Flag = False # 遍历,根据输入的部分信息找到符合条件的学生 for i in Info: if (i["ID"]==ID or len(ID)==0)and \ (i["Name"]==Name or len(Name)==0)and \ (i["Major"]==Major or len(Major)==0)and \ (i["Age"]==Age or len(Age)==0)and \ (i["Class"]==Class or len(Class)==0)and \ (i["Telephone"]==Telephone or len(Telephone)==0)and \ (len(ID)!=0 or len(Name)!=0 or len(Major)!=0 or len(Age)!=0 or len(Class)!=0 or len(Telephone)!=0 ): List.append(i) if len(List) != 0: Flag = True # 在主界面打印符合条件学生信息 Print_Student_Info(List) # 是否查找成功 if Flag: Tip_Search() else: Tip_Search_None() # 5.显示所有学生的主方法 def Print_Student_Info(Student_Info): # 定义一个字符串用于存储想要输出显示的内容 str_out = "" # 学生信息为空将返回 if Student_Info is None: result.set("学生信息为空") return if len(Student_Info) == 0: result.set("无学生信息") return # 学生信息不为空 if len(Student_Info) != 0: str_out += "学生信息如下:\n" # 显示信息标题 str_out += ("{:^7}".format("学生学号") + "{:^7}".format("学生姓名") + "{:^7}".format("学生专业") + "{:^7}".format("学生年龄") + "{:^5}".format("班级") + "{:^9}".format("电话号码") + "\n") for i in range(0, len(Student_Info)): # 格式化字符串 str_out += ("{:^}".format(Student_Info[i].get("ID")) + ' '*(11-Len_Str(Student_Info[i].get("ID"))) + "{:^}".format(Student_Info[i].get("Name")) +' '*(11-Len_Str(Student_Info[i].get("Name")))+ "{:^}".format(Student_Info[i].get("Major")) +' '*(13-Len_Str(Student_Info[i].get("Major")))+ "{:^}".format(Student_Info[i].get("Age")) +' '*(10-Len_Str(Student_Info[i].get("Age")))+ "{:^}".format(Student_Info[i].get("Class")) +' '*(5-Len_Str(Student_Info[i].get("Class")))+ "{:^}".format(Student_Info[i].get("Telephone")) +' '*(11-Len_Str(Student_Info[i].get("Telephone")))+ "\n") # 在主界面显示学生信息 result.set(str_out) # 添加学生信息的窗口 def Window_Add(): # 实例化对象,创建root的子窗口window window = tk.Toplevel(root) # 窗口名字 window.title("添加学生信息") # 窗口大小 window.geometry('500x500') # 关于学号的 label 和 entry Txt_ID = tk.StringVar() Txt_ID.set("") Label_Line1 = tk.Label(window, text = "学 号 (6 位):", font = ('Arial', 10), width = 15).place(x = 75, y = 50, anchor = 'nw') Entry_Line1 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_ID, width = 20) Entry_Line1.place(x = 200, y = 50, anchor = 'nw') # 关于姓名的 label 和 entry Txt_Name = tk.StringVar() Txt_Name.set("") Label_Line2 = tk.Label(window, text = "姓 名:", font = ('Arial', 10), width = 15).place(x = 75, y = 100, anchor = 'nw') Entry_Line2 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Name, width = 20) Entry_Line2.place(x = 200, y = 100, anchor = 'nw') # 关于专业的 label 和 entry Txt_Major = tk.StringVar() Txt_Major.set("") Label_Line3 = tk.Label(window, text = "专 业:", font = ('Arial', 10), width = 15).place(x = 75, y = 150, anchor = 'nw') Entry_Line3 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Major, width = 20) Entry_Line3.place(x = 200, y = 150, anchor = 'nw') # 关于年龄的 label 和 entry Txt_Age = tk.StringVar() Txt_Age.set("") Label_Line4 = tk.Label(window, text = "年 龄 (17~21):", font = ('Arial', 10), width = 15).place(x = 75, y = 200, anchor = 'nw') Entry_Line4 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Age, width = 20) Entry_Line4.place(x = 200, y = 200, anchor = 'nw') # 关于班级的 label 和 entry Txt_Class = tk.StringVar() Txt_Class.set("") Label_Line5 = tk.Label(window, text = "班 级 (序号):", font = ('Arial', 10), width = 15).place(x = 75, y = 250, anchor = 'nw') Entry_Line5 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Class, width = 20) Entry_Line5.place(x = 200, y = 250, anchor = 'nw') # 关于电话的 label 和 entry Txt_Telephone = tk.StringVar() Txt_Telephone.set("") Label_Line6 = tk.Label(window, text = "电 话 (11位):", font = ('Arial', 10), width = 15).place(x = 75, y = 300, anchor = 'nw') Entry_Line6 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Telephone, width = 20) Entry_Line6.place(x = 200, y = 300, anchor = 'nw') # 关于"确认"组件,此处绑定函数Add_Student_Info用于添加学生信息 Button1_Yes = tk.Button(window, text = '确认', bg = 'silver', font = ('Arial', 12), command = lambda:Add_Student_Info(Txt_ID.get(), Txt_Name.get(), Txt_Major.get(),Txt_Age.get(),Txt_Class.get(),Txt_Telephone.get()), width = 10) Button1_Yes.place(x = 75, y = 400, anchor = 'nw') # 关于"取消"组件,此处绑定函数destroy()用于关闭窗口 Button2_No = tk.Button(window, text = '取消', bg = 'silver', font = ('Arial', 12), command = lambda:window.destroy(), width = 10) Button2_No.place(x = 325, y = 400, anchor = 'nw') # 窗口显示 window.mainloop() # 删除学生信息的窗口 def Window_Del(): # 创建root的子窗口 window = tk.Toplevel(root) window.title("删除学生信息") window.geometry('500x300') # 关于学号的 label 和 entry Txt_ID = tk.StringVar() Txt_ID.set("") Label_Line1 = tk.Label(window, text = "学 号 (6 位):", font = ('Arial', 10), width = 15).place(x = 75, y = 100, anchor = 'nw') Entry_Line1 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_ID, width = 20) Entry_Line1.place(x = 200, y = 100, anchor = 'nw') # 关于"确认"组件,此处绑定函数Del_Student_Info用于删除学生信息 Button1_Yes = tk.Button(window, text = '确认', bg = 'silver', font = ('Arial', 12), command = lambda:Del_Student_Info(Txt_ID.get()), width = 10) Button1_Yes.place(x = 75, y = 200, anchor = 'nw') # 关于"取消"组件,此处绑定函数destroy()用于关闭窗口 Button2_No = tk.Button(window, text = '取消', bg = 'silver', font = ('Arial', 12), command = lambda:window.destroy(), width = 10) Button2_No.place(x = 325, y = 200, anchor = 'nw') # tk.StringVar()用于接收用户输入 result = tk.StringVar() result.set(">>>请先通过'查询学生信息'查询待删除学生的学号<<<") # 在界面中显示文本框,打印result的信息 Show_result = tk.Label(window, bg = "white", fg = "black", font = ("宋体", 12), bd = '0', anchor = 'nw', textvariable = result) Show_result.place(x = "50", y = "50", width = "400", height = "50") # 显示窗口 window.mainloop() # 修改学生信息的窗口 def Window_Mod(): # 创建root的子窗口 window = tk.Toplevel(root) window.title("修改学生信息") window.geometry('500x300') # 关于学号的 label 和 entry Txt_ID = tk.StringVar() Txt_ID.set("") Label_Line1 = tk.Label(window, text = "学 号 (6 位):", font = ('Arial', 10), width = 15).place(x = 75, y = 100, anchor = 'nw') Entry_Line1 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_ID, width = 20) Entry_Line1.place(x = 200, y = 100, anchor = 'nw') # 关于"确认"组件,此处绑定函数Mod_Student_Info用于修改学生信息 Button1_Yes = tk.Button(window, text = '确认', bg = 'silver', font = ('Arial', 12), command = lambda:Mod_Student_Info(Txt_ID.get()), width = 10) Button1_Yes.place(x = 75, y = 200, anchor = 'nw') # 关于"取消"组件,此处绑定函数destroy()用于关闭窗口 Button2_No = tk.Button(window, text = '取消', bg = 'silver', font = ('Arial', 12), command = lambda:window.destroy(), width = 10) Button2_No.place(x = 325, y = 200, anchor = 'nw') # 在界面中显示文本框,打印result的信息 result = tk.StringVar() result.set(">>>请先通过'查询学生信息'查询待修改学生的学号<<<") Show_result = tk.Label(window, bg = "white", fg = "black", font = ("宋体", 12), bd = '0', anchor = 'nw', textvariable = result) Show_result.place(x = "50", y = "50", width = "400", height = "50") #显示窗口 window.mainloop() # 输入修改学生信息的窗口 def Window_Mod_Input(ID): # 创建root的子窗口 window = tk.Toplevel(root) window.title("修改学生信息") window.geometry('500x500') # 关于姓名的 label 和 entry Txt_Name = tk.StringVar() Txt_Name.set("") Label_Line2 = tk.Label(window, text = "姓 名:", font = ('Arial', 10), width = 15).place(x = 75, y = 100, anchor = 'nw') Entry_Line2 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Name, width = 20) Entry_Line2.place(x = 200, y = 100, anchor = 'nw') # 关于专业的 label 和 entry Txt_Major = tk.StringVar() Txt_Major.set("") Label_Line3 = tk.Label(window, text = "专 业:", font = ('Arial', 10), width = 15).place(x = 75, y = 150, anchor = 'nw') Entry_Line3 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Major, width = 20) Entry_Line3.place(x = 200, y = 150, anchor = 'nw') # 关于年龄的 label 和 entry Txt_Age = tk.StringVar() Txt_Age.set("") Label_Line4 = tk.Label(window, text = "年 龄 (17~21):", font = ('Arial', 10), width = 15).place(x = 75, y = 200, anchor = 'nw') Entry_Line4 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Age, width = 20) Entry_Line4.place(x = 200, y = 200, anchor = 'nw') # 关于班级的 label 和 entry Txt_Class = tk.StringVar() Txt_Class.set("") Label_Line5 = tk.Label(window, text = "班 级 (序号):", font = ('Arial', 10), width = 15).place(x = 75, y = 250, anchor = 'nw') Entry_Line5 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Class, width = 20) Entry_Line5.place(x = 200, y = 250, anchor = 'nw') # 关于电话的 label 和 entry Txt_Telephone = tk.StringVar() Txt_Telephone.set("") Label_Line6 = tk.Label(window, text = "电 话 (11位):", font = ('Arial', 10), width = 15).place(x = 75, y = 300, anchor = 'nw') Entry_Line6 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Telephone, width = 20) Entry_Line6.place(x = 200, y = 300, anchor = 'nw') # 关于"确认"组件,此处绑定函数Mod_Student_Info_1用于修改学生信息 Button1_Yes = tk.Button(window, text = '确认', bg = 'silver', font = ('Arial', 12), command = lambda:Mod_Student_Info_1(ID, Txt_Name.get(), Txt_Major.get(),Txt_Age.get(),Txt_Class.get(),Txt_Telephone.get()), width = 10) Button1_Yes.place(x = 75, y = 400, anchor = 'nw') # 关于"取消"组件,此处绑定函数destroy()用于关闭窗口 Button2_No = tk.Button(window, text = '取消', bg = 'silver', font = ('Arial', 12), command = lambda:window.destroy(), width = 10) Button2_No.place(x = 325, y = 400, anchor = 'nw') # 在界面中显示文本框,打印result的信息 result = tk.StringVar() result.set(" >>>请输入修改后的信息<<<") Show_result = tk.Label(window, bg = "white", fg = "black", font = ("宋体", 12), bd = '0', anchor = 'nw', textvariable = result) Show_result.place(x = "50", y = "50", width = "400", height = "50") # 显示窗口 window.mainloop() # 查找学生信息的窗口 def Window_Ser(): # 创建root的子窗口 window = tk.Toplevel(root) window.title("查询学生信息") window.geometry('500x500') # 关于学号的 label 和 entry Txt_ID = tk.StringVar() Txt_ID.set("") Label_Line1 = tk.Label(window, text = "学 号 (6 位):", font = ('Arial', 10), width = 15).place(x = 75, y = 100, anchor = 'nw') Entry_Line1 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_ID, width = 20) Entry_Line1.place(x = 200, y = 100, anchor = 'nw') # 关于姓名的 label 和 entry Txt_Name = tk.StringVar() Txt_Name.set("") Label_Line2 = tk.Label(window, text = "姓 名:", font = ('Arial', 10), width = 15).place(x = 75, y = 150, anchor = 'nw') Entry_Line2 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Name, width = 20) Entry_Line2.place(x = 200, y = 150, anchor = 'nw') # 关于专业的 label 和 entry Txt_Major = tk.StringVar() Txt_Major.set("") Label_Line3 = tk.Label(window, text = "专 业:", font = ('Arial', 10), width = 15).place(x = 75, y = 200, anchor = 'nw') Entry_Line3 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Major, width = 20) Entry_Line3.place(x = 200, y = 200, anchor = 'nw') # 关于年龄的 label 和 entry Txt_Age = tk.StringVar() Txt_Age.set("") Label_Line4 = tk.Label(window, text = "年 龄 (17~21):", font = ('Arial', 10), width = 15).place(x = 75, y = 250, anchor = 'nw') Entry_Line4 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Age, width = 20) Entry_Line4.place(x = 200, y = 250, anchor = 'nw') # 关于班级的 label 和 entry Txt_Class = tk.StringVar() Txt_Class.set("") Label_Line5 = tk.Label(window, text = "班 级 (序号):", font = ('Arial', 10), width = 15).place(x = 75, y = 300, anchor = 'nw') Entry_Line5 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Class, width = 20) Entry_Line5.place(x = 200, y = 300, anchor = 'nw') # 关于电话的 label 和 entry Txt_Telephone = tk.StringVar() Txt_Telephone.set("") Label_Line6 = tk.Label(window, text = "电 话 (11位):", font = ('Arial', 10), width = 15).place(x = 75, y = 350, anchor = 'nw') Entry_Line6 = tk.Entry(window, show = None, font = ('宋体', 15), textvariable = Txt_Telephone, width = 20) Entry_Line6.place(x = 200, y = 350, anchor = 'nw') # 关于"确认"组件,此处绑定函数Search_Student_Info用于修改学生信息 Button1_Yes = tk.Button(window, text = '确认', bg = 'silver', font = ('Arial', 12), command = lambda:Search_Student_Info(Txt_ID.get(), Txt_Name.get(), Txt_Major.get(),Txt_Age.get(),Txt_Class.get(),Txt_Telephone.get()), width = 10) Button1_Yes.place(x = 75, y = 400, anchor = 'nw') # 关于"取消"组件,此处绑定函数destroy()用于关闭窗口 Button2_No = tk.Button(window, text = '取消', bg = 'silver', font = ('Arial', 12), command = lambda:window.destroy(), width = 10) Button2_No.place(x = 325, y = 400, anchor = 'nw') # 在界面中显示文本框,打印result的信息 result = tk.StringVar() result.set(" >>>请输入待查找学生的部分信息(可不全填)<<<") Show_result = tk.Label(window, bg = "white", fg = "black", font = ("宋体", 12), bd = '0', anchor = 'nw', textvariable = result) Show_result.place(x = "50", y = "50", width = "400", height = "50") # 显示窗口 window.mainloop() # 退出管理系统的窗口 def Window_Exit(): # 创建root的子窗口 window = tk.Toplevel() window.title("退出管理系统") window.geometry('400x300') # 关于"确认"组件,此处绑定函数destroy()用于关闭主窗口 Button1_Yes = tk.Button(window, text = '确认', bg = 'silver', font = ('Arial', 12), command = lambda:root.destroy(), width = 10) Button1_Yes.place(x = 50, y = 200, anchor = 'nw') # 关于"取消"组件,此处绑定函数destroy()用于关闭窗口 Button2_No = tk.Button(window, text = '取消', bg = 'silver', font = ('Arial', 12), command = lambda:window.destroy(), width = 10) Button2_No.place(x = 250, y = 200, anchor = 'nw') # 在界面中显示文本框,打印result的信息 result = tk.StringVar() result.set(" >>>您确认离开系统吗?<<<") Show_result = tk.Label(window, bg = "white", fg = "black", font = ("宋体", 15), bd = '0', anchor = 'nw', textvariable = result) Show_result.place(x = "50", y = "75", width = "300", height = "50") # 显示窗口 window.mainloop() # 用于将文件里的信息存入全局变量Info中 try: for i in ReadTxt(): Info.append(i) except: pass # 如果该文件是程序运行的主文件,将会运行以下代码 if __name__ == '__main__': # 创建主窗口 root = tk.Tk() root.title("学生信息管理系统 V1.1") root.geometry('900x400') # 关于"添加学生信息"组件,此处绑定函数Search_Student_Info用于修改学生信息 Button1_Add = tk.Button(root, text = '添 加 学 生 信 息', bg = 'silver', font = ('Arial', 12), command = Window_Add, width = 20) Button1_Add.place(x = 50, y = 50, anchor = 'nw') Button2_Del = tk.Button(root, text = '删 除 学 生 信 息', bg = 'silver', font = ('Arial', 12), command = Window_Del, width = 20) Button2_Del.place(x = 50, y = 100, anchor = 'nw') Button3_Mod = tk.Button(root, text = '修 改 学 生 信 息', bg = 'silver', font = ('Arial', 12), command = Window_Mod, width = 20) Button3_Mod.place(x = 50, y = 150, anchor = 'nw') Button4_Ser = tk.Button(root, text = '查 询 学 生 信 息', bg = 'silver', font = ('Arial', 12), command = Window_Ser, width = 20) Button4_Ser.place(x = 50, y = 200, anchor = 'nw') Button5_Show = tk.Button(root, text = '显 示 学 生 信 息', bg = 'silver', font = ('Arial', 12), command = lambda:Print_Student_Info(Info), width = 20) Button5_Show.place(x = 50, y = 250, anchor = 'nw') Button6_Exit = tk.Button(root, text = '退 出 管 理 系 统', bg = 'silver', font = ('Arial', 12), command = Window_Exit, width = 20) Button6_Exit.place(x = 50, y = 300, anchor = 'nw') result = tk.StringVar() result.set(">>>欢迎使用学生信息管理系统<<<\n Edition: V1.1 \n @Author: Marshal\nComplete Time: 2022/10/14") Show_result = tk.Label(root, bg = "white", fg = "black", font = ("宋体", 12), bd = '0', anchor = 'nw', textvariable = result) Show_result.place(x = "300", y = "50", width = "520", height = "300") root.mainloop()
到此这篇关于Python使用tkinter模块实现GUI界面的学生信息管理系统流程分步详解的文章就介绍到这了,更多相关Python学生信息管理系统内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!