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...