如何在 Tkinter 文字框中設定對齊?


Text 控制元件支援使用者的多行輸入。我們可以透過使用 configure() 方法來配置 Text 控制元件屬性,例如其字型屬性、文字顏色、背景等。

要設定 Text 控制元件中我們文字的對齊,我們可以使用 tag_add()tag_configure() 屬性。我們將 "justify" 的值指定為 CENTER

示例

# Import the required libraries
from tkinter import *

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

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

# Create a text widget
text=Text(win, width=40, height=10)

# justify the text alignment to the center
text.tag_configure("center", justify='center')
text.insert(INSERT, "Welcome to Tutorialspoint...")

# Add the tag from start to end text
text.tag_add("center", 1.0, "end")
text.pack()

win.mainloop()

輸出

如果你執行上述程式碼,你會觀察到文字視窗的游標的對齊設定為居中。

更新於: 06-08-2021

3K+ 檢視

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.