Tkinter Label 文本的多行显示

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Kompany4/article/details/73469025

在 Tk004 中,使用 width 和 heigth 来指定控件的大小,如果指定的大小无法满足文本的要求
是,会出现什么现象呢?如下代码:
Label(root,text = 'welcome to jcodeer.cublog.cn',width = 10,height = 3).pack()
运行程序,超出 Label 的那部分文本被截断了,常用的方法是:使用自动换行功能,及当文
本长度大于控件的宽度时,文本应该换到下一行显示,Tk 不会自动处理,但提供了属性:


wraplength: 指定多少单位后开始换行
justify:  指定多行的对齐方式
ahchor: 指定文本(text)或图像(bitmap/image)在 Label 中的显示位置
可用的值:
e/w/n/s/ne/se/sw/sn/center
布局如下图
nw  n        ne
w  center  e
sw   s       se
'''

from Tkinter import *
root = Tk()
#左对齐,文本居中
Label(root,
text = 'welcome to jcodeer.cublog.cn',
bg = 'yellow',
width = 40,
height = 3,
wraplength = 80,
justify = 'left'
).pack()

#居中对齐,文本居左
Label(root,
text = 'welcome to jcodeer.cublog.cn',
bg = 'red',
width = 40,
height = 3,
wraplength = 80,
anchor = 'w'
).pack()
#居中对齐,文本居右


Label(root,
text = 'welcome to jcodeer.cublog.cn',
bg = 'blue',
width = 40,
height = 3,
wraplength = 80,
anchor = 'e'
).pack()
root.mainloop()

'''
输出结果,justify 与 anchor 的区别了:一个用于控制多行的对齐;另一个用于控制整个文本
块在 Label 中的位置
'''



猜你喜欢

转载自blog.csdn.net/Kompany4/article/details/73469025