1.文字对齐
anchor=‘center‘(需要配合width和height和relief使用才能看出效果)(默认居中center)
可选值如下
nw n ne
w center e
sw s se
# -*- encoding=utf-8 -*- import tkinter from tkinter import * if __name__ == ‘__main__‘: w = tkinter.Tk() w.geometry(‘{}x{}+{}+{}‘.format(400, 500, 100, 100)) """ anchor表示文字或图片的对齐方式,可选值如下 nw n ne w center e sw s se """ Label(text=‘title‘, width=20, relief=‘g‘, height=3).pack(pady=(0, 0)) anchor_values = [‘nw‘, ‘n‘, ‘ne‘, ‘w‘, ‘center‘, ‘e‘, ‘sw‘, ‘s‘, ‘se‘] for anchor_value in anchor_values: Label(text=‘title‘, width=20, relief=‘g‘, height=3, anchor=anchor_value).pack(pady=(10, 0)) w.mainloop()
2.文字换行
wraplength=50每行显示多少单位后换行(别指定height,让它自动适应,否则效果不好)
justify=‘left‘换行后的对齐方式(left,right,center)(默认左对齐)
# -*- encoding=utf-8 -*- import tkinter from tkinter import * if __name__ == ‘__main__‘: w = tkinter.Tk() text = ‘哈哈哈哈哈哈哈哈哈‘ w.geometry(‘{}x{}+{}+{}‘.format(400, 500, 100, 100)) Label(text=text, width=15, relief=‘g‘, height=2).pack(pady=(0, 0)) Label(text=text, width=15, wraplength=50, relief=‘g‘, justify=‘left‘).pack(pady=(0, 0)) Label(text=text, width=15, wraplength=50, relief=‘g‘, justify=‘center‘).pack(pady=(0, 0)) Label(text=text, width=15, wraplength=50, relief=‘g‘, justify=‘right‘).pack(pady=(0, 0)) w.mainloop()
3.同时使用对齐方式和换行
# -*- encoding=utf-8 -*- import tkinter from tkinter import * if __name__ == ‘__main__‘: w = tkinter.Tk() text = ‘哈哈哈哈哈哈哈哈哈‘ w.geometry(‘{}x{}+{}+{}‘.format(400, 500, 100, 100)) Label(text=text, width=15, relief=‘g‘, height=2).pack(pady=(0, 0)) Label(text=text, width=15, wraplength=50, relief=‘g‘, justify=‘left‘).pack(pady=(0, 0)) Label(text=text, width=15, wraplength=50, relief=‘g‘,anchor=‘ne‘, justify=‘center‘).pack(pady=(0, 0)) Label(text=text, width=15, wraplength=50, relief=‘g‘,anchor=‘nw‘, justify=‘right‘).pack(pady=(0, 0)) w.mainloop()
原文:https://www.cnblogs.com/rainbow-tan/p/14742026.html