溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Python GUI主窗體的界面設計與實現(xiàn)方法是什么

發(fā)布時間:2021-11-25 14:59:55 來源:億速云 閱讀:294 作者:iii 欄目:大數據

本篇內容介紹了“Python GUI主窗體的界面設計與實現(xià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):
        # 設定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 = "添加學生",style = "TButton")
        self.Button_add.place(x = 40,y = 20)
        self.Button_update = Button(self.Pane_left, text="修改學生", )
        self.Button_update.place(x=40, y=45)
        self.Button_delete = Button(self.Pane_left, text="刪除學生", )
        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()

二、添加查詢區(qū)域

在右邊的Pannedwindow容器中,添加一個LabelFrame容器作為查詢區(qū)域,在LabelFrame容器中添加一系列的Label、Entry、Button控件,可以輸入學號、姓名、電話、身份證、查詢、和顯示全部信息:

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 = "學生信息查詢",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)

顯示效果:

Python GUI主窗體的界面設計與實現(xiàn)方法是什么

三、加載Treeview控件

創(chuàng)建控件、設置對齊方式和每個列的標題

# 添加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)

四、實現(xiàn)登錄用戶登錄信息加載

登錄成功后,在頂部顯示用戶姓名和登錄時間,用戶姓名是怎么來的?是我們在登錄窗口輸入的,所以這就涉及到了跨窗體數據的傳遞。這一點非常重要!登錄窗體(登錄信息)==>主窗體 傳遞的基本方式:構造函數 在主窗體的構造函數中添加一個接收參數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)

這樣主窗口就會顯示通過登錄窗口登錄的用戶名(首字母自動轉大寫)和登錄時間:效果演示:

Python GUI主窗體的界面設計與實現(xiàn)方法是什么

五、加載學生信息到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("系統(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)就結束了
                    current_line = fd.readline()
        except:
            showinfo("系統(tǒng)消息","文件讀取出現(xiàn)異常!")

然后我們在構造方法中把這個函數寫入,以實現(xiàn)自動把學生信息寫入到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("系統(tǒng)消息","沒有數據加載")
    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)

“Python GUI主窗體的界面設計與實現(xiàn)方法是什么”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI