網站首頁 編程語言 正文
GUI應用程序類的經典寫法 — 面向對象
通過類Application組織整個GUI程序,類Application繼承了Frame以及通過繼承擁有了父類的特性,通過__init__初始化窗口中的對象,通過createWidget()
創建窗口中的對象
Frame框架是一個Tkiner的組件,表示一個矩形的區域,一般作為容器使用,可以放置其他組件,從而實現復雜的布局
經典的GUI程序類的寫法
效果的基本示意圖
from tkinter import * from tkinter import messagebox class Application(Frame): # 繼承Frame """ 一個經典的GUI程序類的寫法 """ def __init__(self, master=None): """ 構造器,用來構造組件對象 :param master: """ super().__init__(master) # super() 代表的是父類的定義,而不是父類的對象 self.master = master self.pack() self.createWidget() # 再組件中創建其他組件 def createWidget(self): """創建組件,可以構造函數中調用""" self.btn01 = Button(self) self.btn01['text'] = '點擊送花' self.btn01.pack() self.btn01['command'] = self.songhua # 創建一個退出按鈕 # command綁定事件對象(相應函數) self.btn02 = Button(self, text='退出', command=root.destroy) self.btn02.pack() # 通過布局管理器放到組件中 def songhua(self): # e就是事件對象 messagebox.showinfo("Message", "送你一朵玫瑰花") print('送你玫瑰花') if __name__ == '__main__': root = Tk() # 根窗口對象 root.geometry("400x100+300+300") root.title("一個經典那的GUI程序的測試") # 創建一個Application對象 app = Application(master=root) # master=root 創建的對象放到root里面 root.mainloop()
實際效果圖
Label 的介紹
參數的介紹
Label(text='程序員', width=10, height=2, bg='blue',fg='white',font=("黑體",30),justify='center') """ text 文本內容 width 寬度 一個英文占1個,一個漢字占2個 height 高度 bg 背景 fg 前景色 font=() 指定字體和大小 justify 多行文字的對齊方式 """
使用Label標簽
添加內容,設置高度寬度,背景顏色,前景色,修改顯示的字體和字號
self.label01 = Label(self,text='程序員',width=10,height=2,bg='black',fg='white') self.label01.pack() self.label02 = Label(self, text='程序員2', width=10, height=2, bg='blue', fg='white',font=("黑體",30),justify='center') self.label02.pack()
使用Lable標簽顯示圖像
注意要將圖片對象聲明為全局變量
# 顯示圖像 global photo # 將phone聲明成全局變量,如果是局部變量,本方法執行完圖像就會銷毀,窗口顯出不出來 photo = PhotoImage(file="py.png") # 圖像的路徑,有些照片的類型python可能不支持,需要額外注意 self.label03 = Label(self,image =photo,width=300,height=200) self.label03.pack()
多行文字對齊的方式選擇
self.label04 = Label(self, text="圖片\n我有一個圖片\n一個好看的圖片", borderwidth=1,relief="solid",justify='right') self.label04.pack()
全部源代碼
"""測試Label組件的基本用法,使用面向對象的方式 """ from tkinter import * class Application(Frame): def __init__(self, master=None): super().__init__(master) # super() 代表的是父類的定義,而不是父類的對象 self.master = master self.pack() self.createWidget() def createWidget(self): """創建組件""" self.label01 = Label(self,text='程序員',width=10,height=2,bg='black',fg='white') self.label01.pack() self.label02 = Label(self, text='程序員2', width=10, height=2, bg='blue', fg='white',font=("黑體",30),justify='center') self.label02.pack() # 顯示圖像 global photo # 將phone聲明成全局變量,如果是局部變量,本方法執行完圖像就會銷毀,窗口顯出不出來 photo = PhotoImage(file="py.png") # 圖片支持gif對象 self.label03 = Label(self,image =photo,width=300,height=200) self.label03.pack() self.label04 = Label(self, text="圖片\n我有一個圖片\n一個好看的圖片", borderwidth=1,relief="solid",justify='right') self.label04.pack() if __name__ == '__main__': root = Tk() root.geometry("600x400+300+300") root.title("Label的測試") app = Application(master=root) root.mainloop()
原文鏈接:https://blog.csdn.net/qq_52007481/article/details/126794061
相關推薦
- 2022-04-12 如何在Python中編寫接口和請求外部接口_python
- 2022-09-04 c++?求數組最大最小值函數的實現_C 語言
- 2024-01-05 TCP、IP、TCP/IP、HTTP和HTTPS協議簡介
- 2024-03-20 SpringBoot使用dynamic-datasource實現多數據源方案
- 2022-06-21 分享python?寫?csv?文件的兩種方法_python
- 2023-05-30 python?snap7讀寫PLC的操作方法_python
- 2022-10-28 Python利用Rows快速操作csv文件_python
- 2022-05-21 淺談GO中的Channel以及死鎖的造成_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支