前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理,
作者:小雨tt
PS:如有需要Python學習資料的小伙伴可以加下方的群去找免費管理員領取
可以免費領取原始碼、專案實戰視頻、PDF檔案等
一、基礎界面設計
我們新建一個900x640的視窗,頂部加入圖片,下面主體部分創建兩個Panedwindow容器,左邊添加按鈕,右邊作為TreeView顯示界面;
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()
顯示效果:
注意:tkinter在Mac上Panedwindow不支持修改前景色背景色
二、添加查詢區域
在右邊的Pannedwindow容器中,添加一個LabelFrame容器作為查詢區域,在LabelFrame容器中添加一系列的Label、Entry、Button控制元件,可以輸入學號、姓名、電話、身份證、查詢、和顯示全部資訊:
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)
顯示效果:
三、加載Treeview控制元件
創建控制元件、設定對齊方式和每個列的標題
# 添加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)
顯示效果:
四、實作登錄用戶登錄資訊加載
登錄成功后,在頂部顯示用戶姓名和登錄時間,用戶姓名是怎么來的?是我們在登錄視窗輸入的,所以這就涉及到了跨表單資料的傳遞,這一點非常重要!登錄表單(登錄資訊)==>主表單 傳遞的基本方式:建構式 在主表單的建構式中添加一個接收引數current_user,在登錄表單加載新表單時將引數傳遞進去; 但是我們登錄表單的登錄函式login()中用戶名的變數user是區域變數,函式呼叫完了之后就變數就沒有了,那怎么呼叫呢?我們需要在登錄表單的建構式中定義全域變數:
self.user = "" # 當前的用戶
為了獲取用戶登錄的時間,我們定義一個獲取當前時間的方法:
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))
然后在加載主表單時將引數self.user和self.get_now_time()作為引數傳遞進去
main_window = maingui.MainWindow(self.user,self.get_now_time())
另一邊,我們在主表單中,在建構式中添加全域變數
self.login_user = current_user
self.login_time = current_time
之后,我們在Top_banner中通過標簽將user資訊展示出來:
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)
這樣主視窗就會顯示通過登錄視窗登錄的用戶名(首字母自動轉大寫)和登錄時間:效果演示:
五、加載學生資訊到TreeView中
1. 我們在主表單中定義全域變數來存盤學生資訊:
self.all_student_list = [] self.file_path = "/Users/yushengtan/Desktop/Demo/Studentmgr/Student.txt"
2. 定義方法讀取檔案中的學生資訊
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("系統訊息","檔案讀取出現例外!")
然后我們在構造方法中把這個函式寫入,以實作自動把學生資訊寫入到all_student_list中
self.load_file_student_info()
3. 定義加載TreeView資訊的方法
檔案中讀取到的學生資訊存盤到all_student_list串列,以此作為引數傳入加載TreeView的方法中;
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]))
在構造方法中呼叫該方法,自動把所有學生資訊加載到TreeView中
self.load_treeview(self.all_student_list)
運行效果:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/61895.html
標籤:Python
上一篇:教孩子學編程-Python語言版 電子書籍現在限時免費獲取!
下一篇:99恢復二叉樹
