學生信息管理系統的開發
模塊: 1. 系統登錄界面 --> logingui.py
2. 系統主界面 --> maingui.py
3. 學生明細界面 --> detailgui.py
4. 修改登錄賬號密碼界面 --> changepassword.py
5. GUI界面需要的圖庫
6. 數據 --> (1) 登錄賬號相關文件 (loginuser_info.txt) (2) 加載到主界面中顯示的學生信息文件(student_info.txt)
# 圖片部分
===》 注意: 這里的圖片全部是png格式的 代碼中窗體小圖標是ico格式的可以通過網上轉換圖片生成ico格式的圖片
# 文件部分
student_info.txt
95001,橘右京,20,男,15977693796,juyoujing@yahoo.cn.jp,日本大阪府大阪市,1988/9/12,110103199110186521,2010年6月18日,戰士,橘子成精,18911213348 95002,韓信,19,男,18695719656,www.hanxin163.com,陝西省西安市韓台鎮韓家村196號,1990/5/30,610103199110182714,2010年2月18日,刺客,韓悠悠,15977693321 95003,亞瑟,30,男,15834769921,yase@chengxin196.com,美國卡里福尼亞州,1981/7/21,370626798909071691,1978年7月17日,戰士,亞歷山大三世,18955789581 95004,王昭君,18,女,18977623571,wangzhaojun@qq.com,江蘇省蘇州市姑蘇區,1998/12/3,320506198911112836,2014年9月30日,法師,王他爹,17966289971 95005,阿珂,17,女,13644893365,jingke@qq.com,山東省煙台市芝罘區,2000/3/23,370602198909071333,1999年9月9日,刺客,阿可可,15855372291 95006,白起,28,男,15962789930,www.baiqi@163.com,陝西省西安市,1978/12/31,610103199110182255,1978年12月30日,戰士,白斬,18633462237 95007,蘇烈,37,男,18912347689,sulie@yahoo.cn.jp,遼寧省黑龍江市,1975/9/30,3205061989167810056,1971年8月12日,戰士,蘇醒,15977889900 95008,虞姬,18,女,15942887540,www.yuji_s@qq.com,江蘇省蘇州市,1999/6/10,320506198911110056,1998年2月28日,射手,虞陣子,15890017789 95009,魯班,16,男,18856713396,luban250@163.com,河南省鄭州市,2001/5/7,3865198911110011,2001年12月12日,射手,魯班大師,15977889911 95010,狄仁傑,20,男,18867321541,direnjie@alibaba.com,江蘇省杭州市,1996/10/30,32050619891230056,1998年2月1日,射手,狄小兵,15778890071 95011,東方曜,21,男,15855667913,dongfangyao@qq.com,陝西省西安市,1997/10/24,610103199110181412,2008年11月06日,戰士,東方朔,18977239971 95012,嬴政,29,男,15588988888,yingzheng@beifeng.com,陝西省西安市,1981/4/12,610103199110182079,1999年5月31日,法師,嬴政他爹,18857613374 95013,蔡文姬,20,女,18666890013,caicai@alibaba.com,雲南省大理市,1997/10/16,320506156911110056,1998年11月27日,輔助,蔡銘,18758092231 95014,甄姬,24,女,18562782278,www.zhenji@163.com,山西省大同市,1994/12/1,320506198911110056,1997年4月30日,法師,甄子丹,16599002345 95015,宮本武藏,27,男,18977889900,juyoujing@yahoo.cn.jp,日本大阪府大阪市,1982/3/19,3205061989112346,1982年2月21日,戰士,宮本重生,17971889920 95016,明世隱,30,男,18662722447,www.mingshiyin@163.com,湖南省荊州市,1977/11/11,320506198911110216,1971年2月28日,輔助,明世隱公子,15888990011 95017,李元芳,17,男,15727458228,liyuanfang@ibm.com,湖北省武漢市,1999/12/11,32050619891111112234,2004年7月8日,射手,李元直,1857789012237 95018,盾山,37,男,18677882288,www.dunshan@qq.com,山東省煙台市,1975/12/12,370602198909071472,1978年5月17日,輔助,盾地,15888887777 95019,諸葛亮,24,男,18911118888,zhugeliang@qq.com,湖南省荊州市,1996/6/8,320506198911223344,1998年9月11日,法師,諸葛蘇,15777812910 95020,趙雲,27,男,18889966657,zhaozilong@qq.com,河南省荊州市,1993/12/12,320506198922331005,1989年10月28日,戰士,趙形,167877889109 95021,關羽,29,男,18811007788,guanyu@yahoo.com,山東省煙台市,1981/12/14,370602198909070437,1990年1月12日,戰士,關包,13877639961 95022,不知火舞,18,女,18677120091,huwu@yahoo.co.jp,日本東京,1997/2/14,370602199702141471,2020年4月1日,法師,安迪,18612416097 95023,張飛,28,男,15811221190,zhangfei@qq.com,河南省鄭州市,1985/1/30,320506198501301014,2020年1月1日,坦克,劉備,15988002581
loginuser_info.txt
liupeng,P@ssw0rd,1 admin,123.com,0 zhangsan,123.com,0
一,系統登錄界面
思路: 1. GUI界面的設計 --> 通過python自帶的 tkinter 及 tkinter的加強版 tkinter.ttk 來實現登錄界面的部署 (編碼部署界面 --> 原因:為了日后習慣web開發)
2. GUI界面下通過用戶名密碼進行身份驗證 --> 讀取到文件中的用戶信息存入集合 --> 集合中通過元素[0][1][2]的方式管理學生信息的用戶名,密碼及鎖定狀態
3. 用戶登錄判定: (1) 判斷登錄用戶是否被鎖定
(2) 判斷登錄用戶是否存在
(3) 判斷登錄用戶密碼輸入是否正確
(4) 判斷登錄用戶密碼登錄是否超過3次,3次判定為鎖定並寫入到文件
代碼實現:

from tkinter import * import tkinter.ttk as ttk import os from tkinter.messagebox import * from datetime import datetime import wangzhexiagu.GUI_student_system.maingui as man class Login_Windows(Tk): def __init__(self): """ 主構造函數 1. 初始化登錄界面窗體 2. 布局登錄窗體並配置各個按鍵的屬性 3. 實現用戶驗證功能 """ super().__init__() #初始化框口 self.title("登錄界面") self.geometry("640x420+300+200") #通過控制圖像大小可以通過+號來調整窗體出現的位置 self.resizable(0,0) #固定窗體大小.不允許改動窗體大小. resizable(0,0)表示x軸為0y軸也為0 self.iconbitmap(R"C:\Users\Administrator\PycharmProjects\untitled1\wangzhexiagu\GUI_student_system\photo\china.ico") #添加圖片背景色 self["bg"] = "RoyalBlue" #傳統tkinter中配置顏色方法 #加載窗體 self.SetUp_UI() #設置全局變量 self.file_path = R"C:\Users\Administrator\PycharmProjects\untitled1\wangzhexiagu\GUI_student_system\loginUserInfo_Data\loginuser_info.txt" self.user_list = [] #用戶存儲讀取文件中所有的元素 self.misspasswd_counter = 0 #記錄password錯誤的次數 self.input_user = "" #記錄當前用戶 self.input_password = "" #記錄當前用戶密碼 self.current_login_list = [] #存儲登錄的賬號和密碼 #自動執行文件中文件的加載 self.load_localfile_info() #showinfo(message=self.user_list) #通過該提示可以直接看文件是否被讀取成功! #設置GUI頁面窗體及控件的布局 def SetUp_UI(self): #通過style設置屬性 self.Style01 = ttk.Style() self.Style01.configure("TLabel",font=("微軟眼黑",18,"bold"),background = "RoyalBlue",foreground = "white") self.Style01.configure("TButton", font=("微軟眼黑", 11), background="RoyalBlue", foreground="black") #加載窗體圖片 self.login_img = PhotoImage(file =R"C:\Users\Administrator\PycharmProjects\untitled1\wangzhexiagu\GUI_student_system\photo\school_demo.png") self.label_img = ttk.Label(self,image = self.login_img) self.label_img.pack(padx = 10,pady = 10) #創建Label + Entry ----用戶名 self.username = ttk.Label(self,text = "用戶名") self.var_user = StringVar() #為了獲取鍵盤輸入 self.username.pack(side = LEFT,padx = 10, pady = 10) self.user_entry = ttk.Entry(self,width = 15,textvariable =self.var_user,font=("微軟眼黑",13,"bold"))# bold代表字體粗體 self.user_entry.pack(side = LEFT,padx = 10, pady = 10) #創建Label + Entry ----密碼 self.password = ttk.Label(self,text = "密碼") self.var_password = StringVar()#為了獲取鍵盤輸入 self.password.pack(side = LEFT,padx = 10, pady = 10) self.password_entry = ttk.Entry(self,show = "*",width = 15,textvariable=self.var_password,font=("微軟眼黑",13,"bold")) #show方法代表密碼密文 self.password_entry.pack(side = LEFT,padx = 10, pady = 10) #創建Button ---- 登錄 self.button_login = ttk.Button(self,text = "登錄",command = self.login) self.button_login.pack(side = LEFT,padx = 10, pady = 10) def load_localfile_info(self): """ 加載本地文件: 通過readline()方法一次讀取一行的方式進行讀取(避免日后文件過大一次性讀取的弊端) :return: """ if not os.path.exists(self.file_path): showinfo("系統提示","文件不存在.請確認后重新加載!!") else: try: with open(self.file_path,mode = "r") as fd: current_list = fd.readline() #一次讀一行 while current_list: #表示直到current_list中所有元素被讀完,循環結束 temp_list = current_list.split(",") #按照逗號把每個list中的元素分割出來 self.user_list.append(temp_list) #把分割后的每個元素重新添加到user_list列表中 current_list = fd.readline() #因為我們一次只讀一行。讀完后如果不加這一句那么循環會一直只讀第一行。 except: showinfo("系統信息","文件讀取出現異常.請聯系管理員!!") def login(self): """ 用戶身份驗證功能的實現 1. 獲取到文件中的用戶名,密碼,狀態(是否被鎖定) 2. 判斷用戶是否被鎖定及是否是規定用戶以外的非法用戶登錄(如果是拒絕登錄) 3. 用戶正確判斷密碼是否匹配,密碼不匹配3次被鎖定並寫入到文件 :return: """ # 獲取輸入的用戶名及密碼 self.input_user = self.var_user.get() self.input_password = self.var_password.get() #showinfo("提示信息","用戶名:" + input_user + "\n" + "密碼:" + input_password) #實現身份驗證 for index in range(len(self.user_list)): #這里的user_list 為之前從文件中讀取重新存入list列表中的數據 #先判斷用戶名是否存在 if self.input_user.strip().lower() == str(self.user_list[index][0]).strip().lower(): #如果用戶名存在,再判斷用戶名是否被鎖定。1為鎖定,0為active. if "1" in str(self.user_list[index][2]): showinfo("系統提示", "該用戶被鎖定.請聯系管理員解鎖后再登錄。") break else: #如果用戶存在並且是active的狀態再判斷密碼是否正確 if self.input_password != str(self.user_list[index][1]): self.misspasswd_counter +=1 #初始值設置為0.如果上述條件不成立則循環加1次 if self.misspasswd_counter >=3: #設置密碼錯誤次數最大不能超過3次 showinfo("系統提示","密碼輸入錯誤3次賬號被鎖定!") #改變鎖定賬戶的狀態(如果錯誤3次則改變文件中賬戶的狀態把第3個元素變為1代表用戶被鎖定) self.user_list[index][2] = "1\n" #這里需要加一個空格 #寫入文件 self.write_file() #調用下列write_file()方法 else: showinfo("系統提示", "密碼錯誤,請重新輸入") break else: self.misspasswd_counter = 0 #如果輸入的password正確那么這里的錯誤次數還是為0 self.current_login_list = self.user_list[index] #用戶密碼輸入都正確則加載主窗體() self.upload_main() break #這句話的意思是:循環到最后如果沒有找到相同的用戶名則用戶判定為不存在!!!(這句話為重點,也是因為這句話才循環時用索引) if index == len(self.user_list)-1: showinfo("系統提示","輸入的用戶名不存在!") def write_file(self): """ 寫入文件":1.分2次寫入第一次先清空原文件,第2次再逐一寫入 2. 寫入的是用戶登錄后反應的最新的狀態信息 :return: """ try: #[1] 先清空原文件(不放心可以先備份原文件再執行代碼,不然數據會丟失) with open(self.file_path,mode="w") as fd: fd.write("") #不寫任何東西代表清空 #[2] 再逐一把user_list中的數據寫入到文件."a"代表追加寫入 with open(self.file_path,mode = "a") as fd: for item in self.user_list: fd.write(",".join(item)) #通過.join()方法指定寫入時按照什么進行分割寫入文件。這里指定的是按照逗號分割寫入 except: showinfo("系統信息","寫入數據失敗.") def upload_main(self): """ 加載主窗體函數: 1. 用戶驗證通過后自動關閉當前窗體 2. 用戶驗證通過后自動加載主窗體 :return: """ #關閉當前窗體 self.destroy() #加載主窗體 self.main_window = man.Main_Windows(self.current_login_list,self.get_login_time()) #man.Main_Windows()中self.current_login_list及self.get_login_time()為加載登錄主窗體后實現當前用戶及登錄時間顯示的功能 def get_login_time(self): """ 實現用戶登錄后自動加載登錄時間功能 #通過import datetime這個模塊中datetime.today()方法來實現.當然也可以通過其他方法實現比如時間元祖等 :return: """ today = datetime.today() return ("%04d/%02d/%02d %02d:%02d:%02d" % ( today.year, today.month, today.day, today.hour, today.minute, today.second)) if __name__ == "__main__": this_login = Login_Windows() this_login.mainloop()
二,系統主界面
思路: 1. GUI界面的設計 --> 同上同樣是通過tkinter進行設計
(1) 上部 : 加載主窗體的圖片 * 右下方顯示當前登錄的用戶及當時登錄時的系統時間 (這里是這部分的重點)
(2) 左部: 左邊部分是功能區域 (用戶的增刪改及修改用戶及密碼的GUI界面部署)
(3) 右部: 右邊部分分2大區域 1. 查詢區域( 學號,姓名,年齡,手機號碼,住址 等 來查詢的GUI界面部署)
2. 數據展示區域(展示的是添加進來的student_info.txt中的數據,及查詢后所展示的數據)
3. 查詢區域中查詢按鈕及顯示全部數據的GUI部署
(4) 總體 : 數據最終保存及退出界面的GUI界面部署
2. 上部: 如何顯示當前用戶及當前登錄的系統時間
(1) logingui界面中的login方法里,用戶最終登錄系統成功的節點下 --> 獲取登錄成功的用戶名及密碼 (這里主要用到的是用戶名) --> 存入到集合備用
(2) logingui界面中,創建get_login_time()方法獲取當前的系統時間
(3) maingui主界面的構造函數中傳參並實例化,把login界面中的集合在主界面下進行使用
(4) 主界面上部右下方的label窗口中,加載當前的用戶和時間
3. 左部及右部 : 查看, 添加,修改 調用的都是同一個GUI界面. 如何實現在加載相同的窗口時,提示標題不相同
(1) 通過flag來標識登錄的detailgui是查看還是添加,修改 --> flag為全局變量 --> GUI窗體設計title部分不要寫死 --> 通過獲取title狀態的方式識別
(2) 創建load_window_flag方法來控制每種狀態下窗口的顯示
1. flag =1 代表查看窗口 --> 查看窗口不允許更改任何信息,因此禁用detailgui查看窗口下的所有控件
2. flag = 2 代表添加窗口 --> 因為是添加所以不做任何限制
3. flag = 3 代表修改窗口 --> 學號綁定不許修改其他控件允許修改 -->可以更加優化修改窗口中每個控件的值的輸入(因為時間原因我本人沒有做更好的優化)
(3) 添加,修改,刪除,修改密碼等具體功能的實現 --> 詳情見代碼有注釋
4. 右部: TreeView控件中展示所有學員信息 -->通過查詢功能的實現來展示 --> 詳情見代碼
代碼實現:

from tkinter import * import tkinter.ttk as ttk import os from tkinter.messagebox import * import wangzhexiagu.GUI_student_system.detailgui as dt import wangzhexiagu.GUI_student_system.change_password as ps class Main_Windows(Tk): """ 主窗體的構造函數 1. current_user,current_time的引入是為了加載logingui.py窗體中用戶輸入及加載登錄時間記錄時間的參數 2. 同樣設計主窗體下界面的布局及配置 """ def __init__(self,current_login_user,current_time): #current_login_user為了實現登錄信息的加載(記錄logingui界面中當前用戶是誰) #current_time同樣是為了記錄當前用戶登錄的系統時間 super().__init__() self.title("主窗體") self.geometry("1200x700+1+20") # 通過控制圖像大小可以通過+號來調整窗體出現的位置 self.resizable(0, 0) # 固定窗體大小.不允許改動窗體大小. resizable(0,0)表示x軸為0y軸也為0 self.iconbitmap(R"C:\Users\Administrator\PycharmProjects\untitled1\wangzhexiagu\GUI_student_system\photo\china.ico") # 加載.ico文件到窗口小圖標. 網上可以將png轉成ico文件 # 添加圖片背景色 self["bg"] = "RoyalBlue" #設置全局變量 self.login_times = current_time #為了記錄用戶的登錄時間 self.all_list = [] # 讀取student_info.txt中所有的元素並存入all_list中 self.current_stu_list = [] #存儲雙擊行匹配到的最終數據 self.get_number_result = [] #用戶存儲獲取到的所有學員信息 self.file_path = R"C:\Users\Administrator\PycharmProjects\untitled1\wangzhexiagu\GUI_student_system\loginUserInfo_Data\student_info.txt" self.action_flag = 0 #設置查看/修改/添加學生信息的title值,默認為0 self.current_user_list = current_login_user #把login界面的list傳到主界面接收 #加載窗體 self.Setup_UI() #自動執行函數 self.Load_local_Data() # 自動加載數據(加載的是all_list中已經讀取到的所有數據) self.load_TreeView(self.all_list) #自動將all_list中每一行的數據加載到TreeView中展示 #將窗體的行為轉化為方法 self.protocol("WM_DELETE_WINDOW",self.close_windows) #"WM_DELETE_WINDOW"為固定寫法 def Setup_UI(self): """ 布局主窗體的窗體並對其中的控件屬性進行配置 :return: """ #設置Sytle配置控件的屬性 self.Style01 = ttk.Style() self.Style01.configure("left.TPanedwindow",background="RoyalBlue") self.Style01.configure("right.TPanedwindow", background="skyblue") self.Style01.configure("TButton",font = ("微軟雅黑",12,"bold")) self.Style01.configure("TLabel", font=("微軟雅黑", 10, "bold")) self.Style01.configure("labframe.TButton", font=("微軟雅黑", 10, "bold")) self.Style01.configure("title1.TLabel", font=("微軟雅黑", 10, "bold"),background = "lightblue") # 加載窗體圖片 self.login_img = PhotoImage(file=R"C:\Users\Administrator\PycharmProjects\untitled1\wangzhexiagu\GUI_student_system\photo\1.png") self.label_img = ttk.Label(self, image=self.login_img) self.label_img.pack() #加載當前用戶和時間 self.label_login_user = ttk.Label(self,text = "當前登錄用戶:" + str(self.current_user_list[0]).title() + "\n登錄時間:" + self.login_times,style = "title1.TLabel" ) #str(self.current_user_list[0]).title()為用戶登錄后的首字母顯示未大寫 self.label_login_user.place(x=1000,y=165) # 左邊按鈕區域的布局 self.pan_left = ttk.PanedWindow(width =200,height=500,style ="left.TPanedwindow" ) self.pan_left.pack(side = LEFT,padx=3,pady=1) # 添加左邊區域按鈕 self.buttion_useradd = ttk.Button(self.pan_left,text = "添加學生",width =12,command = self.add_student) self.buttion_useradd.place(x = 30,y = 20) self.buttion_userupdate = ttk.Button(self.pan_left,text = "修改學生",width =12,command = self.update_student) self.buttion_userupdate.place(x = 30,y = 60) self.buttion_userdelete = ttk.Button(self.pan_left,text = "刪除學生",width =12,command = self.delete_student) self.buttion_userdelete.place(x = 30,y = 100) self.buttion_changepassword = ttk.Button(self.pan_left,text = "更改密碼",width =12,command = self.change_password_windows) self.buttion_changepassword.place(x = 30,y = 140) #右邊按鈕區域的布局 self.pan_right = ttk.PanedWindow(width = 991,height = 500,style = "right.TPanedwindow" ) self.pan_right.pack(side = LEFT) # 添加查詢區域(屬於右邊區域) self.labelFrame_query = ttk.LabelFrame(self.pan_right,text = "學生信息",width =990,height = 60) self.labelFrame_query.place(x=1,y=1) #添加控件 self.label_number = ttk.Label(self.labelFrame_query,text = "學號:") self.label_stu_no = StringVar() self.label_number.place(x =10,y =10) self.entry_number = ttk.Entry(self.labelFrame_query,textvariable = self.label_stu_no,font = ("微軟雅黑",10,"bold"),width=8) self.entry_number.place(x =50,y =10) self.label_name = ttk.Label(self.labelFrame_query,text = "姓名:") self.get_stu_name = StringVar() self.label_stu_name = StringVar() self.label_name.place(x =130,y =10) self.entry_name = ttk.Entry(self.labelFrame_query,textvariable = self.get_stu_name,font = ("微軟雅黑",10,"bold"),width=10) self.entry_name.place(x =170,y =10) self.label_age = ttk.Label(self.labelFrame_query,text = "年齡:") self.get_stu_age = StringVar() self.label_age.place(x =270,y =10) self.entry_age = ttk.Entry(self.labelFrame_query,textvariable=self.get_stu_age,font = ("微軟雅黑",10,"bold"),width=5) self.entry_age.place(x =310,y =10) self.label_mobile = ttk.Label(self.labelFrame_query,text = "電話:") self.get_stu_mobile = StringVar() self.label_mobile.place(x =360,y =10) self.entry_mobile = ttk.Entry(self.labelFrame_query,textvariable=self.get_stu_mobile,font = ("微軟雅黑",10,"bold"),width=16) self.entry_mobile.place(x =400,y =10) self.label_address = ttk.Label(self.labelFrame_query,text = "住址:") self.get_stu_address = StringVar() self.label_address.place(x =540,y =10) self.entry_address = ttk.Entry(self.labelFrame_query,textvariable=self.get_stu_address,font = ("微軟雅黑",10,"bold"),width=20) self.entry_address.place(x =590,y =10) self.button_search = ttk.Button(self.labelFrame_query,text = "查詢",width=6,style="labframe.TButton",command = self.get_student_result) self.button_search.place(x=780, y=10) self.button_homepage = ttk.Button(self.labelFrame_query,text = "返回主頁面",width=10,style="labframe.TButton" ,command = self.load_all_data) self.button_homepage.place(x=850, y=10) #添加TreeView控件 self.treeView = ttk.Treeview(self.pan_right,columns = ("學號","姓名","年齡","性別","電話","郵箱","家庭住址"), show = "headings") # 設置每一列的寬度和對齊方式 self.treeView.column("學號", width=80, anchor="center") self.treeView.column("姓名", width=60, anchor="w") #w代表西面也就是左邊對齊的意思 self.treeView.column("年齡", width=60, anchor="center") self.treeView.column("性別", width=60, anchor="center") self.treeView.column("電話", width=180, anchor="center") self.treeView.column("郵箱", width=250, anchor="center") self.treeView.column("家庭住址", width=298, anchor="w") # 設置表頭的標題文本 self.treeView.heading("學號", text="學號") self.treeView.heading("姓名", text="姓名") self.treeView.heading("年齡", text="年齡") self.treeView.heading("性別", text="性別") self.treeView.heading("電話", text="電話") self.treeView.heading("郵箱", text="郵箱") self.treeView.heading("家庭住址", text="家庭住址") #插入滾動條 #self.scrollBar = Scrollbar(self) #self.scrollBar.pack(side=RIGHT,fill=Y) #設置關聯 #self.scrollBar.config(command = self.treeView.yview) #展示treeView信息 self.treeView.place(x=1,y=60,height =450) # 雙擊TreeView中某一行觸發PopUp窗體 self.treeView.bind("<Double-1>",self.view_student) #<Double>是必須的Double以后的可以任意但是不能跟命令行重名 def Load_local_Data(self): """ 加載student_info.txt中的信息(本地文件的讀取) :return: 把文件中的每一個元素存儲到 all_list 中 """ if not os.path.exists(self.file_path): showinfo("系統提示","文件不存在.請確認后重新加載!!") else: try: with open(self.file_path,mode = "r") as fd: current_list = fd.readline() #一次讀一行 while current_list:#表示直到current_list中所有元素被讀完,循環結束 temp_list = current_list.split(",") #按照逗號把每個list中的元素分割出來 self.all_list.append(temp_list) #把分割后的每個元素重新添加到all_list列表中 current_list = fd.readline() #因為我們一次只讀一行。讀完后如果不加這一句那么循環會一直只讀第一行。 except: showinfo("系統信息","文件讀取出現異常.請聯系管理員!!") def load_TreeView(self,student_list:list): """ 1. 判斷all_list中的長度是否為0如果為0則沒有數據被加載 2. 把數據加載到treeView中 :return: """ #加載數據后清空TreeView for i in self.treeView.get_children(): #固定用法 self.treeView.delete(i) #加載數據 if len(student_list) == 0: #這里的student_list是個參數變量傳什么數據就是什么。這里最終傳的事all_list中的數據在load_TreeView(self.all_list)中展示 showinfo("系統消息","沒有數據被加載!") else: for index in range(len(student_list)): self.treeView.insert("",index,values = student_list[index]) def get_student_result(self): """ 1. 獲取輸入的值 2. 判斷如果輸入的值包含在all_list中那么把數據存入到get_number_result列表中 :return: 返回的是匹配到的學生編號的所有信息 """ #清空get_number_result中的數據(因為每次查詢一個stu_number就顯示一條.如果不清空就會每次查詢的結果都會被顯示) self.get_number_result.clear() #獲取輸入的值 get_input = [] get_input.append(self.label_stu_no.get().strip()) get_input.append(self.get_stu_name.get().strip()) get_input.append(self.get_stu_age.get().strip()) get_input.append(self.get_stu_mobile.get().strip()) get_input.append(self.get_stu_address.get().strip()) #print(get_input) #匹配學號把結果存入到get_number_result中(第一個元素學號必須填寫) for item in self.all_list: if get_input[0] in item[0] and get_input[1] in item[1] and get_input[2] in item[2] \ and get_input[3] in item[4] and get_input[4] in item[6]: self.get_number_result.append(item) #print(self.get_number_result) #把數據加載到TreeView中 self.load_TreeView(self.get_number_result) def load_all_data(self): """ 重新加載一下treeView中的所有數據就是展開全部信息 :return: """ #清空所有填寫的內容文本框 self.label_stu_no.set("") self.get_stu_name.set("") self.get_stu_age.set("") self.get_stu_mobile.set("") self.get_stu_address.set("") #把all_list中的數據加載到TreeView self.load_TreeView(self.all_list) def load_detail_windows(self): """ 創建load_detail_windows方法調用class detailgui模塊中的方法(加載學生明細窗體) :return: """ detail_window = dt.Detail_Windows(self.action_flag,self.current_stu_list,self.all_list)#detailgui中傳了幾個參數就要load的幾個參數 self.wait_window(detail_window) #通過self.wait_window()方法接收detail_window的值 return detail_window.comp_info def add_student(self): self.action_flag = 2 #如果action_flag值等於2則title值為添加學生信息 if self.load_detail_windows() == 1: self.load_all_data() else: return def update_student(self): self.action_flag = 3 #如果action_flag值等於3則title值為修改學生信息 #獲取雙擊行的數據 item = self.treeView.selection()[0] temp_stu_list = self.treeView.item(item, "values") # 因為雙擊行中的數據並不是我們想要的所有數據,因此我們還要遍歷all_list中的元素。通過雙擊行獲取的學號跟all_list中的學號做對比 # 如果兩個學號相等那么把all_list中對等的那一行的所有item存入到預備號的current_stu_list中 for item in self.all_list: if item[0] == temp_stu_list[0]: self.current_stu_list = item #加載數據 if self.load_detail_windows() == 1: self.load_all_data() else: return def view_student(self,event): """ 1.獲取選定行的數據 2.遍歷所有學員信息,當前學員學號等於所有學員信息中學號的信息那么就讀取選定行所有數據 3.重新加載數據 :param event: :return: """ self.action_flag = 1 #如果action_flag值等於1則title值為查看學生信息 # 固定方法獲取雙擊行的數據 item = self.treeView.selection()[0] temp_stu_list = self.treeView.item(item, "values") # 因為雙擊行中的數據並不是我們想要的所有數據,因此我們還要遍歷all_list中的元素。通過雙擊行獲取的學號跟all_list中的學號做對比 # 如果兩個學號相等那么把all_list中對等的那一行的所有item存入到預備號的current_stu_list中 for item in self.all_list: if item[0] == temp_stu_list[0]: self.current_stu_list = item self.load_detail_windows() def delete_student(self): #獲取選中行的數據 item = self.treeView.selection()[0] temp_stu_list = self.treeView.item(item, "values") #提醒是否刪除數據 choose = askyesno("刪除確認","確定要刪除學生信息【學號:" + temp_stu_list[0] + " 姓名:" + temp_stu_list[1] + " ] 信息嗎?") if choose: #如果是執行下面代碼 for index in range(len(self.all_list)): if self.all_list[index][0] == temp_stu_list[0]: self.all_list.pop(index) break #更新表格 self.load_all_data() showinfo("系統消息","數據刪除成功!") else: return def close_windows(self): """ 1.提醒用戶關閉窗口前是否保存數據 2.保存數居前先清空數據再保存數據 :return: """ choose = askyesno("系統關閉提醒","是否將修改的數據保存到文件?") if choose: try: with open(self.file_path,mode="w") as fd: fd.write("") with open(self.file_path,mode="a") as fd: for item in self.all_list: temp = ",".join(item) temp = temp.replace("\n","") + "\n" #為了讓光標換行 fd.write(temp) except: showinfo("系統消息","寫入文件出錯!") #提醒 showinfo("系統消息","文件寫入成功!") #關閉窗體 self.destroy() else: self.destroy() def change_password_windows(self): this_password_windows = ps.change_User_password(self.current_user_list) #把list綁定到change_password_windows中 if __name__ == "__main__": this_main_window = Main_Windows() this_main_window.mainloop()
三,用戶明細界面
思路: 1. GUI界面的設計 --> 同上同樣是通過tkinter進行設計
2. 查看的情況下隱藏save保存按鍵
3. 添加的情況下實現點擊save保存按鍵存入數據到TreeView中展示 --> 先不寫入到文件 --> 關閉主窗體時再寫入文件
4. 修改的情況下實現學號不允許修改其他允許修改
5. 修改密碼的情況下,綁定當前登錄的用戶,提示用戶輸入新舊密碼等
代碼實現:

from tkinter import * import tkinter.ttk as ttk from tkinter.messagebox import * class Detail_Windows(Toplevel): # 這里要用Toplevel不能用Tk因為tkinter只支持一個主窗體存在.要想再跳出一個窗體必須選Toplevel def __init__(self,action_flag:int,current_stu:list,all_stu_list:list): super().__init__() self.title("學生信息窗體") self.geometry("700x600+300+100") # 通過控制圖像大小可以通過+號來調整窗體出現的位置 self.resizable(0, 0) # 固定窗體大小.不允許改動窗體大小. resizable(0,0)表示x軸為0y軸也為0 #self.iconbitmap(R"C:\Users\Administrator\python_圖庫\china.ico") # 加載.ico文件 self["bg"] = "RoyalBlue" #設置全局變量 self.flag = action_flag #接收構造函數中action_flag中的值.(為了判斷傳進來的是添加,修改還是刪除等狀態值) self.current_student_list = current_stu # 接收構造函數中current_stu傳進來的值(為了接收選中行中的值) self.all_stu_list = all_stu_list #接收構造函數中all_stu_list傳進來的值(強調主窗體中all_list中的值最終跟我們子窗體中all_stu_list中的值相同.因為傳的是地址) # 加載窗體 self.Setup_UI() self.load_windows_flag() def Setup_UI(self): # 通過style設置屬性 self.Style01 = ttk.Style() self.Style01.configure("titel2.TLabel", font=("微軟眼黑", 24, "bold"), foreground="darkred") self.Style01.configure("TPanedwindow", background="lightcyan")#RoyalBlue self.Style01.configure("TButton", font=("微軟眼黑", 11), background="RoyalBlue", foreground="black") self.Style01.configure("TLabel",font=("微軟眼黑", 14, "bold"), foreground="black",background="lightcyan") self.Style01.configure("TRadiobutton",font=("微軟眼黑", 14, "bold"), foreground="black",background="lightcyan") #加載窗體圖片 self.login_img = PhotoImage(file = R"C:\Users\Administrator\PycharmProjects\untitled1\wangzhexiagu\GUI_student_system\photo\beijingtu.png") self.label_img = ttk.Label(self,image = self.login_img) self.label_img.pack() #添加Titile框體 self.var_titel = StringVar() self.title_label = ttk.Label(self,textvariable=self.var_titel,style = "titel2.TLabel") self.title_label.place(x = 20,y = 30) #添加一個Panewindows self.pane = ttk.PanedWindow(self,width=696, height=450, style="TPanedwindow") self.pane.place(x=2,y=98) #添加學員信息屬性 self.label_number = ttk.Label(self.pane,text = "學號",style="stu_number.TLabel") self.var_stunumber=StringVar() self.label_number.place(x=30, y=20) self.entry_number = ttk.Entry(self.pane,textvariable = self.var_stunumber,font=("微軟眼黑",12, "bold"),width=8) self.entry_number.place(x=80, y=20) self.label_name = ttk.Label(self.pane,text = "姓名",style="TLabel") self.var_stuname = StringVar() self.label_name.place(x=170, y=20) self.entry_name = ttk.Entry(self.pane,textvariable=self.var_stuname,font=("微軟眼黑",12, "bold"),width=11) self.entry_name.place(x=220, y=20) self.label_age = ttk.Label(self.pane,text = "年齡",style="TLabel") self.var_age = StringVar() self.label_age.place(x=323, y=20) self.entry_age = ttk.Entry(self.pane,textvariable=self.var_age,font=("微軟眼黑",12, "bold"),width=5) self.entry_age.place(x=370, y=20) self.Label_genter = ttk.Label(self.pane, text="性別:") self.Label_genter.place(x=440, y=20) self.var_genter = IntVar() self.radio_boy = ttk.Radiobutton(self, text="男", variable=self.var_genter, value= 0) self.radio_boy.place(x=520, y=116) self.radio_gril = ttk.Radiobutton(self, text="女", variable=self.var_genter, value=1) self.radio_gril.place(x=600, y=116) self.label_brithday = ttk.Label(self.pane,text = "出生日期",style="TLabel") self.var_brithday = StringVar() self.label_brithday.place(x=30, y=70) self.entry_brithday = ttk.Entry(self.pane,textvariable=self.var_brithday,font=("微軟眼黑",12, "bold"),width=15) self.entry_brithday.place(x=120, y=70) self.label_personid = ttk.Label(self.pane,text = "身份證號碼",style="TLabel") self.var_personid = StringVar() self.label_personid.place(x=320, y=70) self.entry_personid = ttk.Entry(self.pane,textvariable=self.var_personid,font=("微軟眼黑",12, "bold"),width=24) self.entry_personid.place(x=430, y=70) self.label_mobile = ttk.Label(self.pane,text = "手機號碼",style="TLabel") self.var_mobile = StringVar() self.label_mobile.place(x=30, y=120) self.entry_mobile = ttk.Entry(self.pane,textvariable=self.var_mobile,font=("微軟眼黑",12, "bold"),width=18) self.entry_mobile.place(x=120, y=120) self.label_mail = ttk.Label(self.pane,text = "郵箱地址",style="TLabel") self.var_mail = StringVar() self.label_mail.place(x=320, y=120) self.entry_mail = ttk.Entry(self.pane,textvariable=self.var_mail,font=("微軟眼黑",12, "bold"),width=24) self.entry_mail.place(x=430, y=120) self.label_homeaddress = ttk.Label(self.pane,text = "家庭住址",style="TLabel") self.var_homeaddress = StringVar() self.label_homeaddress.place(x=30, y=170) self.entry_homeaddress = ttk.Entry(self.pane,textvariable=self.var_homeaddress,font=("微軟眼黑",12, "bold"),width=59) self.entry_homeaddress.place(x=120, y=170) self.label_admission_time = ttk.Label(self.pane,text = "入學時間",style="TLabel") self.var_admission_time = StringVar() self.label_admission_time.place(x=30, y=220) self.entry_admission_time = ttk.Entry(self.pane,textvariable=self.var_admission_time,font=("微軟眼黑",12, "bold"),width=18) self.entry_admission_time.place(x=120, y=220) self.label_professional = ttk.Label(self.pane,text = "職業",style="TLabel") self.var_professional =StringVar() self.label_professional.place(x=320, y=220) self.entry_professional = ttk.Entry(self.pane,textvariable=self.var_professional,font=("微軟眼黑",12, "bold"),width=15) self.entry_professional.place(x=380, y=220) self.label_emergency_contact = ttk.Label(self.pane,text = "緊急聯系人",style="TLabel") self.var_emergency_contact = StringVar() self.label_emergency_contact.place(x=30, y=270) self.entry_emergency_contact = ttk.Entry(self.pane,textvariable=self.var_emergency_contact,font=("微軟眼黑",12, "bold"),width=15) self.entry_emergency_contact.place(x=150, y=270) self.label_emergency_mobile = ttk.Label(self.pane,text = "緊急聯系人電話",style="TLabel") self.var_emergency_mobile = StringVar() self.label_emergency_mobile.place(x=320, y=270) self.entry_emergency_mobile = ttk.Entry(self.pane,textvariable=self.var_emergency_mobile,font=("微軟眼黑",12, "bold"),width=20) self.entry_emergency_mobile.place(x=470, y=270) #添加保存,關閉按鈕 self.save_buttion = ttk.Button(self, text="保存", width=10,command = self.submit) self.save_buttion.place(x=480, y=560) self.exit_buttion = ttk.Button(self, text="退出", width=10,command = self.close_windows) self.exit_buttion.place(x=580, y=560) def load_windows_flag(self): """ 設置加載學員信息的titel值(通過調用Setup_UI(self)中textvariable=self.var_titel變量的方式) :return: """ if self.flag == 1: self.var_titel.set("== 查看學生信息 ==") self.load_stu_detail() #控制學員信息的狀態 self.save_buttion.place_forget() # 隱藏save_buttion按鍵的固定寫法 #通過控制entry變量中的["state"] = Disabled來禁用對象中的entry值.從而達到只讀的效果 self.entry_number["state"] = DISABLED self.entry_name["state"] = DISABLED self.entry_age["state"] = DISABLED self.radio_boy["state"] = DISABLED self.radio_gril["state"] = DISABLED self.entry_brithday["state"] = DISABLED self.entry_homeaddress["state"] = DISABLED self.entry_admission_time["state"] = DISABLED self.entry_mail["state"] = DISABLED self.entry_mobile["state"] = DISABLED self.entry_personid["state"] = DISABLED self.entry_professional["state"] = DISABLED self.entry_emergency_contact["state"] = DISABLED self.entry_emergency_mobile["state"] = DISABLED elif self.flag == 2: self.var_titel.set("== 添加學生信息 ==") elif self.flag == 3: self.var_titel.set("== 修改學生信息 ==") #先填充數據 self.load_stu_detail() #控制學號不允許修改 self.entry_number["state"] = DISABLED def load_stu_detail(self): """ 因為展示跟修改都需要調用后台數據.因此通過load_stu_detail方法進行調用 1.判斷傳入進來的值是否為0,如果是說明沒有數據被加載 2.有數據的情況通過變量.set()方法來匹配每個元素對應本地數據的下標 性別:因為性別分男女分別設定了0跟1.因此這里要判斷一下。一定要跟設定男女values的值對應上設定 :return: """ if len(self.current_student_list) == 0: showinfo("系統提示","沒有數據需要展示!") else: self.var_stunumber.set(self.current_student_list[0]) #學號 self.var_stuname.set(self.current_student_list[1]) #姓名 self.var_age.set(self.current_student_list[2]) #年齡 if "男" in self.current_student_list[3]: self.var_genter.set(0) else: self.var_genter.set(1) self.var_brithday.set(self.current_student_list[7]) self.var_mobile.set(self.current_student_list[4]) self.var_mail.set(self.current_student_list[5]) self.var_homeaddress.set(self.current_student_list[6]) self.var_personid.set(self.current_student_list[8]) self.var_admission_time.set(self.current_student_list[9]) self.var_professional.set(self.current_student_list[10]) self.var_emergency_contact.set(self.current_student_list[11]) self.var_emergency_mobile.set(self.current_student_list[12]) def close_windows(self): """ 關閉窗口 :return: """ self.comp_info = 0 self.destroy() def submit(self): if self.flag == 1: #view data pass elif self.flag == 2: #add data #准備追加學生的數據 temp_list = [] if len(str(self.entry_number.get()).strip()) == 0: showinfo("系統消息","學號不能為空!") else: temp_list.append(str(self.entry_number.get()).strip()) temp_list.append(str(self.entry_name.get()).strip()) temp_list.append(str(self.entry_age.get()).strip()) if self.var_genter.get() == 0: temp_list.append("男") else: temp_list.append("女") temp_list.append(str(self.entry_mobile.get()).strip()) temp_list.append(str(self.entry_mail.get()).strip()) temp_list.append(str(self.entry_homeaddress.get()).strip()) temp_list.append(str(self.entry_brithday.get()).strip()) temp_list.append(str(self.entry_personid.get()).strip()) temp_list.append(str(self.entry_admission_time.get()).strip()) temp_list.append(str(self.entry_professional.get()).strip()) temp_list.append(str(self.entry_emergency_contact.get()).strip()) temp_list.append(str(self.entry_emergency_mobile.get()).strip()) #追加數據 self.all_stu_list.append(temp_list) #提醒 showinfo("系統信息","學生信息添加成功!") #反饋信號給主窗體 self.comp_info = 1 self.destroy() elif self.flag == 3: #update data temp_list = [] if len(str(self.entry_number.get()).strip()) == 0: showinfo("系統消息", "學號不能為空!") return else: temp_list.append(str(self.entry_number.get()).strip()) temp_list.append(str(self.entry_name.get()).strip()) temp_list.append(str(self.entry_age.get()).strip()) if self.var_genter.get() == 0: temp_list.append("男") else: temp_list.append("女") temp_list.append(str(self.entry_mobile.get()).strip()) temp_list.append(str(self.entry_mail.get()).strip()) temp_list.append(str(self.entry_homeaddress.get()).strip()) temp_list.append(str(self.entry_brithday.get()).strip()) temp_list.append(str(self.entry_personid.get()).strip()) temp_list.append(str(self.entry_admission_time.get()).strip()) temp_list.append(str(self.entry_professional.get()).strip()) temp_list.append(str(self.entry_emergency_contact.get()).strip()) temp_list.append(str(self.entry_emergency_mobile.get()).strip()) #遍歷集合 for index in range(len(self.all_stu_list)): if self.all_stu_list[index][0] == self.current_student_list[0]: self.all_stu_list[index] = temp_list # 提醒 showinfo("系統信息", "學生信息修改成功!") # 反饋信號給主窗體 self.comp_info = 1 # 關閉窗口 self.destroy() if __name__ == "__main__": this_windows = Detail_Windows() this_windows.mainloop()
三,用戶修改密碼界面
#難點並不多因此直接上代碼
代碼實現:

from tkinter import * import tkinter.ttk as ttk from tkinter.messagebox import * import os class change_User_password(Toplevel): # 這里要用Toplevel不能用Tk因為tkinter只支持一個主窗體存在.要想再跳出一個窗體必須選Toplevel def __init__(self,current_user_list): super().__init__() self.title("修改密碼") self.geometry("600x500+300+100") # 通過控制圖像大小可以通過+號來調整窗體出現的位置 self.resizable(0, 0) # 固定窗體大小.不允許改動窗體大小. resizable(0,0)表示x軸為0y軸也為0 #self.iconbitmap(R"C:\Users\Administrator\python_圖庫\china.ico") # 加載.ico文件 self["bg"] = "RoyalBlue" #定義全局變量 self.current_user_list = current_user_list #同樣在change_User_password窗口中接收list的地址 self.file_path = R"C:\Users\Administrator\PycharmProjects\untitled1\wangzhexiagu\GUI_student_system\loginUserInfo_Data\loginuser_info.txt" self.get_all_userlist = [] #獲取所有的賬號密碼 #加載setup_UI self.Setup_UI() #初始化 self.var_loginuser.set(self.current_user_list[0]) #初始化登錄的賬號 #自動加載 self.get_all_loginuser() def Setup_UI(self): # 通過style設置屬性 self.Style01 = ttk.Style() self.Style01.configure("titel3.TLabel", font=("微軟眼黑", 20, "bold"), foreground="darkred") self.Style01.configure("TPanedwindow", background="lightcyan")#RoyalBlue self.Style01.configure("TButton", font=("微軟眼黑", 11), background="RoyalBlue", foreground="black") self.Style01.configure("TLabel",font=("微軟眼黑", 14, "bold"), foreground="black",background="lightcyan") self.Style01.configure("TRadiobutton",font=("微軟眼黑", 14, "bold"), foreground="black",background="lightcyan") #加載窗體圖片 self.login_img = PhotoImage(file = R"C:\Users\Administrator\PycharmProjects\untitled1\wangzhexiagu\GUI_student_system\photo\beijingtu.png") self.label_img = ttk.Label(self,image = self.login_img) self.label_img.pack() #添加Titile框體 self.title_label = ttk.Label(self,text="== 更改密碼 ==",style = "titel3.TLabel") self.title_label.place(x = 20,y = 30) #添加一個Panewindows self.pane = ttk.PanedWindow(self,width=596, height=360, style="TPanedwindow") self.pane.place(x=2,y=98) self.label_login_user = ttk.Label(self.pane,text = "登錄賬號",style="stu_number.TLabel") self.var_loginuser=StringVar() self.label_login_user.place(x=170, y=60) self.entry_login_user = ttk.Entry(self.pane,state = DISABLED,textvariable = self.var_loginuser,font=("微軟眼黑",14, "bold"),width=16) self.entry_login_user.place(x=270, y=60) self.label_old_password = ttk.Label(self.pane,text = "舊密碼",style="stu_number.TLabel") self.var_old_password=StringVar() self.label_old_password.place(x=170, y=120) self.entry_old_password = ttk.Entry(self.pane,show = "*",textvariable = self.var_old_password,font=("微軟眼黑",14, "bold"),width=16) self.entry_old_password.place(x=270, y=120) self.label_new_password = ttk.Label(self.pane,text = "新密碼",style="stu_number.TLabel") self.var_new_password=StringVar() self.label_new_password.place(x=170, y=180) self.entry_new_password = ttk.Entry(self.pane,show = "*",textvariable = self.var_new_password,font=("微軟眼黑",14, "bold"),width=16) self.entry_new_password.place(x=270, y=180) self.label_reinput_password = ttk.Label(self.pane,text = "重新輸入新密碼",style="stu_number.TLabel") self.var_reinput_password=StringVar() self.label_reinput_password.place(x=120, y=240) self.entry_reinput_password = ttk.Entry(self.pane,show = "*",textvariable = self.var_reinput_password,font=("微軟眼黑",14, "bold"),width=16) self.entry_reinput_password.place(x=270, y=240) #添加保存,關閉按鈕 self.save_buttion = ttk.Button(self, text="保存", width=10,command = self.save_pass_windows) self.save_buttion.place(x=380, y=470) self.exit_buttion = ttk.Button(self, text="退出", width=10,command = self.close_pass_windows) self.exit_buttion.place(x=480, y=470) def close_pass_windows(self): self.destroy() def save_pass_windows(self): # 獲取登錄賬號相關的值 old_password = self.var_old_password.get() new_password = self.var_new_password.get() second_password = self.var_reinput_password.get() #判斷舊密碼是否真確 if old_password != self.current_user_list[1]: showinfo("系統消息","輸入的舊密碼不正確!") return #判斷新密碼跟舊密碼是否相同 if new_password == old_password: showinfo("系統消息", "新密碼不能跟舊密碼相同!") return #判斷輸入的新密碼不能為空 if len(new_password.strip()) == 0: showinfo("系統消息", "新密碼不能為空!") return #兩次密碼輸入是否相同 if new_password != second_password: showinfo("系統消息", "兩次密碼輸入不一致!") return #修改密碼 for index in range(len(self.get_all_userlist)): if self.get_all_userlist[index][0] == self.current_user_list[0]: self.get_all_userlist[index][1] = new_password # 完成更改(寫入文件) try: with open(self.file_path,mode="w") as fd: fd.write("") with open(self.file_path,mode="a") as fd: for item in self.get_all_userlist: temp = ",".join(item) temp = temp.replace("\n","") + "\n" fd.write(temp) except: showinfo("系統消息","寫入文件出錯.請聯系管理員") #提示 showinfo("系統消息", "密碼修改成功!") #關閉窗口 self.destroy() def get_all_loginuser(self): """ 讀取文件獲取所有的賬號信息保存到get_all :return: """ if not os.path.exists(self.file_path): showinfo("系統提示","文件不存在.請確認后重新加載!!") else: try: with open(self.file_path,mode = "r") as fd: current_list = fd.readline() #一次讀一行 while current_list:#表示直到current_list中所有元素被讀完,循環結束 temp_list = current_list.split(",") #按照逗號把每個list中的元素分割出來 self.get_all_userlist.append(temp_list) #把分割后的每個元素重新添加到all_list列表中 current_list = fd.readline() #因為我們一次只讀一行。讀完后如果不加這一句那么循環會一直只讀第一行。 except: showinfo("系統信息","文件讀取出現異常.請聯系管理員!!") if __name__ == "__main__": this_windows = change_User_password() this_windows.mainloop()