First things first: we should ensure that we have some expected key-bindings happening within our Text
widget. We'll collate these in a method called bind_events
and call this method from within our __init__
:
def __init__(self): ... self.bind_events() def bind_events(self): self.bind('<Control-a>', self.select_all) self.bind('<Control-c>', self.copy) self.bind('<Control-v>', self.paste) self.bind('<Control-x>', self.cut) self.bind('<Control-y>', self.redo) self.bind('<Control-z>', self.undo)
This function now ensures that six commonly used keyboard shortcuts will perform their expected behaviors.
Since these behaviors are already handled by the Text
widget (except for select_all
), we only need to emit the relevant events in order to get them to function. The select_all
method is the only one we need to perform the logic for:
def cut(self, event=None): self.event_generate("<<Cut>>") def copy(self, event=None...