前言
上一節我們介紹了Python GUI項目實戰(一)登錄窗體的設計與實現,實現了該項目登錄窗體的GUI的搭建,用戶的賬號和密碼校驗完成後應當跳轉到主窗體界面,這一節我們將具體介紹主窗體界面的設計與功能實現!
一、基礎界面設計
我們新建一個900x640的窗口,頂部加入圖片,下面主體部分創建兩個Panedwindow容器,左邊添加按鈕,右邊作為TreeView顯示界面;
<code>from tkinter import * from tkinter.ttk import * import os class MainWindow(Tk): def __init__(self): super().__init__() self.title("主窗體") self.geometry("900x640+180+80") self.resizable(0,0) self["bg"]="skyblue" # 加載gui self.setup_UI() def setup_UI(self): # 設定Style self.Style01 = Style() self.Style01.configure("left.TPanedwindow",background = "navy") self.Style01.configure("right.TPanedwindow", background="skyblue") self.Style01.configure("TButton",width = 10,font = ("華文黑體",15,"bold")) # Top_banner self.Login_image = PhotoImage(file = "."+os.sep+"img"+os.sep+"stu_main_top_banner.png") self.Lable_image = Label(self,image = self.Login_image) self.Lable_image.pack() # 左邊:按鈕區域,創建一個容器 self.Pane_left = PanedWindow(width = 200,height = 540,style = "left.TPanedwindow") self.Pane_left.place(x = 4,y = 94) self.Pane_right = PanedWindow(width=685, height=540,style = "right.TPanedwindow") self.Pane_right.place(x = 210,y = 94) # 添加左邊按鈕 self.Button_add = Button(self.Pane_left,text = "添加學生",style = "TButton") self.Button_add.place(x = 40,y = 20) self.Button_update = Button(self.Pane_left, text="修改學生", style="TButton") self.Button_update.place(x=40, y=45) self.Button_delete = Button(self.Pane_left, text="刪除學生", style="TButton") self.Button_delete.place(x=40, y=70) self.Button_modify = Button(self.Pane_left, text="更改密碼", style="TButton") self.Button_modify.place(x=40, y=120) # 右邊:查詢、TreeView if __name__ == '__main__': this_main = MainWindow() this_main.mainloop()/<code>
顯示效果:
注意:tkinter在Mac上Panedwindow不支持修改前景色背景色
二、添加查詢區域
在右邊的Pannedwindow容器中,添加一個LabelFrame容器作為查詢區域,在LabelFrame容器中添加一系列的Label、Entry、Button控件,可以輸入學號、姓名、電話、身份證、查詢、和顯示全部信息:
<code>self.Pane_right = PanedWindow(width=725, height=540, style="right.TPanedwindow") self.Pane_right.place(x=170, y=94) # LabelFrame self.LabelFrame_query = LabelFrame(self.Pane_right,text = "學生信息查詢",width = 700,height = 70) self.LabelFrame_query.place(x = 10 , y = 10) # 添加控件 self.Label_sno = Label(self.LabelFrame_query,text = "學號:") self.Label_sno.place(x = 5,y = 13) self.Entry_sno = Entry(self.LabelFrame_query,width = 8) self.Entry_sno.place(x = 40,y = 10) self.Label_name = Label(self.LabelFrame_query, text="姓名:") self.Label_name.place(x=125, y=13) self.Entry_name = Entry(self.LabelFrame_query, width=8) self.Entry_name.place(x=160, y=10) self.Label_mobile = Label(self.LabelFrame_query, text="電話:") self.Label_mobile.place(x=245, y=13) self.Entry_mobile = Entry(self.LabelFrame_query, width=8) self.Entry_mobile.place(x=280, y=10) self.Label_id = Label(self.LabelFrame_query, text="身份證:") self.Label_id.place(x=365, y=13) self.Entry_id = Entry(self.LabelFrame_query, width=10) self.Entry_id.place(x=420, y=10) self.Button_query = Button(self.LabelFrame_query, text="查詢",width = 4) self.Button_query.place(x=520, y=10) self.Button_all = Button(self.LabelFrame_query, text="顯示全部",width = 8) self.Button_all.place(x=590, y=10)/<code>
顯示效果:
三、加載Treeview控件
創建控件、設置對齊方式和每個列的標題
<code># 添加TreeView控件 self.Tree = Treeview(self.Pane_right,columns=("sno","names", "gender","birthday","mobile","email","address"),show="headings",height=20) # 設置每一個列的寬度和對齊的方式 self.Tree.column("sno",width=100,anchor="center") self.Tree.column("names",width=80,anchor="center") self.Tree.column("gender",width=80,anchor="center") self.Tree.column("birthday",width=100,anchor="center") self.Tree.column("mobile",width=100,anchor="center") self.Tree.column("email", width=100, anchor="center") self.Tree.column("address",width=120,anchor="center") # 設置每個列的標題 self.Tree.heading("sno",text="學號") self.Tree.heading("names", text="姓名") self.Tree.heading("gender", text="性別") self.Tree.heading("birthday", text="生日") self.Tree.heading("mobile", text="手機號碼") self.Tree.heading("email", text="郵箱地址") self.Tree.heading("address", text="家庭住址") self.Tree.place(x=10,y=80)/<code>
顯示效果:
四、實現登錄用戶登錄信息加載
登錄成功後,在頂部顯示用戶姓名和登錄時間,用戶姓名是怎麼來的?是我們在登錄窗口輸入的,所以這就涉及到了跨窗體數據的傳遞。這一點非常重要! 登錄窗體(登錄信息)==>主窗體 傳遞的基本方式:構造函數 在主窗體的構造函數中添加一個接收參數current_user,在登錄窗體加載新窗體時將參數傳遞進去; 但是我們登錄窗體的登錄函數login()中用戶名的變量user是局部變量,函數調用完了之後就變量就沒有了,那怎麼調用呢? 我們需要在登錄窗體的構造函數中定義全局變量:
<code>self.user = "" # 當前的用戶/<code>
為了獲取用戶登錄的時間,我們定義一個獲取當前時間的方法:
<code>def get_now_time(self): today = datetime.today() return ("%04d-%02d-%02d %02d:%02d:%02d"%(today.year, today.month,today.day,today.hour,today.minute,today.second))/<code>
然後在加載主窗體時將參數self.user和self.get_now_time()作為參數傳遞進去
<code>main_window = maingui.MainWindow(self.user,self.get_now_time())/<code>
另一邊,我們在主窗體中,在構造函數中添加全局變量
<code>self.login_user = current_user self.login_time = current_time/<code>
之後,我們在Top_banner中通過標籤將user信息展示出來:
<code>self.Label_login_user = Label(self,text = "當前用戶:"+str(self.login_user).title() +"\n登錄時間:"+self.login_time) self.Label_login_user.place(x = 650,y = 40)/<code>
這樣主窗口就會顯示通過登錄窗口登錄的用戶名(首字母自動轉大寫)和登錄時間: 效果演示:
五、加載學生信息到TreeView中
1. 我們在主窗體中定義全局變量來存儲學生信息:
<code>self.all_student_list = [] self.file_path = "/Users/yushengtan/Desktop/Demo/Studentmgr/Student.txt"/<code>
2. 定義方法讀取文件中的學生信息
<code>def load_file_student_info(self): if not os.path.exists(self.file_path): showinfo("系統消息","提供的文件名不存在!") else: try: with open(file = self.file_path,mode = "r") as fd: # 一次讀一行 current_line = fd.readline() while current_line: temp_list = current_line.split(",") # 長字符串分割層三個 self.all_student_list.append(temp_list) # 讀取下一行,讀完了循環就結束了 current_line = fd.readline() except: showinfo("系統消息","文件讀取出現異常!")/<code>
然後我們在構造方法中把這個函數寫入,以實現自動把學生信息寫入到all_student_list中
<code>self.load_file_student_info()/<code>
3. 定義加載TreeView信息的方法
文件中讀取到的學生信息存儲到all_student_list列表,以此作為參數傳入加載TreeView的方法中;
<code>def load_treeview(self,current_list:list): # 判斷是否有數據: if len(current_list) == 0: showinfo("系統消息","沒有數據加載") else: for index in range(len(current_list)): self.Tree.insert("",index,values=(current_list[index][0],current_list[index][1], current_list[index][2],current_list[index][3],current_list[index][4], current_list[index][5],current_list[index][6]))/<code>
在構造方法中調用該方法,自動把所有學生信息加載到TreeView中
<code>self.load_treeview(self.all_student_list) /<code>
運行效果:
最後
這一節我們實現了主窗體的搭建,從界面的佈局到TreeView加載全部學生信息。學生數據如此之多,如果我們想精確查看具體某個學生的信息該怎麼做呢?下一講,我們將實現學生信息的查詢功能,敬請期待吧~
小夥伴們如果覺得這篇文章還不錯的話,那就點個讚唄;如果覺得很有用的話,那就轉發一下吧;如果覺得下次還不想錯過,那就關注一下嘍~哈哈哈哈,感謝有你的支持~