如何在 Tkinter 中高亮文本小部件的当前行?
我们可以使用 Tkinter text 小部件来接受多行用户输入。我们可以插入文本、显示信息以及从文本小部件中获取输出。
为了高亮文本小部件中当前选中的文本,我们可以使用 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()输出
运行上述代码将显示一个带有文本小部件的窗口,其中包含高亮显示的文本。

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP