views:

24

answers:

1

It's my understanding that in wxPython in OSX, ⌘+w support for closing wx.Window objects. In order to add it, I've had to bind to wx.EVT_KEY_DOWN, checking for event.MetaDown() and event.KeyCode == 'W' explicitly.

In my app, I need to have all windows and dialogs support this. I'm still in the process of layout out my GUI, but I got to thinking, and I'm wondering what the best way to add this support to an existing class easily is. I tried out Multiple Inheritance, but it didn't seem to be working (my event handler never got called).

I was thinking maybe a class decorator, but this is functionality that will be added at runtime, due to the dynamic nature of python. So I'm a little stumped.

PS: I know 'best' is subjective, but I'm honestly looking for anything here that might work that's not an exorbitant amount of code.

A: 

I was thinking maybe a class decorator, but this is functionality that will be added at runtime, due to the dynamic nature of python.

I don't understand why this makes you "a little stumped". The class decorator executes just after the end of the class statement -- yes, that's "at runtime", but, so is the clqss statement itself, all the def statement in it for its methods, and so forth. By the time you instantiate any of the classes thus decorated, the decorator will have run and so the class you're instantiating will have been modified accordingly by the decorator's code. Can you please give a small example of why this isn't working for you?

Alex Martelli
Well, I guess I maybe didn't think it all the way through. Since I'd have to `Bind` the event handler during the `__init__` phase, what I'd basically have to do is wrap the `__init__` function, as well as dynamically add a method to the class in the decorator to handle the event. Lemme see what I can come up with.
Bryan Ross