1

I recently made a simple notepad-like text editor but now I want to implement things like syntax highlighting certain words and automatic indentation as you type. How could I do this dynamically as the user types. So far everything I've done is event-based so I'm guessing I need to have some sort of loop that constantly checks the contents of the textbox as the user is typing? Is tkinter not suited for this? Appreciate it if you steer me in the right direction as to how I can implement this.

TheEyesHaveIt
  • 980
  • 5
  • 16
  • 33
  • You could look at the source code for [IDLE](https://docs.python.org/3/library/idle.html). It is written entirely in Tkinter and does this by default. –  Jul 14 '14 at 23:30
  • @iCodez I did not know IDLE was written in tkinter, thanks for that! – TheEyesHaveIt Jul 14 '14 at 23:38

1 Answers1

2

Tkinter is quite well suited to this sort of thing. It's possible to make a very smart text editor if you're willing to put in some effort.

This answer shows how to get the text widget to fire an event whenever something in the text widget changes. It's a little complicated, but fairly foolproof.

If you want something simpler, you can simply bind on <Any-KeyRelease> which will fire an event whenever the user releases a key. You can then use the information in the event object to decide what to do. It won't handle the case where you cut and paste with the mouse, for example, and your binding will fire for arrow keys and other non-inserting keys, which is why I recommend the more complicated solution.

This answer shows an example of using a binding on <space> to do do a simple spellcheck, and also shows a fairly simplistic implementation of a toolbar with a "bold" button.

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685