1
我正在做一個一致性的工具,我想強調的所有顏色的結果。在下面的代碼中,它只適用於第一行。有新行時,標籤將中斷。例如,當我在字符串中搜索單詞「蟒蛇」下面,標籤只強調了第一線。它不適用於第二和第三行。請幫幫我。如何添加標籤到tkinter文本中的新行?
import tkinter as tk
from tkinter import ttk
import re
# ==========================
strings="""blah blah blah python blah blah blah
blah blah blah python blah blah blah
blah blah blah python blah blah blah
"""
# ==========================
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_widget()
def create_widget(self):
self.word_entry=ttk.Entry(self)
self.word_entry.pack()
self.word_entry.bind('<Return>', self.concord)
self.string_text=tk.Text(self)
self.string_text.insert(tk.INSERT, strings)
self.string_text.pack()
# ==========================
def concord(self, event):
word_concord=re.finditer(self.word_entry.get(), self.string_text.get(1.0, tk.END))
for word_found in word_concord:
self.string_text.tag_add('color', '1.'+str(word_found.start()), '1.'+str(word_found.end()))
self.string_text.tag_config('color', background='yellow')
# ==========================
def main():
root=tk.Tk()
myApp=Application(master=root)
myApp.mainloop()
if __name__=='__main__':
main()
哇,非常感謝您的幫助,Bryan Oakley。這是完美的。 –