如何在 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()输出
如果你运行上述代码,你会观察到文本窗口的光标的对齐设置为居中。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP