在 Tkinter 中向選中文字新增彩色文字
如果我們想在一個可接受多行使用者輸入的應用程式中實現文字編輯器,那麼我們可以使用Tkinter 文字小部件。Tkinter 中的文字小部件通常用於為應用程式建立文字編輯器,在應用程式中我們可以寫入文字並執行選擇、編輯和建立特定文字等操作。
如果您想強調文字併為強調的文字提供顏色,那麼您可以使用tag_add("start", "first", "second")方法。tag_add()方法接收兩個引數,用於從文字小部件中選擇指定文字。您可以透過使用tag_configure()方法配置標籤來為強調的文字賦予背景色。
示例
# Import the required library
from tkinter import *
# Create an instance of tkinter frame or window
win = Tk()
# set the size of the window
win.geometry("700x350")
# Create a new frame
frame= Frame(win)
# Add a text widget
text= Text(frame)
# insert a new text
text.insert(INSERT, "Hello, Welcome to TutorialsPoint.com")
text.pack()
# Add a tag to the specified text
text.tag_add("start", "1.8", "1.35")
text.tag_configure("start", background= "black", foreground= "yellow")
frame.pack()
win.mainloop()輸出
執行以上程式碼將顯示一個小部件,其中包含一些強調的文字。

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP