I am working on a simple messaging system, and need to add the following to a Tkinter text widget:
- Spell Check
- Option To Change Font ( on selected text )
- Option to change font color ( on selected text )
- Option to Change Font Size ( on selected text )
I understand that the tkinter Text widget has the ability to use multiple fonts and colors through the tagging mechanism, but I don’t understand how to make use of those capabilities.
How can I implement those features using the features of the Text widget? Specifically, how can I change the font family, color and size of words, and how could I use that to implement something like spellcheck, where misspelled words are underlined or colored differently than the rest of the text.
The Tkinter text widget is remarkably powerful, but you do have to do some advanced features yourself. It doesn’t have built-in spell check or built-in buttons for bolding text, etc, but they are quite easy to implement. All the capabilities are there in the widget, you just need to know how to do it.
The following example gives you a button to toggle the bold state of the highlighted text — select a range of characters then click the button to add and then remove the bold attribute. It should be pretty easy for you to extend this example for fonts and colors.
Spell check is also pretty easy. the following example uses the words in /usr/share/dict/words (which almost certainly doesn’t exist on Windows 7, so you’ll need to supply a suitable list of words) It’s rather simplistic in that it only spell-checks when you press the space key, but that’s only to keep the code size of the example to a minimal level. In the real world you’ll want to be a bit more smart about when you do the spell checking.