02-tkinter Label学习笔记

标签Label

Label()用来在窗口建立文字或者图像标签。
使用方法:Label(父对象,options,......)。

常用的options参数:

(1)anthor:控制标签位置,默认是CENTER(居中)。

(2)bg:背景颜色。

(3)bitmap:使用默认图标当做标签内容。

(4)borderwidth:标签边界宽度。

(5)compound:可以设置标签内含图片和文字时,彼此的位置关系。

(6)cursor:鼠标光标停留在标签上的外形。

(7)fg:前景颜色。

(8)font:可以设置字形、字形样式和大小。

(9)height:标签高度,单位是字符。

(10)width:标签宽度,单位是字符。

(11)image:标签以图像的方式显示。

(12)justify:当存在多行文本时最后一行的对齐方案,可取LEFT/RIGHT/CENTER(向左/向右/居中),默认是居中对齐。

(13)padx/pady:标签文字与标签之间的间距,单位是像素。

(14)relief:控制标签的外形,默认是relief=FLAT。

(15)text:标签内容,可以使用"\n"控制多行输入。

(16)textvariable:设置标签对象以变量方式显示。

(17)underline:设置第几个文字有下划线,从0开始,默认是-1,表示无下划线。

(18)wraplength:到多少宽度后换行。

06-第一个标签程序

from tkinter import *
win=Tk()
label=Label(win,text="hello tkinter")
label.pack() #pack布局。
win.mainloop()

07-为标签添加颜色

from tkinter import *
win=Tk()
win.title("添加颜色")
label=Label(win,text="hello tkinter",bg="green",fg="blue",width=15,height=3)
label.pack()
win.mainloop()

08-设置标签位置

from tkinter import *
win=Tk()
win.title("标签颜色")
label=Label(win,text="hello tkinter" ,bg="green",fg="blue",anchor="nw",height=3,width=15)
label.pack()
win.mainloop()

此时标签在左上方输出,如果想要标签在右下角输出则anchor="se",左对齐输出用"w",右对齐输出用"e",
上对齐输出用"n",下对齐输出用"s"。

09-设置label中文字达到30像素后自动换行。

from tkinter import *
win=Tk()
win.title("设置标签文字换行输出")
label=Label(win,text="life is short,you need python",bg="green",fg="blue",width=20,height=7,anchor="nw",wraplength=30)
label.pack()
win.mainloop()

wraplength=30会设置文字达到30像素后自动换行。

font属性,主要包括以下内容:

(1)字形 family:可以参考word里的所有字形。

(2)字号 size:单位是像素。

(3)weight:normal(正常)、bold(加粗)

(4)underline:True或者False

(5)overstrike:True或者False

(6)slant:italic或者roman

10-使用标签时设置宋体字形,大小是30像素,粗体显示。

from tkinter import *
win=Tk()
win.geometry('300x400')
win.title("设置标签字体")
label=Label(win,text="这是一段文字",bg="green",fg="blue",width=20,heigh=7,font="宋体 30 bold")
label.pack()
win.mainloop()

11-使用标签设置标签最后一行靠左输出。

from tkinter import *
win=Tk()
win.geometry('300x200')
win.title("设置标签文字靠左输出")
label=Label(win,text=" life is short,you need python",width=20,height=8,bg="green",fg="blue",wraplength=30,justify="left")
label.pack()
win.mainloop()

12-在标签位置显示error位图

from tkinter import *
win=Tk()
win.title("改变位图")
label=Label(win,bitmap="error")
label.pack()
win.mainloop()

bitmap除了error取值外,还有warning、hourglass、info、gray12、gray25、gray50、gray75、question、questhead等。

compound参数:当文字与图像共存时,可以使用该参数定义文字与图像的关系。
13-当图像和文字共存时,图像在左边,文字在右边。

from tkinter import *
win=Tk()
label=Label(win,text="这是测试文字",bitmap="error",compound="left")
label.pack()
win.mainloop()

除了有left,还有top(图像在上边),right(图像在右边),center(文字覆盖在图像上边),bottom(图像在下边)

14-设置一个raised属性的标签。

from tkinter import *
win=Tk()
label=Label(win,text="外边框设置",relief="raised")
label.pack()
win.mainloop()

relief除了有raised属性外,还有flat、groove、ridge、solid、sunken。

15-设置文字与标签的距离。

from tkinter import *
win=Tk()
label=Label(win,text="raised",relief="raised",bg="green",padx=5,pady=10)
label.pack()
win.mainloop()

此时设置标签文字与标签间的左右距离是5,上下距离是10。

16-设置标签显示图片。

from tkinter import *
win=Tk()
image_gif=PhotoImage(file=r"C:\Users\Administrator\Desktop\logo.gif")
label=Label(win,image=image_gif)
label.pack()
win.mainloop()

PhotoImage()方法只支持.gif类型的图片,如果想要显示.jpg类型的图片,可以使用PIL模块中的Image和ImageTk。
17-显示.jpg图片。

from tkinter import *
from PIL import Image,ImageTk
win=Tk()
image=Image.open("logo.jpg")
t=ImageTk.PhotoImage(image)
label=Label(win,image=t)
label.pack()
win.mainloop()

如果程序在未来想要添加新的属性或者更改现有的属性,可以使用config()方法。

18-设计一个计数器,每秒会更新一次计数器内容。

from tkinter import *
counter=0
def run_count(digit):
def count():
global counter
counter=counter+1
digit.config(text=str(counter))
digit.after(1000,count)
count()

win=Tk()
digit=Label(win,bg="green",fg="blue",font="宋体 20 bold")
digit.pack()
run_count(digit)
win.mainloop()

Cursors属性:当光标经过标签时会改变形状。

19-Cursors属性应用。

from tkinter import *
win=Tk()
label=Label(win,text="raised",relief="raised",bg="green",fg="black",padx=6,pady=10,cursor="clock")
label.pack()
win.mainloop()

分割线Separator:
20-设置分割线。

from tkinter import *
from tkinter.ttk import Separator
win=Tk()
label1=Label(win,text="这是标题",font="宋体 20 bold")
label1.pack()
sep=Separator(win,orient=HORIZONTAL)
sep.pack(fill=X,padx=5)
label2=Label(win,text="这是内容",font="宋体 20 bold")
label2.pack()
win.mainloop()

fill=X表示此分割线填满X轴,padx=5表示分割线与窗口左右均距离5像素,orient=HORIZONTAL表示是水平分割线,当orient=VERTICAL时则设置的是垂直分割线。

猜你喜欢

转载自blog.51cto.com/13526792/2447253