Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Căn chỉnh justify cho Text box Tkinter

Widget Text hỗ trợ nhập nhiều dòng. Cấu hình font, color qua configure().

Để căn chỉnh (justify) text trong Text widget, dùng tag_add() và tag_configure() với justify='center'.

Ví dụ

from tkinter import *

win = Tk()
win.geometry("700x350")

text = Text(win, width=40, height=10)
text.tag_configure("center", justify='center')
text.insert(INSERT, "Welcome to Tutorialspoint...")
text.tag_add("center", 1.0, "end")
text.pack()
win.mainloop()

Kết quả

Text widget với text "Welcome to Tutorialspoint..." căn giữa (center).

Căn chỉnh justify cho Text box Tkinter

Giá trị justify: LEFT (mặc định), CENTER, RIGHT. Tag có thể áp dụng cho đoạn text cụ thể (range index).