A second top-level window

The new window that will spawn for our find/replace box shall be stored in a new file. Create a new script called findwindow.py and begin by entering the following:

import tkinter as tk
import tkinter.ttk as ttk


class FindWindow(tk.Toplevel):
def __init__(self, master, **kwargs):
super().__init__(**kwargs )

self.geometry('350x100')
self.title('Find and Replace')

self.text_to_find = tk.StringVar()
self.text_to_replace_with = tk.StringVar()

top_frame = tk.Frame(self)
middle_frame = tk.Frame(self)
bottom_frame = tk.Frame(self)

We will only need our usual Tkinter and ttk imports for this class.

We subclass Tkinter's Toplevel widget, which is a window that can act as a pop-up window to be displayed on top of a main window. It can be configured much like a regular Tk widget, but requires a master which needs to be an instance of the Tk widget as it cannot act as the main window of an application. A widget such as this is a great fit for our find/replace window, since it is much easier to spawn a new, smaller window above the main one than to try and place the relevant widgets somewhere around our TextArea.

After initializing the Toplevel widget class, we set this window's size to 350 pixels wide and 100 pixels tall using the geometry method. Then its title is changed to "Find and Replace" so that the user knows what it does.

We need two StringVars which will hold the text to be found and the text to replace that with.

To lay out our application, we will be using three Frame widgets. These will be stacked top, middle, and bottom, and span the entire horizontal space.

With the layout taken care of, we can begin adding functional widgets:

find_entry_label = tk.Label(top_frame, text="Find: ")
self.find_entry = ttk.Entry(top_frame, textvar=self.text_to_find)

replace_entry_label = tk.Label(middle_frame, text="Replace: ")
self.replace_entry = ttk.Entry(middle_frame, textvar=self.text_to_replace_with)

self.find_button = ttk.Button(bottom_frame, text="Find", command=self.on_find)
self.replace = ttk.Button(bottom_frame, text="Replace", command=self.on_replace)
self.cancel_button = ttk.Button(bottom_frame, text="Cancel", command=self.destroy)

The find window will need two places for the user to enter text, and these will need to be labeled so that they know which is which. We achieve this with two Entry widgets and two accompanying Label widgets. The Label widgets will have set text since they will not need to update, whereas the Entry widgets are bound to our StringVar so that we can easily get their values and adjust them if need be.

Along the bottom Frame, we will have three Button widgets. These buttons will be responsible for finding text in the TextArea widget which matches that in our text_to_find variable, replacing text in the TextArea widget with the text held in our text_to_replace_with variable, and canceling any further operations and closing the window.

The first two buttons are bound to methods within our FindWindow class, and the cancel button is bound to the top-level widget's destroy method, which we have looked at before.

The two methods will not do anything while our FindWindow is treated as its own class, but later on, when we link it to the rest of our application, it will be able to call methods from our other classes.

For now, enter the following code as these two methods:

def on_find(self):
self.master.find(self.text_to_find.get())

def on_replace(self):
self.master.replace(self.text_to_find.get(), self.text_to_replace_with.get())

In order to preview our FindWindow before the next chapter, we will include a Tk widget as its master. Simply create an instance and call its mainloop as you usually would:

if __name__ == '__main__':
mw = tk.Tk()
fw = FindWindow(mw)
mw.mainloop()

Run this code and you should have two windows pop up. One will be an empty main window, and the other our new FindWindow:

This is where we shall leave the first iteration of our text editor. We have a solid base on which we can add some sophisticated new features in the next chapter.

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

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