標(biāo)簽Label的基本應(yīng)用
Label()方法可以用于在窗口內(nèi)建立文字或圖像標(biāo)簽。
Label(父對(duì)象,options,···)
Label()方法的第一個(gè)參數(shù)是父對(duì)象,表示這個(gè)標(biāo)簽將建立在哪一個(gè)父對(duì)象(可想成父窗口或稱(chēng)容器)內(nèi)。下列是Label()方法內(nèi)其他常用的options參數(shù):
(1)anchor:如果空間大于所需時(shí),控制標(biāo)簽的位置,默認(rèn)是CENTER(居中)
(2)bg或background:背景色彩。
(3)bitmap:使用默認(rèn)圖標(biāo)當(dāng)作標(biāo)簽內(nèi)容。
(4)borderwidth或bd:標(biāo)簽邊界寬度,默認(rèn)是1.
(5)compound:可以設(shè)置標(biāo)簽內(nèi)含圖像和文字時(shí),彼此的位置關(guān)系。
(6)cursor:當(dāng)鼠標(biāo)光標(biāo)在標(biāo)簽上方時(shí)的外形。
(7)fg或foreground:前景色彩。
(8)font:可選擇字形、字形樣式與大小。
(9)height:標(biāo)簽高度,單位是字符。
(10)image:標(biāo)簽以圖像方式呈現(xiàn)。
(11)justify:存在多行文本時(shí)最后一行的對(duì)齊方式,可取值有LEFT/CENTER/RIGHT(靠左/居中/靠右),默認(rèn)是居中對(duì)齊。
(12)padx/pady:標(biāo)簽文字與標(biāo)簽區(qū)間的間距,單位是像素。
(13)relief:默認(rèn)是relief=FLAT,可由此控制標(biāo)簽的外框。
(14)text:標(biāo)簽內(nèi)容,如果有"\n"則可輸入多行文字。
(15)textvariable:可以設(shè)置標(biāo)簽以變量方式顯示。
(16)underline:可以設(shè)置第幾個(gè)文字有下劃線(xiàn),從0開(kāi)始算起,默認(rèn)是-1,表示無(wú)下劃線(xiàn)。
(17)width:標(biāo)簽寬度,單位是字符。
(18)wraplength:文本到多少寬度后換行,單位是像素。
樣例:
from tkinter import *
root=Tk()
root.title("ch2_1")
label=Label(root,text="I like tkinter")
label.pack()
print(type(label))
root.mainloop()
執(zhí)行后,很明顯窗口高度會(huì)比沒(méi)有控件時(shí)更小,因?yàn)閠kinter只會(huì)安排足夠的空間顯示空間。上述代碼的pack()方法主要是包裝
窗口的Widget控件和定位窗口的對(duì)象,所以可以在執(zhí)行結(jié)果的窗口內(nèi)見(jiàn)到上述Widget控件。
d對(duì)于以后的程序設(shè)計(jì),筆者建議將對(duì)象聲明與pack方法分開(kāi),或是如果不會(huì)使用此對(duì)象做更一步操作時(shí)才使用
label=Label(root,text="I like tkinter").pack()這種聲明與pack一起的方式,如此不容易出現(xiàn)錯(cuò)誤。
Widget共同屬性Color
fg或foreground:可以設(shè)置前景色彩,在此相當(dāng)于是標(biāo)簽的顏色。bg或background可以設(shè)置
背景色彩。
from tkinter import *
root=Tk()
root.title("ch2_3")
label=Label(root,text="I like tkinter",)
label=Label(root,text="I like tkinter",
fg="blue",bg="yellow")
label.pack()
root.mainloop()
Widget的共同屬性Dimensions
height可以設(shè)置Widget控件(此例是標(biāo)簽)的高度,單位是字符高度。width可以設(shè)置Widget控件(此例是標(biāo)簽)的寬度,、
單位是字符寬度。
樣例:設(shè)置標(biāo)簽寬度是15,高度是3,背景是黃色,前景是藍(lán)色。
from tkinter import *
root=Tk()
root.title("ch2_4")
label=Label(root,text="I like tkinter",
fg="blue",bg="yellow",
height=3,width=15)
label.pack()
root.mainloop()
Widget的共同屬性Anchor
Anchor其實(shí)是指標(biāo)簽文字在標(biāo)簽區(qū)域輸出位置的設(shè)置,在默認(rèn)情況下Widget控件是
上下與左右都居中對(duì)齊。我們也可以使用anchor選項(xiàng)設(shè)定Widget控件的對(duì)齊,選項(xiàng)如下:
nw n ne
w center e
sw s se
樣例:讓字符串從標(biāo)簽區(qū)間左上角位置開(kāi)始輸出。
from tkinter import *
root=Tk()
root.title("ch2_5")
''
label=Label(root,text="I Like tkinter",
fg="blue",bg="yellow",
height=3,width=15,
anchor="nw")
label.pack()
root.mainloop()
注意:
anchor的參數(shù)設(shè)置也可以使用內(nèi)建大寫(xiě)常數(shù),例如,nw使用NW、n使用N、ne使用NE、
w使用W、center使用CENTER、e使用E、sw使用SW、s使用S、se使用SE。當(dāng)程序使用
大寫(xiě)常數(shù)時(shí),可以省略字符串的雙引號(hào)。
Label文字輸出換行位置wraplength
wraplength這個(gè)參數(shù)可以設(shè)置標(biāo)簽中的文字在多少寬度后自動(dòng)換行。
from tkinter import *
root=Tk()
root.title("ch2_5")
''
label=Label(root,text="I Like tkinter",
fg="blue",bg="yellow",
height=3,width=15,
anchor="nw",
wraplength=40)
label.pack()
Widget的共同屬性Font
font參數(shù)用于設(shè)置文字字形,這個(gè)參數(shù)包含下列內(nèi)容。
(1)字形family:如Helvetica、Times等。
(2)字號(hào)size:?jiǎn)挝皇窍袼亍?br>
(3)weight:例如bold、normal。
(4)slant:例如italic、roman,如果不是italic則是roman。
(5)underline:例如True、False。
(6)overstrlike:例如True、False。
樣例:使用Helvetic字形,大小是20,粗體顯示。
from tkinter import *
root=Tk()
root.title("ch2_8")
''
label=Label(root,text="I Like tkinter",
fg="blue",bg="yellow",
height=3,width=15,
font="Helvetic 20 bold")
label.pack()
root.mainloop()
另外,也可以使用元組方式處理font參數(shù):
font=("Helvetic",20,"bold")
Label的justify參數(shù)
在標(biāo)簽的輸出中,如果是多行的輸出,在最后一行輸出時(shí)可以使用justify參數(shù)設(shè)置所輸出的標(biāo)簽
內(nèi)容是left/center/right(靠左/居中/靠右),默認(rèn)是居中輸出。
樣例:使用默認(rèn)方式執(zhí)行多行輸出,并觀(guān)察最后一行是居中對(duì)齊輸出。
label.pack()
from tkinter import *
root=Tk()
root.title("ch2_9")
''
label=Label(root,text="abcdefghijklmnopqrstuvwy",
fg="blue",bg="lightyellow",
wraplength=80)
label.pack()
root.mainloop()
執(zhí)行多行輸出,并設(shè)置最后一行是靠左對(duì)齊輸出
label.pack()
from tkinter import *
root=Tk()
root.title("ch2_9")
''
label=Label(root,text="abcdefghijklmnopqrstuvwy",
fg="blue",bg="lightyellow",
wraplength=80,
justify="left")
label.pack()
root.mainloop()
Widget的共同屬性Bitmaps
tkinter也提供了在標(biāo)簽位置放置內(nèi)建位圖的功能。下面是在各操作系統(tǒng)平臺(tái)都可以使用的位圖。
error hourglass info questhead question
warning gray12 gray25 gray50 gray75
from tkinter import *
root=Tk()
root.title("ch2_13")
''
label=Label(root,bitmap="hourglass")
label.pack()
root.mainloop()
compound參數(shù)
圖像與文字共存時(shí),可以使用此參數(shù)定義文字與圖像的位置關(guān)系。compound參數(shù)可以是下列
值。
left:圖像在左。
right:圖像在右。
top:圖像在上。
bottom:圖像在下。
center:文字覆蓋在圖像上方。
樣例,圖像與文字共存時(shí),圖像在左邊。
from tkinter import *
root=Tk()
root.title("ch2_14")
''
label=Label(root,bitmap="hourglass",
compound="left",text="我的天空")
label.pack()
Widget的共同屬性relief
這個(gè)relief屬性也可以應(yīng)用在許多Widget控件上,可以利用relief屬性建立Widget控件的邊框。
樣例:
from tkinter import *
root=Tk()
root.title("ch2_17")
''
label=Label(root,text="我的天空",relief="raised")
label.pack()
root.mainloop()
標(biāo)簽文字與標(biāo)簽區(qū)間的間距padx/pady
在設(shè)計(jì)標(biāo)簽或其他Widget控件時(shí),若是不設(shè)置Widget的大小,系統(tǒng)將使用最適空間作為
此Widget的大小,padx可以設(shè)置標(biāo)簽文字左右邊界與標(biāo)簽區(qū)間的x軸間距,pady可以設(shè)置
標(biāo)簽文字上下邊界與標(biāo)簽區(qū)間的y軸間距。
樣例:將標(biāo)簽文字與標(biāo)簽區(qū)間的左右間距設(shè)為5,標(biāo)簽文字與標(biāo)簽區(qū)間的上下間距設(shè)為10。
from tkinter import *
root=Tk()
root.title("ch2_18")
''
label=Label(root,text="raised",relief="raised",
root.title("ch2_9")
''
>>> label=Label(root,text="abcdefghijklmnopqrstuvwy",
fg="blue",bg="lightyellow",
wraplength=80,
justify="left")
>>> label.pack()
>>>root.mainloop()
#Widget的共同屬性Bitmaps
tkinter也提供了在標(biāo)簽位置放置內(nèi)建位圖的功能。下面是在各操作系統(tǒng)平臺(tái)都可以使用的位圖。
error hourglass info questhead question
warning gray12 gray25 gray50 gray75
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_13")
''
>>> label=Label(root,bitmap="hourglass")
>>> label.pack()
>>>root.mainloop()
#compound參數(shù)
圖像與文字共存時(shí),可以使用此參數(shù)定義文字與圖像的位置關(guān)系。compound參數(shù)可以是下列
值。
left:圖像在左。
right:圖像在右。
top:圖像在上。
bottom:圖像在下。
center:文字覆蓋在圖像上方。
樣例,圖像與文字共存時(shí),圖像在左邊。
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_14")
''
>>> label=Label(root,bitmap="hourglass",
compound="left",text="我的天空")
>>> label.pack()
#Widget的共同屬性relief
這個(gè)relief屬性也可以應(yīng)用在許多Widget控件上,可以利用relief屬性建立Widget控件的邊框。
樣例:
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_17")
''
>>> label=Label(root,text="我的天空",relief="raised")
>>> label.pack()
>>>root.mainloop()
#標(biāo)簽文字與標(biāo)簽區(qū)間的間距padx/pady
在設(shè)計(jì)標(biāo)簽或其他Widget控件時(shí),若是不設(shè)置Widget的大小,系統(tǒng)將使用最適空間作為
此Widget的大小,padx可以設(shè)置標(biāo)簽文字左右邊界與標(biāo)簽區(qū)間的x軸間距,pady可以設(shè)置
標(biāo)簽文字上下邊界與標(biāo)簽區(qū)間的y軸間距。
樣例:將標(biāo)簽文字與標(biāo)簽區(qū)間的左右間距設(shè)為5,標(biāo)簽文字與標(biāo)簽區(qū)間的上下間距設(shè)為10。
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_18")
''
>>> label=Label(root,text="raised",relief="raised",
bg="lightyellow",label=Label(root,text="abcdefghijklmnopqrstuvwy",
fg="blue",bg="lightyellow",
wraplength=80,
justify="left")
>>> label.pack()
>>>root.mainloop()
#Widget的共同屬性Bitmaps
tkinter也提供了在標(biāo)簽位置放置內(nèi)建位圖的功能。下面是在各操作系統(tǒng)平臺(tái)都可以使用的位圖。
error hourglass info questhead question
warning gray12 gray25 gray50 gray75
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_13")
''
>>> label=Label(root,bitmap="hourglass")
>>> label.pack()
>>>root.mainloop()
#compound參數(shù)
圖像與文字共存時(shí),可以使用此參數(shù)定義文字與圖像的位置關(guān)系。compound參數(shù)可以是下列
值。
left:圖像在左。
right:圖像在右。
top:圖像在上。
bottom:圖像在下。
center:文字覆蓋在圖像上方。
樣例,圖像與文字共存時(shí),圖像在左邊。
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_14")
''
>>> label=Label(root,bitmap="hourglass",
compound="left",text="我的天空")
>>> label.pack()
#Widget的共同屬性relief
這個(gè)relief屬性也可以應(yīng)用在許多Widget控件上,可以利用relief屬性建立Widget控件的邊框。
樣例:
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_17")
''
>>> label=Label(root,text="我的天空",relief="raised")
>>> label.pack()
>>>root.mainloop()
#標(biāo)簽文字與標(biāo)簽區(qū)間的間距padx/pady
在設(shè)計(jì)標(biāo)簽或其他Widget控件時(shí),若是不設(shè)置Widget的大小,系統(tǒng)將使用最適空間作為
此Widget的大小,padx可以設(shè)置標(biāo)簽文字左右邊界與標(biāo)簽區(qū)間的x軸間距,pady可以設(shè)置
標(biāo)簽文字上下邊界與標(biāo)簽區(qū)間的y軸間距。
樣例:將標(biāo)簽文字與標(biāo)簽區(qū)間的左右間距設(shè)為5,標(biāo)簽文字與標(biāo)簽區(qū)間的上下間距設(shè)為10。
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_18")
''
>>> label=Label(root,text="raised",relief="raised",
bg="lightyellow",
padx=5,pady=10)
label.pack()
root.mainloop()
圖像PhotoImage
圖片可以應(yīng)用在許多地方,例如標(biāo)簽、功能按鈕、選項(xiàng)按鈕、文字區(qū)域等。在使用前可以用
PhotoImage()方法建立圖像對(duì)象,然后再將此對(duì)象應(yīng)用在其他窗口組件上。它的語(yǔ)法如下:
imageobj=PhotoImage(file="xxx.gif")
可以使用Label()方法內(nèi)使用"image=imageobj"參數(shù)設(shè)置此圖像對(duì)象。
樣例:窗口顯示html.gif圖片的基本應(yīng)用。
from tkinter import *
root=Tk()
root.title("ch2_19")
''
html_gif=PhotoImage(file="html.gif")
label=Label(root,image=html_gif)
label.pack()
root.mainloop()
如果想要在標(biāo)簽內(nèi)顯示jpg文件,需要借助PIL模塊的Image和ImageTk模塊。
請(qǐng)先導(dǎo)入pillow模塊。
注意在程序設(shè)計(jì)中需導(dǎo)入的是PIL模塊,主要原因是要向舊版Python Image Library兼容。
from tkinter import *
from PIL import Image,ImageTk
root=Tk()
root.title("ch2_19_1")
''
root.geometry("600x400")
image=PhotoImage(file="yellowstone.jpg")
yellowstone=ImageTk.PhotoImage(image)
label=Label(root,image=yellowstone)
label.pack()
root.mainloop()
最后要提醒的是bitmap參數(shù)和image參數(shù)不能共存,如果發(fā)生了這種狀況,bitmap參數(shù)將不起作用。、
Widget的共同方法config()
Widget控件在建立時(shí)可以直接設(shè)置對(duì)象屬性,若是部分屬性未建立,未來(lái)在程序執(zhí)行時(shí)如果想要
建立或是更改屬性可以使用config()方法。此方法內(nèi)屬性設(shè)置的參數(shù)用法與建立時(shí)相同。
樣例:計(jì)數(shù)器的設(shè)計(jì),這個(gè)程序會(huì)每秒更新一次計(jì)數(shù)器內(nèi)容。
from tkinter import *
counter=0
def run_counter(digit):
def counting():
global counter
counter+=1
digit.config(text=str(counter))
digit.after(1000,counting)label=Label(root,text="raised",relief="raised",
bg="lightyellow",
padx=5,pady=10)
>>> label.pack()
>>> root.mainloop()
#圖像PhotoImage
圖片可以應(yīng)用在許多地方,例如標(biāo)簽、功能按鈕、選項(xiàng)按鈕、文字區(qū)域等。在使用前可以用
PhotoImage()方法建立圖像對(duì)象,然后再將此對(duì)象應(yīng)用在其他窗口組件上。它的語(yǔ)法如下:
imageobj=PhotoImage(file="xxx.gif")
可以使用Label()方法內(nèi)使用"image=imageobj"參數(shù)設(shè)置此圖像對(duì)象。
樣例:窗口顯示html.gif圖片的基本應(yīng)用。
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_19")
''
>>>html_gif=PhotoImage(file="html.gif")
>>> label=Label(root,image=html_gif)
>>> label.pack()
>>> root.mainloop()
如果想要在標(biāo)簽內(nèi)顯示jpg文件,需要借助PIL模塊的Image和ImageTk模塊。
請(qǐng)先導(dǎo)入pillow模塊。
注意在程序設(shè)計(jì)中需導(dǎo)入的是PIL模塊,主要原因是要向舊版Python Image Library兼容。
>>> from tkinter import *
>>>from PIL import Image,ImageTk
>>> root=Tk()
>>> root.title("ch2_19_1")
''
>>>root.geometry("600x400")
>>>image=PhotoImage(file="yellowstone.jpg")
>>>yellowstone=ImageTk.PhotoImage(image)
>>> label=Label(root,image=yellowstone)
>>> label.pack()
>>> root.mainloop()
最后要提醒的是bitmap參數(shù)和image參數(shù)不能共存,如果發(fā)生了這種狀況,bitmap參數(shù)將不起作用。、
#Widget的共同方法config()
Widget控件在建立時(shí)可以直接設(shè)置對(duì)象屬性,若是部分屬性未建立,未來(lái)在程序執(zhí)行時(shí)如果想要
建立或是更改屬性可以使用config()方法。此方法內(nèi)屬性設(shè)置的參數(shù)用法與建立時(shí)相同。
樣例:計(jì)數(shù)器的設(shè)計(jì),這個(gè)程序會(huì)每秒更新一次計(jì)數(shù)器內(nèi)容。
>>> from tkinter import *
>>> counter=0
>>> def run_counter(digit):
def counting():
global counter
counter+=1
digit.config(text=str(counter))
digit.after(1000,counting)
counting()
root=Tk()
root.title("ch2_23")
''
digit=Label(root,bg="yellow",fg="blue",
label.pack()
>>> root.mainloop()
#圖像PhotoImage
圖片可以應(yīng)用在許多地方,例如標(biāo)簽、功能按鈕、選項(xiàng)按鈕、文字區(qū)域等。在使用前可以用
PhotoImage()方法建立圖像對(duì)象,然后再將此對(duì)象應(yīng)用在其他窗口組件上。它的語(yǔ)法如下:
imageobj=PhotoImage(file="xxx.gif")
可以使用Label()方法內(nèi)使用"image=imageobj"參數(shù)設(shè)置此圖像對(duì)象。
樣例:窗口顯示html.gif圖片的基本應(yīng)用。
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_19")
''
>>>html_gif=PhotoImage(file="html.gif")
>>> label=Label(root,image=html_gif)
>>> label.pack()
>>> root.mainloop()
如果想要在標(biāo)簽內(nèi)顯示jpg文件,需要借助PIL模塊的Image和ImageTk模塊。
請(qǐng)先導(dǎo)入pillow模塊。
注意在程序設(shè)計(jì)中需導(dǎo)入的是PIL模塊,主要原因是要向舊版Python Image Library兼容。
>>> from tkinter import *
>>>from PIL import Image,ImageTk
>>> root=Tk()
>>> root.title("ch2_19_1")
''
>>>root.geometry("600x400")
>>>image=PhotoImage(file="yellowstone.jpg")
>>>yellowstone=ImageTk.PhotoImage(image)
>>> label=Label(root,image=yellowstone)
>>> label.pack()
>>> root.mainloop()
最后要提醒的是bitmap參數(shù)和image參數(shù)不能共存,如果發(fā)生了這種狀況,bitmap參數(shù)將不起作用。、
#Widget的共同方法config()
Widget控件在建立時(shí)可以直接設(shè)置對(duì)象屬性,若是部分屬性未建立,未來(lái)在程序執(zhí)行時(shí)如果想要
建立或是更改屬性可以使用config()方法。此方法內(nèi)屬性設(shè)置的參數(shù)用法與建立時(shí)相同。
樣例:計(jì)數(shù)器的設(shè)計(jì),這個(gè)程序會(huì)每秒更新一次計(jì)數(shù)器內(nèi)容。
>>> from tkinter import *
>>> counter=0
>>> def run_counter(digit):
def counting():
global counter
counter+=1
digit.config(text=str(counter))
digit.after(1000,counting)
counting()
>>> root=Tk()
>>> root.title("ch2_23")
''
>>> digit=Label(root,bg="yellow",fg="blue",
height=3,width=10,
font="Helvetic 20 bold")digit.pack()
run_counter(digit)
root.mainloop()、
run_counter方法是內(nèi)嵌方法的設(shè)計(jì),after()方法,第一個(gè)參數(shù)1000表示每隔1秒會(huì)調(diào)用第二個(gè)參數(shù)注名的
方法,此例中是counting()方法。
Widget的共同屬性Cursors
Cursor表示光標(biāo)形狀,程序設(shè)計(jì)時(shí)如果想要更改光標(biāo)形狀。例如,可以設(shè)計(jì)鼠標(biāo)光標(biāo)在
標(biāo)簽(Label)或按鈕(Button)上時(shí)的形狀,可以使用本功能。不過(guò)讀者需留意,光標(biāo)形狀
可能會(huì)因?yàn)椴僮飨到y(tǒng)不同而有所差異。光標(biāo)形狀太多,名稱(chēng)不列出來(lái)了。
在一些Widget控件的參數(shù)中右cursor,可以由此設(shè)置光標(biāo)在此控件上時(shí)的形狀,如果省略,
系統(tǒng)將沿用光標(biāo)在父容器上的形狀。
當(dāng)鼠標(biāo)光標(biāo)經(jīng)過(guò)raised標(biāo)簽時(shí),其形狀將變?yōu)?heart"。
from tkinter import *
root=Tk()
label=Label(root,text="raised",relief="raised",
bg="lightyellow",
padx=5,pady=10,
root=Tk()
>>> root.title("ch2_19_1")
''
>>>root.geometry("600x400")
>>>image=PhotoImage(file="yellowstone.jpg")
>>>yellowstone=ImageTk.PhotoImage(image)
>>> label=Label(root,image=yellowstone)
>>> label.pack()
>>> root.mainloop()
最后要提醒的是bitmap參數(shù)和image參數(shù)不能共存,如果發(fā)生了這種狀況,bitmap參數(shù)將不起作用。、
#Widget的共同方法config()
Widget控件在建立時(shí)可以直接設(shè)置對(duì)象屬性,若是部分屬性未建立,未來(lái)在程序執(zhí)行時(shí)如果想要
建立或是更改屬性可以使用config()方法。此方法內(nèi)屬性設(shè)置的參數(shù)用法與建立時(shí)相同。
樣例:計(jì)數(shù)器的設(shè)計(jì),這個(gè)程序會(huì)每秒更新一次計(jì)數(shù)器內(nèi)容。
>>> from tkinter import *
>>> counter=0
>>> def run_counter(digit):
def counting():
global counter
counter+=1
digit.config(text=str(counter))
digit.after(1000,counting)
counting()
>>> root=Tk()
>>> root.title("ch2_23")
''
>>> digit=Label(root,bg="yellow",fg="blue",
height=3,width=10,
font="Helvetic 20 bold")
>>>
>>> digit.pack()
>>> run_counter(digit)
>>> root.mainloop()、
run_counter方法是內(nèi)嵌方法的設(shè)計(jì),after()方法,第一個(gè)參數(shù)1000表示每隔1秒會(huì)調(diào)用第二個(gè)參數(shù)注名的
方法,此例中是counting()方法。
#Widget的共同屬性Cursors
Cursor表示光標(biāo)形狀,程序設(shè)計(jì)時(shí)如果想要更改光標(biāo)形狀。例如,可以設(shè)計(jì)鼠標(biāo)光標(biāo)在
標(biāo)簽(Label)或按鈕(Button)上時(shí)的形狀,可以使用本功能。不過(guò)讀者需留意,光標(biāo)形狀
可能會(huì)因?yàn)椴僮飨到y(tǒng)不同而有所差異。光標(biāo)形狀太多,名稱(chēng)不列出來(lái)了。
在一些Widget控件的參數(shù)中右cursor,可以由此設(shè)置光標(biāo)在此控件上時(shí)的形狀,如果省略,
系統(tǒng)將沿用光標(biāo)在父容器上的形狀。
當(dāng)鼠標(biāo)光標(biāo)經(jīng)過(guò)raised標(biāo)簽時(shí),其形狀將變?yōu)?heart"。
>>> from tkinter import *
>>> root=Tk()
>>> label=Label(root,text="raised",relief="raised",
bg="lightyellow",
padx=5,pady=10,
cursor="heart")label.pack()
root.mainloop()
Widget的共同方法keys()
Widget有一個(gè)共同方法keys()可以用列表(list)傳回這個(gè)Widget所有的參數(shù)。
樣例:傳回標(biāo)簽Label()方法的所有參數(shù)。
from tkinter import *
root=Tk()
root.title("ch2_25")
''
label=Label(root,text="I like tkinter")
label.pack()
print(label.keys())
['activebackground', 'activeforeground', 'anchor', 'background', 'bd', 'bg', 'bitmap', 'borderwidth', 'compound', 'cursor', 'disabledforeground', 'fg', 'font', 'foreground', 'height', 'highlightbackground', 'highlightcolor', 'highlightthickness', 'image', 'justify', 'padx', 'pady', 'relief', 'state', 'takefocus', 'text', 'textvariable', 'underline', 'width', 'wraplength']
root.mainloop()
分隔線(xiàn)Separator
在設(shè)計(jì)GUI程序時(shí),有時(shí)適度地在適當(dāng)位置增加分割線(xiàn)可以讓整體視覺(jué)效果更佳。tkinter.ttk中有Separator模塊,可以用此
模塊完成此工作,它的語(yǔ)法格式如下。
Separator(父對(duì)象,options)
Separator()方法的第一個(gè)參數(shù)是父對(duì)象,表示這個(gè)分隔線(xiàn)將建立在哪一個(gè)父對(duì)象內(nèi);
options參數(shù)如果是HORIZONTAL則建立水平分隔線(xiàn),VERTICAL則建立垂直分隔線(xiàn)
from tkinter import *
from tkinter.ttk import Separator
root=Tk()
root.title("ch2_26")
''
myTitle="一個(gè)人的極境旅行"
myContent="""2016年12月,我一個(gè)人訂了機(jī)票和船票,
開(kāi)始我的南極旅行,飛機(jī)經(jīng)迪拜再往阿根廷的烏斯懷亞,
在此我登上郵輪開(kāi)始我的南極之旅"""
lab1=Label(root,text=myTitle,
font="Helvetic 20 bold")
lab1.pack(padx=10,pady=10)
sep=Separator(root,orient=HORIZONTAL)
sep.pack(fill=X,padx=5)
lab2=Label(root,text=myContent)
lab2.pack(padx=10,pady=10)
root.mainloop()
pack(fill=X,padx=5),表示此分割線(xiàn)填滿(mǎn)X軸,它與窗口邊界左右均相距5像素。