Let's work towards showing the line numbers to the left of the Text widget. This will require us to tweak the code at various places. So, before we start coding, let's look at what we are trying to achieve here.
The View menu has a menu item that allows users to choose whether to show line numbers. We only want to show the line numbers if the option is selected, as shown in the following screenshot:
If the option is selected, we need to display the line numbers in the left frame that we created earlier.
The line number should update every time a user enters a new line, deletes a line, cuts or pastes text from the line, performs an undo
or a redo
operation, opens an existing file, or clicks on the new menu item. In short, the line number should be updated after every activity results in change of content.
Therefore, we need to define a function called on_content_changed()
. This function should be called after the definitions of every key press, cut, paste, undo, redo...