首页 > 其他 > 详细

tkinter 文字对齐,换行

时间:2021-05-08 00:22:41      阅读:63      评论:0      收藏:0      [点我收藏+]

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()

技术分享图片

 

tkinter 文字对齐,换行

原文:https://www.cnblogs.com/rainbow-tan/p/14742026.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!