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

วิธีตั้งค่าการจัดตำแหน่งข้อความ (Justification) ในกล่องข้อความ Tkinter


Widget Text รองรับการป้อนข้อมูลหลายบรรทัดจากผู้ใช้ เราสามารถตั้งค่าคุณสมบัติ Text widget เช่น font properties, สีข้อความ, พื้นหลัง ฯลฯ ด้วย configure()

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

ผลลัพธ์

รันโค้ด จะสังเกตว่าเคอร์เซอร์ของกล่องข้อความจะมีการจัดตำแหน่งกึ่งกลาง

วิธีตั้งค่าการจัดตำแหน่งข้อความ (Justification) ในกล่องข้อความ Tkinter