标签Label的基本应用
Label()方法可以用于在窗口内建立文字或图像标签。
Label(父对象,options,···)
Label()方法的第一个参数是父对象,表示这个标签将建立在哪一个父对象(可想成父窗口或称容器)内。下列是Label()方法内其他常用的options参数:
(1)anchor:如果空间大于所需时,控制标签的位置,默认是CENTER(居中)
(2)bg或background:背景色彩。
(3)bitmap:使用默认图标当作标签内容。
(4)borderwidth或bd:标签边界宽度,默认是1.
(5)compound:可以设置标签内含图像和文字时,彼此的位置关系。
(6)cursor:当鼠标光标在标签上方时的外形。
(7)fg或foreground:前景色彩。
(8)font:可选择字形、字形样式与大小。
(9)height:标签高度,单位是字符。
(10)image:标签以图像方式呈现。
(11)justify:存在多行文本时最后一行的对齐方式,可取值有LEFT/CENTER/RIGHT(靠左/居中/靠右),默认是居中对齐。
(12)padx/pady:标签文字与标签区间的间距,单位是像素。
(13)relief:默认是relief=FLAT,可由此控制标签的外框。
(14)text:标签内容,如果有"\n"则可输入多行文字。
(15)textvariable:可以设置标签以变量方式显示。
(16)underline:可以设置第几个文字有下划线,从0开始算起,默认是-1,表示无下划线。
(17)width:标签宽度,单位是字符。
(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()
执行后,很明显窗口高度会比没有控件时更小,因为tkinter只会安排足够的空间显示空间。上述代码的pack()方法主要是包装
窗口的Widget控件和定位窗口的对象,所以可以在执行结果的窗口内见到上述Widget控件。
d对于以后的程序设计,笔者建议将对象声明与pack方法分开,或是如果不会使用此对象做更一步操作时才使用
label=Label(root,text="I like tkinter").pack()这种声明与pack一起的方式,如此不容易出现错误。
Widget共同属性Color
fg或foreground:可以设置前景色彩,在此相当于是标签的颜色。bg或background可以设置
背景色彩。
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可以设置Widget控件(此例是标签)的高度,单位是字符高度。width可以设置Widget控件(此例是标签)的宽度,、
单位是字符宽度。
样例:设置标签宽度是15,高度是3,背景是黄色,前景是蓝色。
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其实是指标签文字在标签区域输出位置的设置,在默认情况下Widget控件是
上下与左右都居中对齐。我们也可以使用anchor选项设定Widget控件的对齐,选项如下:
nw n ne
w center e
sw s se
样例:让字符串从标签区间左上角位置开始输出。
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的参数设置也可以使用内建大写常数,例如,nw使用NW、n使用N、ne使用NE、
w使用W、center使用CENTER、e使用E、sw使用SW、s使用S、se使用SE。当程序使用
大写常数时,可以省略字符串的双引号。
Label文字输出换行位置wraplength
wraplength这个参数可以设置标签中的文字在多少宽度后自动换行。
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参数用于设置文字字形,这个参数包含下列内容。
(1)字形family:如Helvetica、Times等。
(2)字号size:单位是像素。
(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参数:
font=("Helvetic",20,"bold")
Label的justify参数
在标签的输出中,如果是多行的输出,在最后一行输出时可以使用justify参数设置所输出的标签
内容是left/center/right(靠左/居中/靠右),默认是居中输出。
样例:使用默认方式执行多行输出,并观察最后一行是居中对齐输出。
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()
执行多行输出,并设置最后一行是靠左对齐输出
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也提供了在标签位置放置内建位图的功能。下面是在各操作系统平台都可以使用的位图。
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参数
图像与文字共存时,可以使用此参数定义文字与图像的位置关系。compound参数可以是下列
值。
left:图像在左。
right:图像在右。
top:图像在上。
bottom:图像在下。
center:文字覆盖在图像上方。
样例,图像与文字共存时,图像在左边。
from tkinter import *
root=Tk()
root.title("ch2_14")
''
label=Label(root,bitmap="hourglass",
compound="left",text="我的天空")
label.pack()
Widget的共同属性relief
这个relief属性也可以应用在许多Widget控件上,可以利用relief属性建立Widget控件的边框。
样例:
from tkinter import *
root=Tk()
root.title("ch2_17")
''
label=Label(root,text="我的天空",relief="raised")
label.pack()
root.mainloop()
标签文字与标签区间的间距padx/pady
在设计标签或其他Widget控件时,若是不设置Widget的大小,系统将使用最适空间作为
此Widget的大小,padx可以设置标签文字左右边界与标签区间的x轴间距,pady可以设置
标签文字上下边界与标签区间的y轴间距。
样例:将标签文字与标签区间的左右间距设为5,标签文字与标签区间的上下间距设为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也提供了在标签位置放置内建位图的功能。下面是在各操作系统平台都可以使用的位图。
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参数
图像与文字共存时,可以使用此参数定义文字与图像的位置关系。compound参数可以是下列
值。
left:图像在左。
right:图像在右。
top:图像在上。
bottom:图像在下。
center:文字覆盖在图像上方。
样例,图像与文字共存时,图像在左边。
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_14")
''
>>> label=Label(root,bitmap="hourglass",
compound="left",text="我的天空")
>>> label.pack()
#Widget的共同属性relief
这个relief属性也可以应用在许多Widget控件上,可以利用relief属性建立Widget控件的边框。
样例:
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_17")
''
>>> label=Label(root,text="我的天空",relief="raised")
>>> label.pack()
>>>root.mainloop()
#标签文字与标签区间的间距padx/pady
在设计标签或其他Widget控件时,若是不设置Widget的大小,系统将使用最适空间作为
此Widget的大小,padx可以设置标签文字左右边界与标签区间的x轴间距,pady可以设置
标签文字上下边界与标签区间的y轴间距。
样例:将标签文字与标签区间的左右间距设为5,标签文字与标签区间的上下间距设为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也提供了在标签位置放置内建位图的功能。下面是在各操作系统平台都可以使用的位图。
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参数
图像与文字共存时,可以使用此参数定义文字与图像的位置关系。compound参数可以是下列
值。
left:图像在左。
right:图像在右。
top:图像在上。
bottom:图像在下。
center:文字覆盖在图像上方。
样例,图像与文字共存时,图像在左边。
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_14")
''
>>> label=Label(root,bitmap="hourglass",
compound="left",text="我的天空")
>>> label.pack()
#Widget的共同属性relief
这个relief属性也可以应用在许多Widget控件上,可以利用relief属性建立Widget控件的边框。
样例:
>>> from tkinter import *
>>> root=Tk()
>>> root.title("ch2_17")
''
>>> label=Label(root,text="我的天空",relief="raised")
>>> label.pack()
>>>root.mainloop()
#标签文字与标签区间的间距padx/pady
在设计标签或其他Widget控件时,若是不设置Widget的大小,系统将使用最适空间作为
此Widget的大小,padx可以设置标签文字左右边界与标签区间的x轴间距,pady可以设置
标签文字上下边界与标签区间的y轴间距。
样例:将标签文字与标签区间的左右间距设为5,标签文字与标签区间的上下间距设为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
图片可以应用在许多地方,例如标签、功能按钮、选项按钮、文字区域等。在使用前可以用
PhotoImage()方法建立图像对象,然后再将此对象应用在其他窗口组件上。它的语法如下:
imageobj=PhotoImage(file="xxx.gif")
可以使用Label()方法内使用"image=imageobj"参数设置此图像对象。
样例:窗口显示html.gif图片的基本应用。
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()
如果想要在标签内显示jpg文件,需要借助PIL模块的Image和ImageTk模块。
请先导入pillow模块。
注意在程序设计中需导入的是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参数和image参数不能共存,如果发生了这种状况,bitmap参数将不起作用。、
Widget的共同方法config()
Widget控件在建立时可以直接设置对象属性,若是部分属性未建立,未来在程序执行时如果想要
建立或是更改属性可以使用config()方法。此方法内属性设置的参数用法与建立时相同。
样例:计数器的设计,这个程序会每秒更新一次计数器内容。
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
图片可以应用在许多地方,例如标签、功能按钮、选项按钮、文字区域等。在使用前可以用
PhotoImage()方法建立图像对象,然后再将此对象应用在其他窗口组件上。它的语法如下:
imageobj=PhotoImage(file="xxx.gif")
可以使用Label()方法内使用"image=imageobj"参数设置此图像对象。
样例:窗口显示html.gif图片的基本应用。
>>> 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()
如果想要在标签内显示jpg文件,需要借助PIL模块的Image和ImageTk模块。
请先导入pillow模块。
注意在程序设计中需导入的是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参数和image参数不能共存,如果发生了这种状况,bitmap参数将不起作用。、
#Widget的共同方法config()
Widget控件在建立时可以直接设置对象属性,若是部分属性未建立,未来在程序执行时如果想要
建立或是更改属性可以使用config()方法。此方法内属性设置的参数用法与建立时相同。
样例:计数器的设计,这个程序会每秒更新一次计数器内容。
>>> 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
图片可以应用在许多地方,例如标签、功能按钮、选项按钮、文字区域等。在使用前可以用
PhotoImage()方法建立图像对象,然后再将此对象应用在其他窗口组件上。它的语法如下:
imageobj=PhotoImage(file="xxx.gif")
可以使用Label()方法内使用"image=imageobj"参数设置此图像对象。
样例:窗口显示html.gif图片的基本应用。
>>> 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()
如果想要在标签内显示jpg文件,需要借助PIL模块的Image和ImageTk模块。
请先导入pillow模块。
注意在程序设计中需导入的是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参数和image参数不能共存,如果发生了这种状况,bitmap参数将不起作用。、
#Widget的共同方法config()
Widget控件在建立时可以直接设置对象属性,若是部分属性未建立,未来在程序执行时如果想要
建立或是更改属性可以使用config()方法。此方法内属性设置的参数用法与建立时相同。
样例:计数器的设计,这个程序会每秒更新一次计数器内容。
>>> 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方法是内嵌方法的设计,after()方法,第一个参数1000表示每隔1秒会调用第二个参数注名的
方法,此例中是counting()方法。
Widget的共同属性Cursors
Cursor表示光标形状,程序设计时如果想要更改光标形状。例如,可以设计鼠标光标在
标签(Label)或按钮(Button)上时的形状,可以使用本功能。不过读者需留意,光标形状
可能会因为操作系统不同而有所差异。光标形状太多,名称不列出来了。
在一些Widget控件的参数中右cursor,可以由此设置光标在此控件上时的形状,如果省略,
系统将沿用光标在父容器上的形状。
当鼠标光标经过raised标签时,其形状将变为"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参数和image参数不能共存,如果发生了这种状况,bitmap参数将不起作用。、
#Widget的共同方法config()
Widget控件在建立时可以直接设置对象属性,若是部分属性未建立,未来在程序执行时如果想要
建立或是更改属性可以使用config()方法。此方法内属性设置的参数用法与建立时相同。
样例:计数器的设计,这个程序会每秒更新一次计数器内容。
>>> 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方法是内嵌方法的设计,after()方法,第一个参数1000表示每隔1秒会调用第二个参数注名的
方法,此例中是counting()方法。
#Widget的共同属性Cursors
Cursor表示光标形状,程序设计时如果想要更改光标形状。例如,可以设计鼠标光标在
标签(Label)或按钮(Button)上时的形状,可以使用本功能。不过读者需留意,光标形状
可能会因为操作系统不同而有所差异。光标形状太多,名称不列出来了。
在一些Widget控件的参数中右cursor,可以由此设置光标在此控件上时的形状,如果省略,
系统将沿用光标在父容器上的形状。
当鼠标光标经过raised标签时,其形状将变为"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有一个共同方法keys()可以用列表(list)传回这个Widget所有的参数。
样例:传回标签Label()方法的所有参数。
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()
分隔线Separator
在设计GUI程序时,有时适度地在适当位置增加分割线可以让整体视觉效果更佳。tkinter.ttk中有Separator模块,可以用此
模块完成此工作,它的语法格式如下。
Separator(父对象,options)
Separator()方法的第一个参数是父对象,表示这个分隔线将建立在哪一个父对象内;
options参数如果是HORIZONTAL则建立水平分隔线,VERTICAL则建立垂直分隔线
from tkinter import *
from tkinter.ttk import Separator
root=Tk()
root.title("ch2_26")
''
myTitle="一个人的极境旅行"
myContent="""2016年12月,我一个人订了机票和船票,
开始我的南极旅行,飞机经迪拜再往阿根廷的乌斯怀亚,
在此我登上邮轮开始我的南极之旅"""
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),表示此分割线填满X轴,它与窗口边界左右均相距5像素。
网友评论