Uso del módulo tkinter en Python para implementar un sistema de gestión de información de estudiantes con una interfaz GUI

Este artículo solo tiene código, que presenta la implementación del sistema de gestión de información de los estudiantes en la interfaz GUI.

Ha sido depurado sin mayores problemas.
Si hay errores, por favor critique y corrija.

1. Importa el módulo tkinter 

import tkinter as tk
from tkinter import messagebox

2. Definir una variable global para almacenar información de los estudiantes 

# 用来存储学生信息的总列表[学号(6位)、姓名、专业、年龄(17~25)、班级(序号)、电话(11位)]
#                        [ID        Name  Major Age         Class       Telephone]
Info = []

3. Para que los datos de la información del estudiante sean persistentes, la información se puede escribir en un archivo llamado 'Student_Info.txt' aquí

# 定义一个方法用于使用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. Lea la información del archivo guardado

# 定义一个方法用于读取文件
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. Para utilizar la información del estudiante guardada cada vez que se ejecuta el programa, importe la información del archivo a la variable global

# 用于将文件里的信息存入全局变量Info中
try:
    for i in ReadTxt():
        Info.append(i)
except:
    pass

 6. Utilice una serie de métodos para verificar si la entrada está estandarizada

# 定于一个方法,用于检查学号是否规范
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. Después de completar las operaciones relevantes, habrá un mensaje rápido

# 提示信息
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. El método principal para agregar información del estudiante

# 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. El método principal para eliminar la información del estudiante

# 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. Abra el método de modificación de la ventana de información del estudiante.

# 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. El método principal para modificar la información del estudiante

# 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. El método principal para encontrar información de los estudiantes 

# 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. El método principal para mostrar toda la información del estudiante

# 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 Problema 1: problema de diseño de caracteres de formato

 >>>En el cálculo de la longitud de la cadena, las longitudes de cadena de caracteres chinos y occidentales son inconsistentes, lo que conducirá a errores de composición tipográfica <<<

Solución 1: puede personalizar la función de cálculo de cadenas y llenarla con espacios en caracteres occidentales

# 定义一个方法,用于判断是否为中文字符
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

 

Solución 2: si solo se trata de texto en chino puro, puede usar chr(12288) para completar los espacios en chino (pero las cadenas chinas y occidentales mezcladas seguirán teniendo una composición tipográfica desigual)

 14. Agregar ventana de información del estudiante

# 添加学生信息的窗口
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 Problema 2: No se puede leer el contenido del cuadro de entrada

>>> Al usar la función get(), como Txt_ID.get() no puede leer el contenido del cuadro de entrada

Solución: Al crear una ventana, defínala como una subventana de la ventana principal, si es window=tk.TK(), se ejecutará incorrectamente

window = tk.Toplevel(root)

14.2 Pregunta 3: La instrucción de comando en Tk.Button no puede ejecutar la función con parámetros

>>>Al usar el comando en Button, si el método enlazado tiene paso de parámetros, no se puede escribir así:

command = func(int a, int b)

>>> Si está escrito así, se ejecutará directamente durante la depuración, es decir, se ejecutará sin hacer clic en el botón

 Solución alternativa: use lambda

 15. Ventana para eliminar información del estudiante

# 删除学生信息的窗口
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. La ventana para modificar la información del estudiante

# 修改学生信息的窗口
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. Ingrese a la ventana para modificar la información del estudiante

# 输入修改学生信息的窗口
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. Ventana para encontrar información del estudiante

# 查找学生信息的窗口
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. Sal de la ventana del sistema de gestión

# 退出管理系统的窗口
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. Ventana principal

# 如果该文件是程序运行的主文件,将会运行以下代码
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()

>>>El siguiente es el código total<<<

# -*- 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()

 

 

 

 

Supongo que te gusta

Origin blog.csdn.net/absorb2601078490/article/details/127323470
Recomendado
Clasificación