Using our FindWindow class

As you may have guessed, the FindWindow class needs to be imported at the beginning of the file:

from findwindow import FindWindow

Now, we need to create an instance whenever the user presses a key combination. Typically, a Find and Replace window appears on Ctrl and F or Ctrl and R. I will use Ctrl and F for this application:

def bind_events(self):
...
self.bind('<Control-f>', self.show_find_window)

def
show_find_window(self, event=None):
FindWindow(self.text_area)

A method called show_find_window is created, which will be responsible for showing our FindWindow popup.

Inside this method, all we need to do is instantiate the FindWindow class, passing it our TextArea to act as its master. The Toplevel class which it inherits from will handle displaying the window itself.

That's everything for this chapter! Run this version of your text editor application and give the find/replace window a try. See how it highlights all matches in yellow and replaces words with others, even when they are of different lengths:

To try out automatic scrolling, enter (or paste) a large amount of text and search for a word which is not on display. You should see the widget automatically scroll down so that the highlighted match in yellow appears. Pretty cool, right?

Now that we have learned all about navigating Tkinter's powerful Text widget, we can move on to some more advanced parts of the application. These include menus and customization options.

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

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