使用带换行的文本小部件,或者更准确地定义字符串,或者进行一些后处理以删除所有多余的空白。使用中的代码
this answer
例如:
import Tkinter as tk
import re
class CustomText(tk.Text):
'''A text widget with a new method, HighlightPattern
example:
text = CustomText()
text.tag_configure("red",foreground="#ff0000")
text.HighlightPattern("this should be red", "red")
The HighlightPattern method is a simplified python
version of the tcl code at http://wiki.tcl.tk/3246
'''
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
def HighlightPattern(self, pattern, tag, start="1.0", end="end", regexp=True):
'''Apply the given tag to all text that matches the given pattern'''
start = self.index(start)
end = se