Expanding our demo

Open back up the demo file we created earlier, which shows off the current cursor location. Underneath your update_index function, we will add four more bindings which will be used to practice the utilization of these special strings:

def down_three_lines(event=None):
current_cursor_index = str(text.index(tk.INSERT))
new_position = current_cursor_index + "+3l"
text.mark_set(tk.INSERT, new_position)

return "break"


def back_four_chars(event=None):
current_cursor_index = str(text.index(tk.INSERT))
new_position = current_cursor_index + "-4c"
text.mark_set(tk.INSERT, new_position)

return "break"

These two functions demonstrate the use of the +nl and -nc strings.

We get the cursor's current position in the same way as before, then add either "+3l" or "-4c" to create the new index at which we will move it to.

In order to move the cursor, we need to use the mark_set method, passing it the name of the mark (which is the INDEX constant again) and the new index which we have created.

Since these functions will be bound to keyboard shortcuts, we will return the break string to prevent any default behaviors:

def highlight_line(event=None):
start = str(text.index(tk.INSERT)) + " linestart"
end = str(text.index(tk.INSERT)) + " lineend"
text.tag_add("sel", start, end)

return "break"


def highlight_word(event=None):
word_pos = str(text.index(tk.INSERT))
start = word_pos + " wordstart"
end = word_pos + " wordend"
text.tag_add("sel", start, end)

return "break"

To demonstrate the line and word navigations, we create two more functions which will be used to select different regions of the Text widget.

Demonstrating the line management, we will use a function which highlights the current line. This was mentioned before and is now demonstrated within our application.

We add the special tag sel, which is used to indicate selection, to the start and end of the cursor's current line, which we refer to by adding the linestart and lineend strings to its current index.

We then define a function that will highlight the word the cursor currently sits inside. We do this by again getting the cursor's current index, then appending wordstart and wordend to get our two indexes.

Once again, the sel tag is added between these indexes in order to create the selection.

Now we just need to bind these functions to keys as before:

text.bind('<Control-h>', highlight_line)
text.bind('<Control-w>', highlight_word)
text.bind('<Control-d>', down_three_lines)
text.bind('<Control-b>', back_four_chars)

Give this new version of the demo application a try. Again, type or copy over a few lines of text and press each keyboard shortcut to get a feel for how everything is working.

Hopefully, now you have a good grasp of how Tkinter's Text widget handles indexing and positioning. This will come in very handy when we search through our text to find Python keywords that require highlighting.

Speaking of which, it is now time to move on to tags. Tags are how the Text widget can style individual elements differently, and will be used to add the syntax highlighting.

We have already been adding one tag to our applications—sel—since this is a special tag which refers to the boundaries within the widget which are selected.

Using our knowledge of indexing, let's begin experimenting with adding tags to areas within a Text widget.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset