Tkinter Text Tags
Tags are a region text, and then we can give a name to this region, this name is called a tag, and we can use this tag name to represent this region text. With tags, we can do further work for this tag, for example, apply glyphs, colors, etc. to this tag. The following are commonly used tagging methods.
(1) tag_add(tagname,startindex[,endindex] …): name the text between startindex and endindex as tagname tag.
(2) tag_config(tagname,options, …): you can perform specific editing for the tag, or action binding.
background
: background color.borderwidth
: thickness of the text periphery, default is 0.font
: font shape.foreground
: foreground color.justify
: the alignment, the default is LEFT, can also be RIGHT or CENTER.overstrike
: if True, add strikethrough.underline
: if True, add the underline.wrap
: When using wrap mode, you can use NONE, CHAR or WORD.
(3) tag_delete(tagname): delete this tag, and remove the special editing or binding of this tag.
(4) tag_remove(tagname[,startindex[,endindex]] … ): Delete the tag, but do not remove the tag’s special edits or bindings.
In addition to the possibility to define your own tags using tag_add( ), the system has a built-in tag SEL, which represents the selected interval.
After understanding the concept of tag, we can now do editing for specific interval text or selected text.
Example#1
Lines 14 and 15 of this program will first set two bookmarks, then line 18 will set the text between the two bookmarks to tag1, and finally set the foreground color to blue and the background color to light yellow for this tag1.
from tkinter import *
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
# Create Text
text = Text(root)
for i in range(1,10):
text.insert(END,str(i) + ' apidemos.com \n')
# set mark
text.mark_set("mark1","5.0")
text.mark_set("mark2","8.0")
# set mark
text.tag_add("tag1","mark1","mark2") # Name the text between mark1 and mark2 as tag1
text.tag_config("tag1",foreground="blue",background="lightyellow") # Perform specific edits or bindings for tags
text.pack(fill=BOTH,expand=True)
root.mainloop()
Output:
Example#2
When the design selects text, the selected text can be displayed according to the selected text size.
from tkinter import *
from tkinter.font import Font
from tkinter.ttk import *
def sizeSelected(event):
f=Font(size=sizeVar.get()) # Get the new font size
text.tag_config(SEL,font=f)
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
# create toolbar
toolbar = Frame(root,relief=RAISED,borderwidth=1)
toolbar.pack(side=TOP,fill=X,padx=2,pady=1)
# create font size Combobox
sizeVar = IntVar()
size = Combobox(toolbar,textvariable=sizeVar)
sizeFamily = [x for x in range(8,30)]
size["value"] = sizeFamily
size.current(4)
size.bind("<<ComboboxSelected>>",sizeSelected)
size.pack()
# create Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Five Hundred Miles\n")
text.insert(END,"If you miss the rain I'm on,\n")
text.insert(END,"You will know that I am gone.\n")
text.insert(END,"You can hear the whistle blow\n")
text.insert(END,"A hundred miles,\n")
text.focus_set()
root.mainloop()
Output:
The above program is designed to use SEL as the basis for changing the font size, see line 8, so when we cancel the selection, the original edited text will return to the original size. In the program design, we can also add the tag tag in the third parameter of the insert( ) method, and then you can set this tag directly.
Example#3
When inserting a song title, set this title to Tag "a" at the same time, and then set the tag to center-aligned, blue, with underline in line 37.
from tkinter import *
from tkinter.font import Font
from tkinter.ttk import *
def sizeSelected(event):
f=Font(size=sizeVar.get()) # Get the new font size
text.tag_config(SEL,font=f)
root = Tk()
root.title("apidemos.com")
root.geometry("300x180")
# create toolbar
toolbar = Frame(root,relief=RAISED,borderwidth=1)
toolbar.pack(side=TOP,fill=X,padx=2,pady=1)
# create font size Combobox
sizeVar = IntVar()
size = Combobox(toolbar,textvariable=sizeVar)
sizeFamily = [x for x in range(8,30)]
size["value"] = sizeFamily
size.current(4)
size.bind("<<ComboboxSelected>>",sizeSelected)
size.pack()
# create Text
text = Text(root)
text.pack(fill=BOTH,expand=True,padx=3,pady=2)
text.insert(END,"Five Hundred Miles\n","a") # Set Tag at the same time when inserting
text.insert(END,"If you miss the rain I'm on,\n")
text.insert(END,"You will know that I am gone.\n")
text.insert(END,"You can hear the whistle blow\n")
text.insert(END,"A hundred miles,\n")
text.focus_set()
# Set Tag a to centered, blue, with underline
text.tag_config("a",foreground="blue",justify=CENTER,underline=True)
root.mainloop()
Output: