Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีจัดตำแหน่งข้อความกึ่งกลางใน Tkinter Text Widget


Tkinter Text widget เป็น widget รับข้อมูลหลายบรรทัด ใช้สำหรับแทรก ลบ และเพิ่มข้อมูลข้อความในฟิลด์อินพุต มีฟังก์ชันและแอตทริบิวต์ในตัวมากมาย

เพื่อกำหนดค่าและจัดตำแหน่งข้อความให้อยู่กึ่งกลางของ Tkinter Text widget ให้ใช้คุณสมบัติ 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 widget

วิธีจัดตำแหน่งข้อความกึ่งกลางใน Tkinter Text Widget