ใช้ widget text ของ Tkinter รับข้อมูลป้อนเข้าหลายบรรทัด แทรกข้อความ แสดงข้อมูล และดึงข้อมูลจาก text widget
เพื่อไฮไลท์ข้อความที่เลือกอยู่ใน text widget ใช้เมธอด tag_add() ที่เพิ่มแท็กในข้อความปัจจุบันเท่านั้น
ตัวอย่าง
# Import the required library
from tkinter import *
# Create an instance of tkinter frame
win=Tk()
# Set the geometry
win.geometry("700x350")
# Add a text widget
text=Text(win, width=80, height=15, font=('Calibri 12'))
# Set default text for text widget
text.insert(INSERT, "Tkinter is a Python Library to create GUI-based applications.")
text.insert(END, "Learning Tkinter is Awesome!!")
# Select Text by adding tags
text.tag_add("start", "1.0","1.7")
text.tag_configure("start", background="OliveDrab1", foreground="black")
text.pack()
win.mainloop()
ผลลัพธ์
รันโค้ด จะแสดงหน้าต่างพร้อม text widget ที่มีข้อความไฮไลท์
