欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python圖形用戶界面tkinter之標(biāo)簽Label的使用說明

 更新時間:2022年06月20日 17:13:19   作者:LLLLLLLLLLLLIU  
這篇文章主要介紹了python圖形用戶界面tkinter之標(biāo)簽Label的使用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

圖形用戶界面tkinter之標(biāo)簽Label使用

導(dǎo)入tkinter模塊

from tkinter import *

構(gòu)建窗口對象

root = Tk()

窗口屬性設(shè)置

#窗口標(biāo)題
root.title('窗口標(biāo)題')
#窗口大小
root.geometry('200x300')
#設(shè)定窗口背景顏色
root.configure(bg = "blue")
#更改窗口圖標(biāo)
root.iconbitmap('icon文件路徑')
#讓程序持續(xù)執(zhí)行
root.mainloop()

結(jié)果圖示

Mark:一般給窗口設(shè)置標(biāo)題、背景顏色、大小、圖標(biāo)應(yīng)該就夠用了。需要注意的是設(shè)置窗口大小的函數(shù)geometry的參數(shù)單位是像素,所呈現(xiàn)的效果就是運(yùn)行程序出現(xiàn)時的窗口大小。設(shè)置背景顏色的函數(shù)configure的參數(shù)是鍵值的形式。另外還可以限制窗口大小,比如限定窗口最大化、最小化:maxsize、minsize。運(yùn)行程序時,呈現(xiàn)的窗口最大化、最小化:state、iconify。還可以更改窗口的默認(rèn)圖標(biāo):iconbitmap。

標(biāo)簽label

標(biāo)簽里面可以放置文本和圖片。

文本標(biāo)簽

Label(root,text='Hello tkinter',
	  fg='white',bg='red',
	  height=1,width=15,anchor='nw').pack()

結(jié)果圖示

如果文本內(nèi)容比較長

比如text=‘I will white a text, in which there are many words,and the method of the condition will be given’

Label(root,
      text='I will white a text, in which there are many words,and the method of the condition will be given',
          fg='white',bg='red',
           height=8,width=15,anchor='nw',
        wraplength=100,justify='left').pack()

結(jié)果圖示

Mark:當(dāng)我們在標(biāo)簽中放置文本時,為了讓文本在適當(dāng)?shù)奈恢?,正常的顯示,需要用到label的一些屬性。比如設(shè)置label標(biāo)簽的高度、寬度、背景顏色:height、weight、bg。設(shè)置字體的顏色、大?。篺g、font。文本在label標(biāo)簽中的位置:anchor。文本中內(nèi)容的對齊方式:justify。如果文本內(nèi)容過長,可以調(diào)節(jié)height、width、wraplength。其中wraplenght表示的是多少像素單位后換行。當(dāng)標(biāo)簽中放置的是文本,height、width指的是多少字符單位。

補(bǔ)充:涉及到單位的有g(shù)eometry、height、width、wraplenght。geometry用于設(shè)置窗口大小,是像素單位。wraplength指的是一段文本多長開始換行,指的是像素單位。而height、width在標(biāo)簽label中放置文本時,指的是字符單位,用于設(shè)置label標(biāo)簽的大小,方便展示出文本內(nèi)容。

圖片標(biāo)簽

python內(nèi)置圖片( bitmap屬性)

Label(root,bitmap='error').pack()

結(jié)果圖示

error可以換為hourglass、info、questhead等等

image屬性顯示圖片

創(chuàng)建image對象

im = PhotoImage(file = r'C:\Users\Administrator\Desktop\動物.png')

創(chuàng)建label對象

Label(root,image = im).pack()

結(jié)果圖示

Mark:在標(biāo)簽label中,使用python內(nèi)置的圖片,需要使用屬性bitmap,bitmap的值可以查找相關(guān)文檔。如果想放置自己的照片,需要使用image屬性,image的值是一個image對象。用類PhotoImage將對應(yīng)的圖片轉(zhuǎn)化為image對象使用。

supplement

文本圖片的組合 屬性compound

xtext='中國風(fēng)'
im = PhotoImage(file = r'C:\Users\Administrator\Desktop\喜鵲桃花折扇.png')
Label(root,text=xtext,fg='red',font=('楷體',40),
      image = im,compound='center').pack()

結(jié)果圖示

Mark:在標(biāo)簽label中同時放入文本和圖片,要使用label的compound屬性。

使用tkinter解決的一些小問題

Label的weight參數(shù)

之前做的一個項目中也是用label顯示圖片,height參數(shù)可以使用

tk.Label(self.root, image=self.p[i] ,width = 200,height = 200 ).place(x =x0-20,y=y0+50)

但是最近做的這個卻提示沒有這個參數(shù),所以就無法更改顯示的圖片大小,找了很長時間沒有解決,最后通過別的庫將圖片改變大小,然后再顯示回來,至于最終要使用哪個圖片傳給別的函數(shù)可以自己選擇

def photo_show(p):
? ? # 待處理圖片存儲路徑
? ? im = Image.open(p)
? ? # Resize圖片大小,入口參數(shù)為一個tuple,新的圖片大小
? ? imBackground = im.resize((200, 200))
? ? # 處理后的圖片的存儲路徑,以及存儲格式
? ? imBackground.save('show.png')

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論