O'Reilly logo

Tkinter GUI Programming by Example by David Love

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required