我有一个文本小部件,其中打印了大量信息。我喜欢让它的部分最小化,所以我可以隐藏信息直到我需要它。
from tkinter import *
main = Tk()
list1 = ['blah', 'blah', 'blah', 'blah', 'blah', 'blah', "\n" 'blah', 'blah', 'blah', 'blah', 'blah', 'blah', "\n", 'blah', 'blah', 'blah', 'blah', 'blah', 'blah', "\n",'blah', 'blah', 'blah', 'blah', 'blah', 'blah' , "\n"]
list2 = ['blah', 'blah', 'blah', 'blah', 'blah', 'blah', "\n" 'blah', 'blah', 'blah', 'blah', 'blah', 'blah', "\n", 'blah', 'blah', 'blah', 'blah', 'blah', 'blah', "\n",'blah', 'blah', 'blah', 'blah', 'blah', 'blah' , "\n"]
list3 = ['blah', 'blah', 'blah', 'blah', 'blah', 'blah', "\n" 'blah', 'blah', 'blah', 'blah', 'blah', 'blah', "\n", 'blah', 'blah', 'blah', 'blah', 'blah', 'blah', "\n",'blah', 'blah', 'blah', 'blah', 'blah', 'blah' , "\n"]
textwin = Text(main, height=20, width=150)
textwin.grid(rowspan=5, columnspan=5)
textwin.config(state='normal')
textwin.insert('end', "+--------------------------------------------------------------------------------- \n")
textwin.insert('end', list1)
textwin.insert('end', "+--------------------------------------------------------------------------------- \n")
textwin.insert('end', list2)
textwin.insert('end', "+--------------------------------------------------------------------------------- \n")
textwin.insert('end', list3)
main.mainloop()
我尝试在“文本”小部件中放置一个按钮,但它将“文本”小部件缩小到按钮的大小,并且不显示任何其他内容。我查看链接文本,所以我可以使“+”运行一个删除/隐藏文本部分的功能,但我只找到超链接指令。
如果在“文本”小部件中不可行,我会对其他想法持开放态度。也许我必须将所有内容移动到树视图小部件。
答案 0 :(得分:4)
您可以配置文本标签以隐藏一系列字符。然后,您可以通过应用或删除此标记来隐藏或显示一系列字符。如果要在文本中插入按钮,则需要使用文本小部件的window_create
方法。
您必须编写代码才能应用或删除代码。例如,您可以使用" block_start"标记块的开头。或"标题"或类似的东西,然后在该标签上添加双击绑定以查找它与下一个标题之间的所有代码,然后添加或删除"隐藏"标签。或者,您可以嵌入一个按钮小部件来执行相同的操作。
配置代码:
textwin.tag_configure("hidden", elide=True)
隐藏文字,其中start
和end
代表要隐藏的字符范围:
textwin.tag_add("hidden", start, end)
要显示一段文字,其中start
和end
代表要显示的字符范围:
textwin.tag_remove("hidden", start, end)
以下代码是一个人为的示例,可让您双击标题以隐藏或显示标题下的文字:
import tkinter as tk
from tkinter import font
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent, borderwidth=1, relief="sunken")
self.text = tk.Text(self, borderwidth=0, highlightthickness=0, wrap="word")
self.vsb = tk.Scrollbar(self, command=self.text.yview)
self.text.configure(yscrollcommand=self.vsb.set)
self.vsb.pack(side="right", fill="y")
self.text.pack(side="left", fill="both", expand=True)
self.text.tag_configure("hidden", elide=True)
self.text.tag_configure("header", background="black", foreground="white", spacing1=10, spacing3=10)
self.text.tag_bind("header", "<Double-1>", self._toggle_visibility)
for i in range(3):
self.text.insert("end", "Header %s\n" % i, "header")
self.text.insert("end", "blah blah blah blah\nblah blah blah\n\n")
def _toggle_visibility(self, event):
block_start, block_end = self._get_block("insert")
# is any of the text tagged with "hidden"? If so, show it
next_hidden = self.text.tag_nextrange("hidden", block_start, block_end)
if next_hidden:
self.text.tag_remove("hidden", block_start, block_end)
else:
self.text.tag_add("hidden", block_start, block_end)
def _get_block(self, index):
'''return indicies after header, to next header or EOF'''
start = self.text.index("%s lineend+1c" % index)
next_header = self.text.tag_nextrange("header", start)
if next_header:
end = next_header[0]
else:
end = self.text.index("end-1c")
return (start, end)
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()