Adding a context menu to our text editor

Once again we will need to add some code to our __init__ method. Since there will only be one Menu needed for our context menu we can just define this directly instead of making another function. Type this code underneath the previous menu code:

self.right_click_menu = tk.Menu(self, bg="lightgrey", fg="black", tearoff=0)
self.right_click_menu.add_command(label='Cut', command=self.edit_cut)
self.right_click_menu.add_command(label='Copy', command=self.edit_copy)
self.right_click_menu.add_command(label='Paste', command=self.edit_paste)

Another Menu widget is created using the same arguments as all of our cascades. We then add three commands to it—cut, copy, and paste. These will still do nothing at the moment, but first things first let's bind this menu to the right mouse button in our bind_events method:

def bind_events(self):
...
self.text_area.bind("<Button-3>", self.show_right_click_menu)

Now let's define the methods that will allow our right-click context menu to function. We need three new methods named after the arguments to our add_command calls:

def edit_cut(self, event=None):
"""
Ctrl+X
"""
self.text_area.event_generate("<Control-x>")
self.line_numbers.force_update()

def edit_paste(self, event=None):
"""
Ctrl+V
"""
self.text_area.event_generate("<Control-v>")
self.line_numbers.force_update()
self.highlighter.force_highlight()

def edit_copy(self, event=None):
"""
Ctrl+C
"""
self.text_area.event_generate("<Control-c>")

Since these methods all start with the word edit, they will automatically be pulled into the edit cascade in our top menu. In order to fit in with our convention they require a docblock containing their keyboard shortcut.

Each method here has already been defined in our TextArea widget, so all we need to do is pass the event over to it with event_generate.

In the case of cut and paste, the line numbers and keywords are going to change, so we need to ensure both our Highlighter and LineNumbers class are told to update. We will need to add a method to each so that these methods do not error:

class Highlighter:
...
def force_highlight(self):
self.highlight()


class LineNumbers(tk.Text):
...
def force_update(self):
self.on_key_press()

No complex logic in either of these functions; they merely call the method, which is bound to a key event on our TextArea class.

With all of this added, go ahead and run your texteditor.py file. Check out the right-click menu, and make sure that the new methods have appeared in your Edit cascade, too:

Both of our menus are now taken care of. If you want to add more functionality to your right-click menu at the end of this chapter, feel free to add more commands to it then. For now, we are going to move on to a new topic—handling files. This will allow us to add New, Open, and Save functionality to our file menu.

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

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