The Highlighter class

Open up your text editor project from the last chapter and create a new file in its root folder called highlighter.py. We can now write our Highlighter class in here:

import tkinter as tkclass Highlighter:    def __init__(self, text_widget):        self.text_widget = text_widget        self.numbers_color = "blue"        self.keywords_color = "orange"        self.keywords = ["True", "False", "def", "for", "while", "import",                          "if", "elif", "else"]         self.disallowed_previous_chars = ["_", "-", "."]        self.tag_configure("keyword", foreground=self.keywords_color)        self.tag_configure("number", foreground=self.numbers_color)        self.text_widget.bind('<KeyRelease>', self.on_key_release)

Our Highlighter class will need to keep a reference to the Text widget which ...

Get Tkinter GUI Programming by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.