TShopping

 找回密碼
 註冊
搜索
查看: 1715|回復: 0
打印 上一主題 下一主題

[教學] Tkinter 教程 - 標籤控制元件

[複製鏈接]
跳轉到指定樓層
1#
發表於 2020-11-26 15:56:59 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
  • 更改標籤文字字型
  • 更改標籤顏色
  • 標籤中顯示圖片
label 標籤控制元件通過用來顯示靜態的文字或者影象,它的內容一般來說不是動態的。當然,你可以根據你的需求來改變它的內容。

Tkinter Label.py
  1. from sys import version_info
  2. if version_info.major == 2:
  3.     import Tkinter as tk
  4. elif version_info.major == 3:
  5.     import tkinter as tk
  6.    
  7. app = tk.Tk()
  8. labelExample = tk.Label(app, text="This is a Label")
  9. labelExample.pack()
  10. app.mainloop()
複製代碼


程式執行後,會在主程式視窗內生成一個文字標籤。

labelExample = tk.Label(app, text = "This is a label")
labelExample 是在主視窗 app 內的一個顯示 This is a label 的標籤例項。


  1. labelExample.pack()
複製代碼

pack() 方法用來對主控制元件裡面的小控制元件來進行佈局分佈。它有如下的引數列表,
pack() 方法
描述
after=widgetpack it after you have packed widget
anchor=NSEW (or subset)position widget according to
before=widgetpack it before you will pack widget
expand=boolexpand widget if parent size grows
fill=NONE or X or Y or BOTHfill widget if widget grows
in=masteruse master to contain this widget
in_=mastersee ‘in’ option description
ipadx=amountadd internal padding in x direction
ipady=amountadd internal padding in y direction
padx=amountadd padding in x direction
pady=amountadd padding in y direction
side=TOP or BOTTOM or LEFT or RIGHTwhere to add this widget.
通過更改 pack() 的引數,你可以獲取不同的控制元件佈局。
標籤的尺寸是由屬於標籤選項中的寬度和高度來定義的。

備註
標籤內包含文字或者圖片的時候,它們的寬度及高度的單位是不同的。如果是文字的話,那單位就是單個字元的寬度;如果是圖片的話,那單位就是畫素了。


工慾善其事必先利其器,我們可以先來看看標籤物件裡面都有哪些屬性 options。通過 dict(label) 我們可以列出標籤物件的所有屬性。

Tkinter Label_Get Properties.py
  1. from sys import version_info
  2. if version_info.major == 2:
  3.     import Tkinter as tk
  4. elif version_info.major == 3:
  5.     import tkinter as tk
  6.    
  7. from pprint import pprint
  8.    
  9. app = tk.Tk()
  10. labelExample = tk.Label(app, text="This is a Label", height=15, width=100)
  11. pprint(dict(labelExample))
複製代碼


得到的屬性字典如下,

{'activebackground': 'SystemButtonFace',
'activeforeground': 'SystemButtonText',
'anchor': 'center',
'background': 'SystemButtonFace',
'bd': <pixel object at 00000000048D1000>,
'bg': 'SystemButtonFace',
'bitmap': '',
'borderwidth': <pixel object at 00000000048D1000>,
'compound': 'none',
'cursor': '',
'disabledforeground': 'SystemDisabledText',
'fg': 'SystemButtonText',
'font': 'TkDefaultFont',
'foreground': 'SystemButtonText',
'height': 15,
'highlightbackground': 'SystemButtonFace',
'highlightcolor': 'SystemWindowFrame',
'highlightthickness': <pixel object at 00000000048FF100>,
'image': '',
'justify': 'center',
'padx': <pixel object at 00000000048FED40>,
'pady': <pixel object at 00000000048FF0D0>,
'relief': 'flat',
'state': 'normal',
'takefocus': '0',
'text': 'This is a Label',
'textvariable': '',
'underline': -1,
'width': 100,
'wraplength': <pixel object at 00000000048FED70>}

知道標籤的所有屬性後,那你就可以通過更改它們來得到不同的標籤外觀。

更改標籤文字字型
你可以用下面的例子來改變標籤中的文字字型。

Tkinter Label_Set Text Font.py
  1. from sys import version_info
  2. if version_info.major == 2:
  3.     import Tkinter as tk
  4. elif version_info.major == 3:
  5.     import tkinter as tk

  6. import tkFont   
  7. app = tk.Tk()
  8. labelExample1 = tk.Label(app, text="Customized Label 1", font=("Times", 20))
  9. labelExample2 = tk.Label(app, text="Customized Label 2",
  10.                          font=("Times", 20, "italic"))

  11. labelFont3 = tkFont.Font(family="Helvetica", size=20, weight=tkFont.BOLD,
  12.                          underline=1, overstrike=1)
  13. labelExample3 = tk.Label(app, text="Customized Label 3",
  14.                          font=labelFont3)
  15. labelExample1.pack()
  16. labelExample2.pack()
  17. labelExample3.pack()
  18. app.mainloop()
複製代碼



通過元組來設定標籤字型
  1. labelExample1 = tk.Label(app, text="Customized Label 1", font=("Times", 20))
  2. labelExample2 = tk.Label(app, text="Customized Label 2",
  3.                          font=("Times", 20, "italic"))
複製代碼


字型元組的第一個元素是字型名字,隨後的元素是大小,格式比如加粗,斜體,下劃線或者刪除線。

用 tkFont 字型物件來設定標籤字型
  1. labelFont3 = tkFont.Font(family="Helvetica", size=20, weight=tkFont.BOLD,
  2.                          underline=1, overstrike=1)
  3. labelExample3 = tk.Label(app, text="Customized Label 3",
  4.                          font=labelFont3)
複製代碼


設定標籤文字字型屬性的另外一種方法是使用 tkFont 模組中的字型物件。labelExample3 的字型型別就是 Helvetica 字型系列,20 號,加粗,有下劃線以及粗細為 1 的刪除線。

Info
也許你想知道 Tkinter 中可用的字型系列,那下面的這段小程式就會把它們全部列出來。

Tkinter Label_Get Fonts List.py
  1. from sys import version_info
  2. if version_info.major == 2:
  3.     import Tkinter as tk
  4. elif version_info.major == 3:
  5.     import tkinter as tk

  6. import tkFont
  7. from pprint import pprint

  8. app = tk.Tk()
  9. pprint(tkFont.families())
複製代碼

更改標籤顏色
fg 及 bg 屬性分別確定了標籤控制元件的前景色及背景色。

標籤中顯示圖片
標籤中 image 屬性可以用來在標籤中顯示圖片。

Tkinter Lael_Display Image.py
  1. from sys import version_info
  2. if version_info.major == 2:
  3.     import Tkinter as tk
  4. elif version_info.major == 3:
  5.     import tkinter as tk
  6.    

  7. app = tk.Tk()
  8. logo = tk.PhotoImage(file='python.gif')
  9. labelExample = tk.Label(app, image=logo)
  10. labelExample.pack()
  11. app.mainloop()
複製代碼


  1. <div class="panel panel-primary panel-warning">
  2. <div class="panel-heading">Warning</div>
  3. <div class="panel-body"><p><code>tk.PhotoImage</code> 只能顯示 GIF,PPM/PPM/PGM 格式的圖片。如果圖片是其他格式的話,那它會報如下錯誤,<em>_tkinter.TclError: couldn’t recognize data in image file</em></p>
複製代碼

文章出處
網頁設計,網站架設 ,網路行銷,網頁優化,SEO - NetYea 網頁設計

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-24 05:14 , Processed in 0.049273 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表