Reputation: 324
DESCRIPTION:
I have a textbox with text in it. See the image below.
QUESTION:
I want the highlighted text to hide when I click the "Hide" button. And then show the text, when I click the Show button (not there in the picture). Similar to pack() and pack_forget(), but this time, for text and not widget.
Upvotes: 0
Views: 2154
Reputation: 386230
You can add a tag to a region of text, and configure the tag with elide=True
to hide the text, and set it to elide=False
to show it.
Here's a little example:
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()
Upvotes: 2