tkinter 文字對齊,換行


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM