0

I am inheriting a Tkinter Text widget to provide some customization.

class CustomText(Text):
    def __init__(self, *args, **kwargs):
        Text.__init__(self, *args, **kwargs)
        self.bind("<Control-v>", lambda event: self.paste())

    def paste(self):
        self.see('insert')
        return "break"

I want to override the <Control-v> (Paste) event handler and extended the basic operation with my own implementation.

Inheriting and creating my own callback to <Control-v> event was straightforward, but I didn't manage to execute the base callback.

I tried

def paste(self):    # -1-
    super().event_generate('<Control-v')
    self.see('insert')
    return "break"

def paste(self):    # -2-
    super.event_generate('<Control-v')
    self.see('insert')
    return "break"    

def paste(self):    # -3-
    self.see('insert')

In -1- I've tried to use super() to invoke the base class method - didn't work

In -2- I've tried to use super to invoke the base class method - didn't work

In -3- I've tried to let tkinter run the base class, didn't work - as based on bindtags, my code will run first, then Text method will run (thus not giving my intention)

How can I invoke the base class method (override and extend the <Control-v> behavior)?

NirMH
  • 4,769
  • 3
  • 44
  • 69
  • Your question is confusing. You ask how to override the base functionality, then you say you know how and it works fine, then you say you want the base functionality afterall. If you want your custom behavior and also the base functionality, just don't return "break". – Bryan Oakley Jul 19 '17 at 00:31
  • @BryanOakley: Hi Bryan, just rephrased my question. The issue that i'm bother in is the order of method execution. I first want the base operation, then my own... bindtags enforces my code running first, then base class – NirMH Jul 19 '17 at 00:41
  • In addition to answers to the question that this is a duplicate of, see this answer for a full explanation of how an event propagates through bind tags: https://stackoverflow.com/a/11542200/7432 – Bryan Oakley Jul 19 '17 at 03:04
  • @BryanOakley: I've read your answer - this is a good explanation on bindtag which i already know. My question is not a duplicate - i'm asking how to change the order of instance/widget bindings execution without altering the bintags itself... – NirMH Jul 19 '17 at 21:45
  • You can't. Bind tags are the mechanism for changing the order of binding execution. That's exactly what it's for. If you want something to happen after the class binding, either move the class bind tag, or add a new bind tag after the class bind tag. – Bryan Oakley Jul 19 '17 at 22:46

1 Answers1

1

I'm not aware of any way to directly do what you're asking, but you can fake it via after():

def paste(self):
    self.after(1, lambda: self.see('insert'))
jasonharper
  • 9,450
  • 2
  • 18
  • 42