[Pythonmac-SIG] Opening a Cocoa window from a Python C extension

Bob Ippolito bob at redivi.com
Tue Feb 10 08:22:25 EST 2004

On Feb 10, 2004, at 3:37 AM, Michiel Jan Laurens de Hoon wrote:

> I am porting PyGist (a scientific plotting package for Python, mainly 
> written in C) to Mac OS X. PyGist was originally written for 
> Linux/Unix machines and was recently ported to Windows. For the Mac OS 
> X port, I am using Cocoa to create the graphics windows and calls to 
> CoreGraphics for the actual drawing. The graphics work fine, but there 
> is still a problem with Cocoa windows created from a Python C 
> extension module.
> I create my window with
> window = [ [NSWindow alloc]
>                initWithContentRect: rect
>                          styleMask: style
>                            backing: NSBackingStoreBuffered
>                              defer: NO];
> and try to show the window with
> [window orderFront: nil];
> The window is displayed and everything seems to be working fine; 
> graphics are also drawn correctly. The window becomes the key window 
> if I click in it, however the previous key window also stays key, so I 
> end up with two key windows at the same time. In addition, cursor 
> events are not handled correctly (which I need for zooming and reading 
> out coordinate values).
> I wrote a little test program to find out what is happening. It turns 
> out that the test program works correctly if it is located in a 
> directory test.app/Contents/MacOS/test. However, if I move the "test" 
> program to a different directory it shows the same problem as a window 
> created in the Python C extension, even though the test.app directory 
> is otherwise empty.
> The problem may therefore be that the window that I am trying to open 
> should be somehow linked to the Terminal.app from which I am running 
> Python in order for it to be recognized as a bona fide window. Does 
> anybody know the right way to do this?

Applications in OS X *must* be run from a bundle.  Run your script with 
pythonw, build a bundle yourself (as you did), or use bundlebuilder.  
See http://pythonmac.org/wiki/FAQ .  It has absolutely nothing to do 
with Terminal.app.

You may also want to consider using PyObjC instead of writing ObjC code.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2357 bytes
Desc: not available
Url : http://mail.python.org/pipermail/pythonmac-sig/attachments/20040210/4cc6a344/smime.bin

More information about the Pythonmac-SIG mailing list