python - Tkinter:在文本框中显示/隐藏特定文本
问题描述
描述:
我有一个带有文本的文本框。见下图。
问题:
我希望在单击“隐藏”按钮时隐藏突出显示的文本。然后显示文本,当我单击“显示”按钮时(图片中没有)。类似于 pack() 和 pack_forget(),但这次是针对文本而不是小部件。
解决方案
您可以将标签添加到文本区域,并将标签配置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()
推荐阅读
- ios - 我正在尝试为 Apple Pay 支付处理创建一个基类
- r - 如何在不同长度的时间序列中创建随机间隙?
- vb.net - 字段名未知详情errorkind水晶报告
- sql - NLSSORT Oracle 到雪花
- reactjs - 尝试使用材质 UI 为具有特定自定义类的项目设置样式
- logging - Jmeter:打印beanshell结果的地方
- wcf - 在 nettcp 绑定中请求特定客户端证书
- python - Python PIL:调整大小时透明gif的帧即使在(处置= 2)时也会相互重叠
- asp.net - IIS URL HTTP 到 HTTPS 重定向在 ASP.Net 应用程序上不起作用
- flutter - 颤振中的 Agora.io RTC