tkinter -- 文本的多行显示

发布时间:2018-05-29 21:25:04编辑:Run阅读(5527)

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

    效果:

    blob.png


    PS:

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

关键字

上一篇: tkinter打包成exe程序

下一篇: tkinter -- button1