Python使用tkinter模塊實(shí)現(xiàn)GUI界面的學(xué)生信息管理系統(tǒng)流程分步詳解
本文只有代碼,介紹了有關(guān)GUI界面的學(xué)生信息管理系統(tǒng)的實(shí)現(xiàn)。
已經(jīng)過調(diào)試沒有很大問題。
如有錯誤,還請批評指正。
1.導(dǎo)入tkinter模塊
import tkinter as tk from tkinter import messagebox
2.定義一個全局變量儲存學(xué)生信息
# 用來存儲學(xué)生信息的總列表[學(xué)號(6位)、姓名、專業(yè)、年齡(17~25)、班級(序號)、電話(11位)] # [ID Name Major Age Class Telephone] Info = []
3.為了使學(xué)生信息的數(shù)據(jù)持久化,可以將信息寫入文件此處命名為'Student_Info.txt'
# 定義一個方法用于使用w模式寫入文件:傳入已經(jīng)存好變更好信息的列表,然后遍歷寫入txt文檔
def WriteTxt_w_Mode(Student_List):
# w:只寫入模式,文件不存在則建立,將文件里邊的內(nèi)容先刪除再寫入
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)
# 關(guān)閉文件
f.close()
# 將含有學(xué)生信息的臨時列表返回
return Temp_List25.為了使每次運(yùn)行程序可以使用保存的學(xué)生信息,將文件信息導(dǎo)入到全局變量里
# 用于將文件里的信息存入全局變量Info中
try:
for i in ReadTxt():
Info.append(i)
except:
pass6.用一系列方法檢查輸入是否規(guī)范
# 定于一個方法,用于檢查學(xué)號是否規(guī)范
def is_ID(ID):
return len(ID) == 6 and ID.isdigit()
# 定于一個方法,用于檢查年齡是否規(guī)范
def is_Age(Age):
return Age.isdigit() and 17 <= int(Age) and int(Age) <= 25
# 定于一個方法,用于檢查班級是否規(guī)范
def is_Class(Class):
return Class.isdigit() and int(Class) > 0
# 定于一個方法,用于檢查電話是否規(guī)范
def is_Telephone(Telephone):
return len(Telephone) == 11 and Telephone.isdigit()7.完成相關(guān)操作后將有提示信息
# 提示信息
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("提示信息","此學(xué)號已經(jīng)存在,請勿重復(fù)添加!")
def Tip_Del_ID_None():
messagebox.showinfo("提示信息","此學(xué)號不存在,請重新輸入!")
def Tip_Search_None():
messagebox.showinfo("提示信息","未查詢到有關(guān)學(xué)生信息!")
def Tip_Add_ID():
messagebox.showinfo("提示信息","學(xué)號格式有誤,請重新輸入!")
def Tip_Add_Age():
messagebox.showinfo("提示信息","年齡格式有誤,請重新輸入!")
def Tip_Add_Class():
messagebox.showinfo("提示信息","班級格式有誤,請重新輸入!")
def Tip_Add_Telephone():
messagebox.showinfo("提示信息","電話格式有誤,請重新輸入!")8.添加學(xué)生信息的主方法
# 1.添加學(xué)生信息的主方法
def Add_Student_Info(ID, Name, Major, Age, Class, Telephone):
# 導(dǎo)入信息
global Info
# 檢查輸入信息是否規(guī)范
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
# 用字典整合學(xué)生信息
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.刪除學(xué)生信息的主方法
# 2.刪除學(xué)生信息的主方法
def Del_Student_Info(ID):
# 檢查輸入信息是否規(guī)范
if not is_ID(ID):
Tip_Add_ID()
return
# 用于指示是否刪除的狀態(tài)指標(biāo)
Flag = True
# 導(dǎo)入信息
global Info
# 遍歷,刪除學(xué)生信息
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.打開修改學(xué)生信息窗口的方法
# 3.修改學(xué)生信息,ID符合條件則打開修改學(xué)生信息的窗口
def Mod_Student_Info(ID):
if not is_ID(ID):
Tip_Add_ID()
return
# 用于指示是否打開窗口的指標(biāo)
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()
return11.修改學(xué)生信息的主方法
# 3.修改學(xué)生信息的主方法
def Mod_Student_Info_1(ID, Name, Major, Age, Class, Telephone):
# 檢查輸入信息是否規(guī)范
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
# 導(dǎo)入信息
global Info
# 遍歷,修改學(xué)生信息
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.查找學(xué)生信息的主方法
# 4.查找學(xué)生信息的主方法
def Search_Student_Info(ID, Name, Major, Age, Class, Telephone):
# 檢查輸入是否規(guī)范,規(guī)范的和空字符串通過
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
# 導(dǎo)入信息
global Info
# 臨時列表
List = []
# 用來指示是否查找到學(xué)生的信息指標(biāo)
Flag = False
# 遍歷,根據(jù)輸入的部分信息找到符合條件的學(xué)生
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
# 在主界面打印符合條件學(xué)生信息
Print_Student_Info(List)
# 是否查找成功
if Flag:
Tip_Search()
else:
Tip_Search_None()13.顯示所有學(xué)生信息的主方法
# 5.顯示所有學(xué)生的主方法
def Print_Student_Info(Student_Info):
# 定義一個字符串用于存儲想要輸出顯示的內(nèi)容
str_out = ""
# 學(xué)生信息為空將返回
if Student_Info is None:
result.set("學(xué)生信息為空")
return
if len(Student_Info) == 0:
result.set("無學(xué)生信息")
return
# 學(xué)生信息不為空
if len(Student_Info) != 0:
str_out += "學(xué)生信息如下:\n"
# 顯示信息標(biāo)題
str_out += ("{:^7}".format("學(xué)生學(xué)號") +
"{:^7}".format("學(xué)生姓名") +
"{:^7}".format("學(xué)生專業(yè)") +
"{:^7}".format("學(xué)生年齡") +
"{:^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")
# 在主界面顯示學(xué)生信息
result.set(str_out)13.1 問題一:格式化字符排版問題
>>>在字符串長度的計算中,中文和西文的字符串長度不一致,這會導(dǎo)致排版出錯<<<
解決方法一:可以自定義字符串計算函數(shù),用西文空格填補(bǔ)
# 定義一個方法,用于判斷是否為中文字符
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),用中文空格填補(bǔ)(但在中西文混合的字符串仍然會出現(xiàn)排版不齊的情況)
14.添加學(xué)生信息的窗口
# 添加學(xué)生信息的窗口
def Window_Add():
# 實(shí)例化對象,創(chuàng)建root的子窗口window
window = tk.Toplevel(root)
# 窗口名字
window.title("添加學(xué)生信息")
# 窗口大小
window.geometry('500x500')
# 關(guān)于學(xué)號的 label 和 entry
Txt_ID = tk.StringVar()
Txt_ID.set("")
Label_Line1 = tk.Label(window, text = "學(xué) 號 (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')
# 關(guān)于姓名的 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')
# 關(guān)于專業(yè)的 label 和 entry
Txt_Major = tk.StringVar()
Txt_Major.set("")
Label_Line3 = tk.Label(window, text = "專 業(yè):", 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')
# 關(guān)于年齡的 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')
# 關(guān)于班級的 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')
# 關(guān)于電話的 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')
# 關(guān)于"確認(rèn)"組件,此處綁定函數(shù)Add_Student_Info用于添加學(xué)生信息
Button1_Yes = tk.Button(window, text = '確認(rèn)', 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')
# 關(guān)于"取消"組件,此處綁定函數(shù)destroy()用于關(guān)閉窗口
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 問題二:不能讀取到輸入框內(nèi)容
>>> 在使用get()函數(shù)時,如Txt_ID.get()不能讀取到輸入框的內(nèi)容
解決方法:創(chuàng)立窗口時定義為主窗口的子窗口,如果為window=tk.TK(),將運(yùn)行錯誤
window = tk.Toplevel(root)
14.2 問題三: Tk.Button中command指令無法執(zhí)行有傳參的函數(shù)
>>>在Button中使用command時,如果綁定的方法有參數(shù)的傳遞,不能寫成這樣:
command = func(int a, int b)
>>> 如果寫成這樣,那么在調(diào)試時會直接執(zhí)行,即不用點(diǎn)擊按鈕就執(zhí)行
解決方法:使用lambda
15.刪除學(xué)生信息的窗口
# 刪除學(xué)生信息的窗口
def Window_Del():
# 創(chuàng)建root的子窗口
window = tk.Toplevel(root)
window.title("刪除學(xué)生信息")
window.geometry('500x300')
# 關(guān)于學(xué)號的 label 和 entry
Txt_ID = tk.StringVar()
Txt_ID.set("")
Label_Line1 = tk.Label(window, text = "學(xué) 號 (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')
# 關(guān)于"確認(rèn)"組件,此處綁定函數(shù)Del_Student_Info用于刪除學(xué)生信息
Button1_Yes = tk.Button(window, text = '確認(rèn)', bg = 'silver', font = ('Arial', 12), command = lambda:Del_Student_Info(Txt_ID.get()), width = 10)
Button1_Yes.place(x = 75, y = 200, anchor = 'nw')
# 關(guān)于"取消"組件,此處綁定函數(shù)destroy()用于關(guān)閉窗口
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(">>>請先通過'查詢學(xué)生信息'查詢待刪除學(xué)生的學(xué)號<<<")
# 在界面中顯示文本框,打印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.修改學(xué)生信息的窗口
# 修改學(xué)生信息的窗口
def Window_Mod():
# 創(chuàng)建root的子窗口
window = tk.Toplevel(root)
window.title("修改學(xué)生信息")
window.geometry('500x300')
# 關(guān)于學(xué)號的 label 和 entry
Txt_ID = tk.StringVar()
Txt_ID.set("")
Label_Line1 = tk.Label(window, text = "學(xué) 號 (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')
# 關(guān)于"確認(rèn)"組件,此處綁定函數(shù)Mod_Student_Info用于修改學(xué)生信息
Button1_Yes = tk.Button(window, text = '確認(rèn)', bg = 'silver', font = ('Arial', 12), command = lambda:Mod_Student_Info(Txt_ID.get()), width = 10)
Button1_Yes.place(x = 75, y = 200, anchor = 'nw')
# 關(guān)于"取消"組件,此處綁定函數(shù)destroy()用于關(guān)閉窗口
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(">>>請先通過'查詢學(xué)生信息'查詢待修改學(xué)生的學(xué)號<<<")
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.輸入修改學(xué)生信息的窗口
# 輸入修改學(xué)生信息的窗口
def Window_Mod_Input(ID):
# 創(chuàng)建root的子窗口
window = tk.Toplevel(root)
window.title("修改學(xué)生信息")
window.geometry('500x500')
# 關(guān)于姓名的 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')
# 關(guān)于專業(yè)的 label 和 entry
Txt_Major = tk.StringVar()
Txt_Major.set("")
Label_Line3 = tk.Label(window, text = "專 業(yè):", 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')
# 關(guān)于年齡的 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')
# 關(guān)于班級的 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')
# 關(guān)于電話的 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')
# 關(guān)于"確認(rèn)"組件,此處綁定函數(shù)Mod_Student_Info_1用于修改學(xué)生信息
Button1_Yes = tk.Button(window, text = '確認(rèn)', 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')
# 關(guān)于"取消"組件,此處綁定函數(shù)destroy()用于關(guān)閉窗口
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.查找學(xué)生信息的窗口
# 查找學(xué)生信息的窗口
def Window_Ser():
# 創(chuàng)建root的子窗口
window = tk.Toplevel(root)
window.title("查詢學(xué)生信息")
window.geometry('500x500')
# 關(guān)于學(xué)號的 label 和 entry
Txt_ID = tk.StringVar()
Txt_ID.set("")
Label_Line1 = tk.Label(window, text = "學(xué) 號 (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')
# 關(guān)于姓名的 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')
# 關(guān)于專業(yè)的 label 和 entry
Txt_Major = tk.StringVar()
Txt_Major.set("")
Label_Line3 = tk.Label(window, text = "專 業(yè):", 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')
# 關(guān)于年齡的 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')
# 關(guān)于班級的 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')
# 關(guān)于電話的 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')
# 關(guān)于"確認(rèn)"組件,此處綁定函數(shù)Search_Student_Info用于修改學(xué)生信息
Button1_Yes = tk.Button(window, text = '確認(rèn)', 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')
# 關(guān)于"取消"組件,此處綁定函數(shù)destroy()用于關(guān)閉窗口
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(" >>>請輸入待查找學(xué)生的部分信息(可不全填)<<<")
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.退出管理系統(tǒng)的窗口
# 退出管理系統(tǒng)的窗口
def Window_Exit():
# 創(chuàng)建root的子窗口
window = tk.Toplevel()
window.title("退出管理系統(tǒng)")
window.geometry('400x300')
# 關(guān)于"確認(rèn)"組件,此處綁定函數(shù)destroy()用于關(guān)閉主窗口
Button1_Yes = tk.Button(window, text = '確認(rèn)', bg = 'silver', font = ('Arial', 12), command = lambda:root.destroy(), width = 10)
Button1_Yes.place(x = 50, y = 200, anchor = 'nw')
# 關(guān)于"取消"組件,此處綁定函數(shù)destroy()用于關(guān)閉窗口
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(" >>>您確認(rèn)離開系統(tǒng)嗎?<<<")
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.主窗口
# 如果該文件是程序運(yùn)行的主文件,將會運(yùn)行以下代碼
if __name__ == '__main__':
# 創(chuàng)建主窗口
root = tk.Tk()
root.title("學(xué)生信息管理系統(tǒng) V1.1")
root.geometry('900x400')
# 關(guān)于"添加學(xué)生信息"組件,此處綁定函數(shù)Search_Student_Info用于修改學(xué)生信息
Button1_Add = tk.Button(root, text = '添 加 學(xué) 生 信 息', 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 = '刪 除 學(xué) 生 信 息', 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 = '修 改 學(xué) 生 信 息', 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 = '查 詢 學(xué) 生 信 息', 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 = '顯 示 學(xué) 生 信 息', 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 = '退 出 管 理 系 統(tǒng)', bg = 'silver', font = ('Arial', 12), command = Window_Exit, width = 20)
Button6_Exit.place(x = 50, y = 300, anchor = 'nw')
result = tk.StringVar()
result.set(">>>歡迎使用學(xué)生信息管理系統(tǒng)<<<\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模塊實(shí)現(xiàn)GUI界面
import tkinter as tk
from tkinter import messagebox
# 用來存儲學(xué)生信息的總列表[學(xué)號(6位)、姓名、專業(yè)、年齡(17~25)、班級(序號)、電話(11位)]
# [ID Name Major Age Class Telephone]
Info = []
# 定義一個方法用于使用w模式寫入文件:傳入已經(jīng)存好變更好信息的列表,然后遍歷寫入txt文檔
def WriteTxt_w_Mode(Student_List):
# w:只寫入模式,文件不存在則建立,將文件里邊的內(nèi)容先刪除再寫入
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)
# 關(guān)閉文件
f.close()
# 將含有學(xué)生信息的臨時列表返回
return Temp_List2
# 定于一個方法,用于檢查學(xué)號是否規(guī)范
def is_ID(ID):
return len(ID) == 6 and ID.isdigit()
# 定于一個方法,用于檢查年齡是否規(guī)范
def is_Age(Age):
return Age.isdigit() and 17 <= int(Age) and int(Age) <= 25
# 定于一個方法,用于檢查班級是否規(guī)范
def is_Class(Class):
return Class.isdigit() and int(Class) > 0
# 定于一個方法,用于檢查電話是否規(guī)范
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("提示信息","此學(xué)號已經(jīng)存在,請勿重復(fù)添加!")
def Tip_Del_ID_None():
messagebox.showinfo("提示信息","此學(xué)號不存在,請重新輸入!")
def Tip_Search_None():
messagebox.showinfo("提示信息","未查詢到有關(guān)學(xué)生信息!")
def Tip_Add_ID():
messagebox.showinfo("提示信息","學(xué)號格式有誤,請重新輸入!")
def Tip_Add_Age():
messagebox.showinfo("提示信息","年齡格式有誤,請重新輸入!")
def Tip_Add_Class():
messagebox.showinfo("提示信息","班級格式有誤,請重新輸入!")
def Tip_Add_Telephone():
messagebox.showinfo("提示信息","電話格式有誤,請重新輸入!")
# 1.添加學(xué)生信息的主方法
def Add_Student_Info(ID, Name, Major, Age, Class, Telephone):
# 導(dǎo)入信息
global Info
# 檢查輸入信息是否規(guī)范
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
# 用字典整合學(xué)生信息
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.刪除學(xué)生信息的主方法
def Del_Student_Info(ID):
# 檢查輸入信息是否規(guī)范
if not is_ID(ID):
Tip_Add_ID()
return
# 用于指示是否刪除的狀態(tài)指標(biāo)
Flag = True
# 導(dǎo)入信息
global Info
# 遍歷,刪除學(xué)生信息
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.修改學(xué)生信息,ID符合條件則打開修改學(xué)生信息的窗口
def Mod_Student_Info(ID):
if not is_ID(ID):
Tip_Add_ID()
return
# 用于指示是否打開窗口的指標(biāo)
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.修改學(xué)生信息的主方法
def Mod_Student_Info_1(ID, Name, Major, Age, Class, Telephone):
# 檢查輸入信息是否規(guī)范
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
# 導(dǎo)入信息
global Info
# 遍歷,修改學(xué)生信息
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.查找學(xué)生信息的主方法
def Search_Student_Info(ID, Name, Major, Age, Class, Telephone):
# 檢查輸入是否規(guī)范,規(guī)范的和空字符串通過
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
# 導(dǎo)入信息
global Info
# 臨時列表
List = []
# 用來指示是否查找到學(xué)生的信息指標(biāo)
Flag = False
# 遍歷,根據(jù)輸入的部分信息找到符合條件的學(xué)生
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
# 在主界面打印符合條件學(xué)生信息
Print_Student_Info(List)
# 是否查找成功
if Flag:
Tip_Search()
else:
Tip_Search_None()
# 5.顯示所有學(xué)生的主方法
def Print_Student_Info(Student_Info):
# 定義一個字符串用于存儲想要輸出顯示的內(nèi)容
str_out = ""
# 學(xué)生信息為空將返回
if Student_Info is None:
result.set("學(xué)生信息為空")
return
if len(Student_Info) == 0:
result.set("無學(xué)生信息")
return
# 學(xué)生信息不為空
if len(Student_Info) != 0:
str_out += "學(xué)生信息如下:\n"
# 顯示信息標(biāo)題
str_out += ("{:^7}".format("學(xué)生學(xué)號") +
"{:^7}".format("學(xué)生姓名") +
"{:^7}".format("學(xué)生專業(yè)") +
"{:^7}".format("學(xué)生年齡") +
"{:^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")
# 在主界面顯示學(xué)生信息
result.set(str_out)
# 添加學(xué)生信息的窗口
def Window_Add():
# 實(shí)例化對象,創(chuàng)建root的子窗口window
window = tk.Toplevel(root)
# 窗口名字
window.title("添加學(xué)生信息")
# 窗口大小
window.geometry('500x500')
# 關(guān)于學(xué)號的 label 和 entry
Txt_ID = tk.StringVar()
Txt_ID.set("")
Label_Line1 = tk.Label(window, text = "學(xué) 號 (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')
# 關(guān)于姓名的 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')
# 關(guān)于專業(yè)的 label 和 entry
Txt_Major = tk.StringVar()
Txt_Major.set("")
Label_Line3 = tk.Label(window, text = "專 業(yè):", 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')
# 關(guān)于年齡的 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')
# 關(guān)于班級的 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')
# 關(guān)于電話的 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')
# 關(guān)于"確認(rèn)"組件,此處綁定函數(shù)Add_Student_Info用于添加學(xué)生信息
Button1_Yes = tk.Button(window, text = '確認(rèn)', 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')
# 關(guān)于"取消"組件,此處綁定函數(shù)destroy()用于關(guān)閉窗口
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()
# 刪除學(xué)生信息的窗口
def Window_Del():
# 創(chuàng)建root的子窗口
window = tk.Toplevel(root)
window.title("刪除學(xué)生信息")
window.geometry('500x300')
# 關(guān)于學(xué)號的 label 和 entry
Txt_ID = tk.StringVar()
Txt_ID.set("")
Label_Line1 = tk.Label(window, text = "學(xué) 號 (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')
# 關(guān)于"確認(rèn)"組件,此處綁定函數(shù)Del_Student_Info用于刪除學(xué)生信息
Button1_Yes = tk.Button(window, text = '確認(rèn)', bg = 'silver', font = ('Arial', 12), command = lambda:Del_Student_Info(Txt_ID.get()), width = 10)
Button1_Yes.place(x = 75, y = 200, anchor = 'nw')
# 關(guān)于"取消"組件,此處綁定函數(shù)destroy()用于關(guān)閉窗口
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(">>>請先通過'查詢學(xué)生信息'查詢待刪除學(xué)生的學(xué)號<<<")
# 在界面中顯示文本框,打印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()
# 修改學(xué)生信息的窗口
def Window_Mod():
# 創(chuàng)建root的子窗口
window = tk.Toplevel(root)
window.title("修改學(xué)生信息")
window.geometry('500x300')
# 關(guān)于學(xué)號的 label 和 entry
Txt_ID = tk.StringVar()
Txt_ID.set("")
Label_Line1 = tk.Label(window, text = "學(xué) 號 (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')
# 關(guān)于"確認(rèn)"組件,此處綁定函數(shù)Mod_Student_Info用于修改學(xué)生信息
Button1_Yes = tk.Button(window, text = '確認(rèn)', bg = 'silver', font = ('Arial', 12), command = lambda:Mod_Student_Info(Txt_ID.get()), width = 10)
Button1_Yes.place(x = 75, y = 200, anchor = 'nw')
# 關(guān)于"取消"組件,此處綁定函數(shù)destroy()用于關(guān)閉窗口
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(">>>請先通過'查詢學(xué)生信息'查詢待修改學(xué)生的學(xué)號<<<")
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()
# 輸入修改學(xué)生信息的窗口
def Window_Mod_Input(ID):
# 創(chuàng)建root的子窗口
window = tk.Toplevel(root)
window.title("修改學(xué)生信息")
window.geometry('500x500')
# 關(guān)于姓名的 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')
# 關(guān)于專業(yè)的 label 和 entry
Txt_Major = tk.StringVar()
Txt_Major.set("")
Label_Line3 = tk.Label(window, text = "專 業(yè):", 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')
# 關(guān)于年齡的 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')
# 關(guān)于班級的 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')
# 關(guān)于電話的 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')
# 關(guān)于"確認(rèn)"組件,此處綁定函數(shù)Mod_Student_Info_1用于修改學(xué)生信息
Button1_Yes = tk.Button(window, text = '確認(rèn)', 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')
# 關(guān)于"取消"組件,此處綁定函數(shù)destroy()用于關(guān)閉窗口
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()
# 查找學(xué)生信息的窗口
def Window_Ser():
# 創(chuàng)建root的子窗口
window = tk.Toplevel(root)
window.title("查詢學(xué)生信息")
window.geometry('500x500')
# 關(guān)于學(xué)號的 label 和 entry
Txt_ID = tk.StringVar()
Txt_ID.set("")
Label_Line1 = tk.Label(window, text = "學(xué) 號 (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')
# 關(guān)于姓名的 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')
# 關(guān)于專業(yè)的 label 和 entry
Txt_Major = tk.StringVar()
Txt_Major.set("")
Label_Line3 = tk.Label(window, text = "專 業(yè):", 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')
# 關(guān)于年齡的 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')
# 關(guān)于班級的 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')
# 關(guān)于電話的 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')
# 關(guān)于"確認(rèn)"組件,此處綁定函數(shù)Search_Student_Info用于修改學(xué)生信息
Button1_Yes = tk.Button(window, text = '確認(rèn)', 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')
# 關(guān)于"取消"組件,此處綁定函數(shù)destroy()用于關(guān)閉窗口
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(" >>>請輸入待查找學(xué)生的部分信息(可不全填)<<<")
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()
# 退出管理系統(tǒng)的窗口
def Window_Exit():
# 創(chuàng)建root的子窗口
window = tk.Toplevel()
window.title("退出管理系統(tǒng)")
window.geometry('400x300')
# 關(guān)于"確認(rèn)"組件,此處綁定函數(shù)destroy()用于關(guān)閉主窗口
Button1_Yes = tk.Button(window, text = '確認(rèn)', bg = 'silver', font = ('Arial', 12), command = lambda:root.destroy(), width = 10)
Button1_Yes.place(x = 50, y = 200, anchor = 'nw')
# 關(guān)于"取消"組件,此處綁定函數(shù)destroy()用于關(guān)閉窗口
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(" >>>您確認(rèn)離開系統(tǒng)嗎?<<<")
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
# 如果該文件是程序運(yùn)行的主文件,將會運(yùn)行以下代碼
if __name__ == '__main__':
# 創(chuàng)建主窗口
root = tk.Tk()
root.title("學(xué)生信息管理系統(tǒng) V1.1")
root.geometry('900x400')
# 關(guān)于"添加學(xué)生信息"組件,此處綁定函數(shù)Search_Student_Info用于修改學(xué)生信息
Button1_Add = tk.Button(root, text = '添 加 學(xué) 生 信 息', 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 = '刪 除 學(xué) 生 信 息', 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 = '修 改 學(xué) 生 信 息', 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 = '查 詢 學(xué) 生 信 息', 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 = '顯 示 學(xué) 生 信 息', 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 = '退 出 管 理 系 統(tǒng)', bg = 'silver', font = ('Arial', 12), command = Window_Exit, width = 20)
Button6_Exit.place(x = 50, y = 300, anchor = 'nw')
result = tk.StringVar()
result.set(">>>歡迎使用學(xué)生信息管理系統(tǒng)<<<\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()
到此這篇關(guān)于Python使用tkinter模塊實(shí)現(xiàn)GUI界面的學(xué)生信息管理系統(tǒng)流程分步詳解的文章就介紹到這了,更多相關(guān)Python學(xué)生信息管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python paramiko利用sftp上傳目錄到遠(yuǎn)程的實(shí)例
今天小編就為大家分享一篇python paramiko利用sftp上傳目錄到遠(yuǎn)程的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
python2.7實(shí)現(xiàn)爬蟲網(wǎng)頁數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了python2.7實(shí)現(xiàn)爬蟲網(wǎng)頁數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Python實(shí)現(xiàn)釘釘/企業(yè)微信自動打卡的示例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)釘釘/企業(yè)微信自動打卡的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
基于Python實(shí)現(xiàn)報表自動化并發(fā)送到郵箱
作為數(shù)據(jù)分析師,我們需要經(jīng)常制作統(tǒng)計分析圖表。但是報表太多的時候往往需要花費(fèi)我們大部分時間去制作報表。本文將利用Python實(shí)現(xiàn)報表自動化并發(fā)送到郵箱,需要的可以參考一下2022-07-07
python爬蟲Scrapy框架:媒體管道原理學(xué)習(xí)分析
這篇文章主要介紹了python爬蟲Scrapy框架:媒體管道原理學(xué)習(xí)分析,有需要的朋友可以借鑒參考,希望可以對廣大一同學(xué)習(xí)的讀者朋友有所幫助2021-09-09
Python報錯:TypeError:?‘xxx‘?object?is?not?subscriptable解決
這篇文章主要給大家介紹了關(guān)于Python報錯:TypeError:?‘xxx‘?object?is?not?subscriptable的解決辦法,TypeError是Python中的一種錯誤,表示操作或函數(shù)應(yīng)用于不合適類型的對象時發(fā)生,文中將解決辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08

