Tkinter Text Create Marks
When editing a file, you can create bookmarks (Marks) in special locations in the file for easy access. Bookmarks are not displayed, but are recorded within the editing system. If the content of a bookmark is deleted, this bookmark will also be automatically deleted. There are actually two default bookmarks within tkinter: INSERT and CURRENT. the following are commonly used bookmark related methods.
(1) index(mark): pass back the line and column of the specified bookmark.
(2) mark_names( ): pass back all the bookmarks of this Text object.
(3) mark_set(mark,index): set the bookmark at the specified index position.
(4) mark_unset(mark): cancel the specified bookmark setting.
Example#1
Set up two Marks and then list the contents between the bookmarks.
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 bookmark
text.mark_set("mark1","5.0")
text.mark_set("mark2","8.0")
print(text.get("mark1","mark2"))
text.pack(fill=BOTH,expand=True)
root.mainloop()
Output: