python gui - 003 tkinter label 調(diào)用

label 是標(biāo)簽控件;可以顯示文本和位圖

Lable 標(biāo)簽

Label=Tkinter.Lable(master,text=’helloworld!’)

屬性:

master

說(shuō)明: 指定控件的父窗口

text

說(shuō)明:要顯示的文字
Label=Tkinter.Lable(master,text=’helloworld!’)

wraplength

說(shuō)明:指定text中文本多少寬度后開(kāi)始換行
label=Tkinter.Label(root,text='abcdefghijklmnopqrstuvwxyz', wraplength=50)

justify

說(shuō)明:text中多行文本的對(duì)齊方式
label=Tkinter.Label(root,text='abcdefghikjlmnopqrstuvwxyz',wraplength=50,justify='left')
label=Tkinter.Label(root,text='abcdefghikjlmnopqrstuvwxyz',wraplength=50,justify='right')
label=Tkinter.Label(root,text='abcdefghikjlmnopqrstuvwxyz', wraplength=50, justify='center')

anchor

說(shuō)明:文本(text)或圖像(bitmap/image)在Label的位置。默認(rèn)為center
值和布局:

           nw        n         ne

           w       center      e

           sw        s          se

label=Tkinter.Label(root,text='abcdefghikjlmnopqrstu',wraplength=50,width=30,height=10, bg='blue',fg='red',anchor='nw')

bitmap

說(shuō)明: 顯示內(nèi)置位圖。如果image選項(xiàng)被指定了,則這個(gè)選項(xiàng)被忽略。下面的位圖在所有平臺(tái)上都有效:error, gray75, gray50, gray25, gray12, hourglass, info, questhead,question, 和 warning。


位圖

fg bg

說(shuō)明:設(shè)置前景色和背景色
label=Tkinter.Label(root,text='helloworld!',fg='red',bg='blue')

(1).使用顏色名稱 Red Green Blue Yellow LightBlue ......
(2).使用#RRGGBB label = Label(root,fg = 'red',bg ='#FF00FF',text = 'Hello I am Tkinter') 指定背景色為緋紅色
(3).除此之外,Tk還支持與OS相關(guān)的顏色值,如Windows支持SystemActiveBorder, SystemActiveCaption, SystemAppWorkspace, SystemBackground, .....

width height

說(shuō)明:設(shè)置寬度和高度
label=Tkinter.Label(root,text='helloworld!',fg='red',bg='blue',width=50,height=10)

compound

說(shuō)明:指定文本(text)與圖像(bitmap/image)是如何在Label上顯示,缺省為None,當(dāng)指定image/bitmap時(shí),文本(text)將被覆蓋,只顯示圖像了。

可以使用的值:
left:        圖像居左
right:        圖像居右
top:         圖像居上
bottom:      圖像居下     
enter:       文字覆蓋在圖像上

label=Tkinter.Label(root,text='error',bitmap='error', compound='left')

以下是關(guān)于label調(diào)用的實(shí)例:

  1. 一個(gè)顯示三個(gè)label的窗口, 其中width命令調(diào)整該label的寬度, height調(diào)整該label的高度.
from tkinter import *
root = Tk()
one = Label(root, text = 'helloworld', width = 30, height = 3)
one.pack()

two = Label(root, text = 'helloworld')
two['width'] = 30
two['height'] = 3
two.pack()


three = Label(root, text = 'helloworld')
three.pack()
three.configure(width =  30, height = 3)
three.pack()

以上三個(gè)實(shí)例中, 分別用三種方式調(diào)整label的長(zhǎng)度和寬度, 其結(jié)果是相同的.
第一種方法: 直接在創(chuàng)建對(duì)象時(shí), 指定label的長(zhǎng)度和寬度
第二種方法: 使用屬性wight和height指定label的長(zhǎng)度和寬度
第三種方法: 使用configure或config方法指定label的長(zhǎng)度和寬度


運(yùn)行結(jié)果
  • [ ]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容