发布时间:2018-05-29 21:25:04编辑:Run阅读(6673)
使用 width 和 heigth 来指定控件的大小,如果指定的大小无法满足文本的要求,
会出现:超出 Label 的那部分文本被截断了
常用的方法是:使用自动换行功能,及当文本长度大于控件的宽度时,文本应该换到下一行显示,Tk 不会自动处理,但提供了属性:
wraplength: 指定多少单位后开始换行
justify: 指定多行的对齐方式
ahchor: 指定文本(text)或图像(bitmap/image)在 Label 中的显示位置
代码示例:
import tkinter as tk root = tk.Tk() # 左对齐,文本居中 tk.Label(root, text='welcome to www.py3study.com', bg='yellow', width=40, height=3, wraplength=80, justify='left').pack() # 居中对齐,文本居左 tk.Label(root, text='welcome to www.py3study.com', bg='red', width=40, height=3, wraplength=80, anchor='w').pack() #居中对齐,文本居右 tk.Label(root, text='welcome to www.py3study.com', bg='blue', width=40, height=3, wraplength=80, anchor='e').pack() root.mainloop()
效果:

PS:
justify 与 anchor 的区别了:一个用于控制多行的对齐;另一个用于控制整个文本块在 Label 中的位置
上一篇: tkinter打包成exe程序
下一篇: tkinter -- button1
51221
50646
41261
38073
32536
29441
28305
23162
23121
21459
1514°
2232°
1854°
1795°
2091°
1846°
2527°
4244°
4105°
2928°