如何將 Tkinter Text 小元件中的文字居中?


Tkinter 文字小元件是一個多行文字輸入小元件。它用於在輸入欄位中插入、刪除和新增文字資料。它在小元件類中提供了許多內建函式和屬性。

要配置文字並將其對齊到 Tkinter Text 小元件的 CENTER,我們可以使用 **justify=CENTER ** 屬性。

示例

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame or window
win=Tk()

# Set the size of the window
win.geometry("700x350")

text=Text(win)

# Configure the alignment of the text
text.tag_configure("tag_name", justify='center')

# Insert a Demo Text
text.insert("1.0", "How do I center align the text " "in a Tkinter Text widget?")

# Add the tag in the given text
text.tag_add("tag_name", "1.0", "end")
text.pack()

win.mainloop()

輸出

執行以上程式碼以顯示一個視窗,其中一個示例文字置於 Text 小元件的中心。

更新於: 2021-6 月 19 日

18K+ 瀏覽量

開始您的職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.