Вы можете добавить тег к области текста и настроить тег на elide=True
, чтобы скрыть текст, и установить его на elide=False
, чтобы показать его.
Вот небольшой пример:
import tkinter as tk
def hide():
text.tag_add("hidden", "sel.first", "sel.last")
def show_all():
text.tag_remove("hidden", "1.0", "end")
root = tk.Tk()
toolbar = tk.Frame(root)
hide_button = tk.Button(toolbar, text="Hide selected text", command=hide)
show_button = tk.Button(toolbar, text="Show all", command=show_all)
hide_button.pack(side="left")
show_button.pack(side="left")
text = tk.Text(root)
text.tag_configure("hidden", elide=True, background="red")
with open(__file__, "r") as f:
text.insert("end", f.read())
toolbar.pack(side="top", fill="x")
text.pack(side="top", fill="both", expand=True)
text.tag_add("sel", "3.0", "8.0")
root.mainloop()