本篇內(nèi)容介紹了“Python GUI主窗體的界面設(shè)計與實現(xiàn)方法是什么”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
成都創(chuàng)新互聯(lián)不只是一家網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司;我們對營銷、技術(shù)、服務(wù)都有自己獨特見解,公司采取“創(chuàng)意+綜合+營銷”一體化的方式為您提供更專業(yè)的服務(wù)!我們經(jīng)歷的每一步也許不一定是最完美的,但每一步都有值得深思的意義。我們珍視每一份信任,關(guān)注我們的成都網(wǎng)站設(shè)計、網(wǎng)站制作質(zhì)量和服務(wù)品質(zhì),在得到用戶滿意的同時,也能得到同行業(yè)的專業(yè)認(rèn)可,能夠為行業(yè)創(chuàng)新發(fā)展助力。未來將繼續(xù)專注于技術(shù)創(chuàng)新,服務(wù)升級,滿足企業(yè)一站式營銷型網(wǎng)站建設(shè)需求,讓再小的品牌網(wǎng)站制作也能產(chǎn)生價值!
我們新建一個900x640的窗口,頂部加入圖片,下面主體部分創(chuàng)建兩個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): # 設(shè)定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() # 左邊:按鈕區(qū)域,創(chuàng)建一個容器 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 = "添加學(xué)生",style = "TButton") self.Button_add.place(x = 40,y = 20) self.Button_update = Button(self.Pane_left, text="修改學(xué)生", ) self.Button_update.place(x=40, y=45) self.Button_delete = Button(self.Pane_left, text="刪除學(xué)生", ) self.Button_delete.place(x=40, y=70) self.Button_modify = Button(self.Pane_left, text="更改密碼", ) self.Button_modify.place(x=40, y=120) # 右邊:查詢、TreeView if __name__ == '__main__': this_main = MainWindow() this_main.mainloop()
在右邊的Pannedwindow容器中,添加一個LabelFrame容器作為查詢區(qū)域,在LabelFrame容器中添加一系列的Label、Entry、Button控件,可以輸入學(xué)號、姓名、電話、身份證、查詢、和顯示全部信息:
self.Pane_right = PanedWindow(width=725, height=540, ) self.Pane_right.place(x=170, y=94) # LabelFrame self.LabelFrame_query = LabelFrame(self.Pane_right,text = "學(xué)生信息查詢",width = 700,height = 70) self.LabelFrame_query.place(x = 10 , y = 10) # 添加控件 self.Label_sno = Label(self.LabelFrame_query,text = "學(xué)號:") 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)
創(chuàng)建控件、設(shè)置對齊方式和每個列的標(biāo)題
# 添加TreeView控件 self.Tree = Treeview(self.Pane_right,columns=("sno","names", "gender","birthday","mobile","email","address"),show="headings",height=20) # 設(shè)置每一個列的寬度和對齊的方式 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") # 設(shè)置每個列的標(biāo)題 self.Tree.heading("sno",text="學(xué)號") self.Tree.heading("names", text="姓名") self.Tree.heading("gender", text="性別") self.Tree.heading("birthday", text="生日") self.Tree.heading("mobile", text="手機(jī)號碼") self.Tree.heading("email", text="郵箱地址") self.Tree.heading("address", text="家庭住址") self.Tree.place(x=10,y=80)
登錄成功后,在頂部顯示用戶姓名和登錄時間,用戶姓名是怎么來的?是我們在登錄窗口輸入的,所以這就涉及到了跨窗體數(shù)據(jù)的傳遞。這一點非常重要!登錄窗體(登錄信息)==>主窗體 傳遞的基本方式:構(gòu)造函數(shù) 在主窗體的構(gòu)造函數(shù)中添加一個接收參數(shù)current_user,在登錄窗體加載新窗體時將參數(shù)傳遞進(jìn)去; 但是我們登錄窗體的登錄函數(shù)login()中用戶名的變量user是局部變量,函數(shù)調(diào)用完了之后就變量就沒有了,那怎么調(diào)用呢?我們需要在登錄窗體的構(gòu)造函數(shù)中定義全局變量:
self.user = "" # 當(dāng)前的用戶
為了獲取用戶登錄的時間,我們定義一個獲取當(dāng)前時間的方法:
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))
然后在加載主窗體時將參數(shù)self.user和self.get_now_time()作為參數(shù)傳遞進(jìn)去
main_window = maingui.MainWindow(self.user,self.get_now_time())
另一邊,我們在主窗體中,在構(gòu)造函數(shù)中添加全局變量
self.login_user = current_user self.login_time = current_time
之后,我們在Top_banner中通過標(biāo)簽將user信息展示出來:
self.Label_login_user = Label(self,text = "當(dāng)前用戶:"+str(self.login_user).title() +"\n登錄時間:"+self.login_time) self.Label_login_user.place(x = 650,y = 40)
這樣主窗口就會顯示通過登錄窗口登錄的用戶名(首字母自動轉(zhuǎn)大寫)和登錄時間:效果演示:
1. 我們在主窗體中定義全局變量來存儲學(xué)生信息:
self.all_student_list = [] self.file_path = "/Users/yushengtan/Desktop/Demo/Studentmgr/Student.txt"
2. 定義方法讀取文件中的學(xué)生信息
def load_file_student_info(self): if not os.path.exists(self.file_path): showinfo("系統(tǒng)消息","提供的文件名不存在!") 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) # 讀取下一行,讀完了循環(huán)就結(jié)束了 current_line = fd.readline() except: showinfo("系統(tǒng)消息","文件讀取出現(xiàn)異常!")
然后我們在構(gòu)造方法中把這個函數(shù)寫入,以實現(xiàn)自動把學(xué)生信息寫入到all_student_list中
self.load_file_student_info()
3. 定義加載TreeView信息的方法
文件中讀取到的學(xué)生信息存儲到all_student_list列表,以此作為參數(shù)傳入加載TreeView的方法中;
def load_treeview(self,current_list:list): # 判斷是否有數(shù)據(jù): if len(current_list) == 0: showinfo("系統(tǒng)消息","沒有數(shù)據(jù)加載") 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]))
在構(gòu)造方法中調(diào)用該方法,自動把所有學(xué)生信息加載到TreeView中
self.load_treeview(self.all_student_list)
“Python GUI主窗體的界面設(shè)計與實現(xiàn)方法是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!