Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Tkinter 텍스트 위젯에서 특정 단어의 색상을 변경하는 방법은 무엇입니까?

<시간/>

Tkinter 텍스트 위젯은 여러 줄 텍스트 입력을 만들고 표시하는 데 사용됩니다. 일반적으로 텍스트 위젯을 구성하는 데 사용되는 여러 기능과 방법을 제공합니다.

텍스트 위젯에서 특정 단어의 색상을 변경하려는 경우 tag_add(태그 이름, 범위)를 사용할 수 있습니다. 형식을 지정하려는 단어를 선택하는 메서드입니다. 단어가 선택되면 tag_config(properties)를 사용하여 단어의 색상, 배경색 및 기타 속성을 변경할 수 있습니다. 방법.

예시

이 예에서는 텍스트 위젯에서 선택한 단어의 색상을 구성합니다.

#Import required libraries
from tkinter import *

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

#Define the geometry of the window
win.geometry("600x250")

#Create a text widget
text= Text(win)
text.insert(INSERT, "Hello World!\n")
text.insert(END, "This is a New Line")

text.pack(fill=BOTH)

#Configure the text widget with certain color
text.tag_config("start", foreground="red")
text.tag_add("start", "1.6", "1.12")

win.mainloop()

출력

위의 코드를 실행하면 "World"에 특정 색상이 포함된 "Hello World" 문자열이 포함된 텍스트가 있는 창이 표시됩니다.

Tkinter 텍스트 위젯에서 특정 단어의 색상을 변경하는 방법은 무엇입니까?