Your first solution is on the right path. In the second, you'd need to either use NSProxy
or write your own explicit delegation, which is a pain (and the third isn't the way to do that—you can do something similar through swizzling, but don't).
However, there are a few problems.
First, this code:
self = NSPanel.initWithContentRect_styleMask_backing_defer_(rect, NSBorderlessWindowMask, buf, flag)
… is equivalent to this ObjC code:
self = [NSPanel initWithContentRect:rect
styleMask:NSBorderlessWindowMask
backing:buf
defer:flag]
… when what you want is:
self = [super initWithContentRect:rect
styleMask:NSBorderlessWindowMask
backing:buf
defer:flag]
The PyObjC equivalent of the latter is:
self = super().initWithContentRect_styleMask_backing_defer_(rect, NSBorderlessWindowMask, buf, flag)
Or, in 2.x:
self = super(HUDWindow, self).initWithContentRect_styleMask_backing_defer_(rect, NSBorderlessWindowMask, buf, flag)
In ObjC, super
isn't the superclass, but a special magic object which is effectively "self as an instance of the superclass". It's almost exactly like Python 3's super()
, or the less-magical Python 2 super
, and PyObjC does the extra work to mean you can ignore that "almost".
What you were doing was (trying to) initialize the NSPanel
class object (or maybe the class object of the PyObjC bridge to NSPanel
, I'm not sure which), or possibly even trying to initialize the rect
as if it were an NSPanel
. Whatever you're doing, it can't possibly do anything to initialize your self
instance. (And assigning the result to self
doesn't do the same thing in Python as in ObjC. It's still usually idiomatic style in PyObjC, but don't be misled by it; the only effect it has is if you use self
again within the initializer method. At any rate, at best that would cover up the problem in ObjC, not fix it…) So, after returning from this method, your self
is still the allocated but uninitialized NSPanel
instance that it was before you called anything.
This is mostly explained pretty early in the tutorial, in Two Phase Instantiation.