Python Tkinter - 文本框文本突出显示错误
Python Tkinter - Textbox text highlight error
我关注了一个已回答的问题 here,但由于某些原因无法正常工作。下面是我尝试过的示例代码以及我遇到问题的地方。
from Tkinter import *
import Tkinter as tk
root = Tk()
textbox = Text(root)
textbox.insert(INSERT, "Hello, world!\n")
textbox.insert(END, "i highlight you, you hightlight him, he highlights me...loop it")
textbox.pack(expand=1, fill=BOTH)
class CustomText(tk.Text):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
def highlight_pattern(self, pattern, tag, start="1.0", end="end",
regexp=False):
'''Apply the given tag to all text that matches the given pattern
If 'regexp' is set to True, pattern will be treated as a regular
expression.
'''
start = self.index(start)
end = self.index(end)
self.mark_set("matchStart", start)
self.mark_set("matchEnd", start)
self.mark_set("searchLimit", end)
count = tk.IntVar()
while True:
index = self.search(pattern, "matchEnd","searchLimit",
count=count, regexp=regexp)
if index == "": break
self.mark_set("matchStart", index)
self.mark_set("matchEnd", "%s+%sc" % (index, count.get()))
self.tag_add(tag, "matchStart", "matchEnd")
CustomText()
textbox.tag_configure("red", foreground="red")
textbox.highlight_pattern("loop it", "red")
root.mainloop()
我收到以下错误:
textbox.highlight_pattern("loop it", "red")
AttributeError: Text instance has no attribute 'highlight_pattern'
我想要的:
我明白了这个错误,但我不知道我应该如何突出显示 loop it
或我的 textbox
中的任何文本,使用 class 中提到的 [=] 28=]。这听起来很简单,但我仍然无法弄清楚我应该如何使用它 class。
改变这个:
textbox = Text(root)
为此:
textbox = CustomText(root)
当然,您必须重新安排代码,以便在使用前定义 class。
我关注了一个已回答的问题 here,但由于某些原因无法正常工作。下面是我尝试过的示例代码以及我遇到问题的地方。
from Tkinter import *
import Tkinter as tk
root = Tk()
textbox = Text(root)
textbox.insert(INSERT, "Hello, world!\n")
textbox.insert(END, "i highlight you, you hightlight him, he highlights me...loop it")
textbox.pack(expand=1, fill=BOTH)
class CustomText(tk.Text):
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
def highlight_pattern(self, pattern, tag, start="1.0", end="end",
regexp=False):
'''Apply the given tag to all text that matches the given pattern
If 'regexp' is set to True, pattern will be treated as a regular
expression.
'''
start = self.index(start)
end = self.index(end)
self.mark_set("matchStart", start)
self.mark_set("matchEnd", start)
self.mark_set("searchLimit", end)
count = tk.IntVar()
while True:
index = self.search(pattern, "matchEnd","searchLimit",
count=count, regexp=regexp)
if index == "": break
self.mark_set("matchStart", index)
self.mark_set("matchEnd", "%s+%sc" % (index, count.get()))
self.tag_add(tag, "matchStart", "matchEnd")
CustomText()
textbox.tag_configure("red", foreground="red")
textbox.highlight_pattern("loop it", "red")
root.mainloop()
我收到以下错误:
textbox.highlight_pattern("loop it", "red")
AttributeError: Text instance has no attribute 'highlight_pattern'
我想要的:
我明白了这个错误,但我不知道我应该如何突出显示 loop it
或我的 textbox
中的任何文本,使用 class 中提到的 [=] 28=]。这听起来很简单,但我仍然无法弄清楚我应该如何使用它 class。
改变这个:
textbox = Text(root)
为此:
textbox = CustomText(root)
当然,您必须重新安排代码,以便在使用前定义 class。