Re: NSWindow

Lawrence Akka <[email protected]>
Newsgroups gmane.comp.python.pyobjc.devel
Message-ID <[email protected]>
On 29 Jun 2011, at 23:04, Diez B. Roggisch wrote:

> 
> On Jun 29, 2011, at 11:23 PM, Thomas Cool wrote:
> 
>> Hello,
>> I'm starting with PyObjC and am having some trouble creating windows
>> I am not a beginner at either python or cocoa by the way.
>> 
>> Here is what i am trying (both with macports python 2.6 and builtin python 2.6) on 10.6.8
>> 
>> from AppKit import *
>> a=NSWindow.alloc().init()
>> 
>> simply this returns an error:
>> 
>> Wed Jun 29 16:23:09 TCi7.local python[10060] <Error>: kCGErrorInvalidConnection: CGSGetCurrentCursorLocation: Invalid connection
>> Wed Jun 29 16:23:09 TCi7.local python[10060] <Error>: kCGErrorInvalidConnection: CGSNewWindowWithOpaqueShape: Invalid connection
>> Traceback (most recent call last):
>>  File "<stdin>", line 1, in <module>
>> objc.error: NSInternalInconsistencyException - Error (1002) creating CGSWindow
> 
> 
> Let me guess, you just wrote that into a script, and executed it?
> 
> It won't work in Objective-C either, if all you do is just to create a simple executable.
> 
> Instead, you need to create an application bundle, using py2app. Otherwise, the window-server refuses to accept connections. I never really bothered to get to know the gory details, but see e.g. this discussion:
> 
>  http://stackoverflow.com/questions/3704629/why-cant-i-use-cocoa-classes-from-my-python-script
> 
> And especially the related tech-note.
> 
> Diez

You need to create an application object first.

Try something like this.  

#!/usr/bin/env python
# encoding: utf-8

import sys
import os

# We need to import the relvant object definitions from PyObjC
from AppKit import *
from PyObjCTools import AppHelper

# Cocoa prefers composition to inheritance. The members of an object's
# delegate will be called upon the happening of certain events. Once we define
# methods with particular names, they will be called automatically
class Delegate (NSObject):
    def applicationDidFinishLaunching_(self, aNotification):
        '''Called automatically when the application has launched'''
        print "Hello, World!"

    def windowWillClose_(self, aNotification):
        '''Called automatically when the window is closed'''
        print "Window has been closed"
        # Terminate the application
        NSApp().terminate_(self)


def main():
    # Create a new application instance ...
    a=NSApplication.sharedApplication()
    # ... and create its delgate.  Note the use of the
    # Objective C constructors below, because Delegate
    # is a subcalss of an Objective C class, NSObject
    delegate = Delegate.alloc().init()
    # Tell the application which delegate object to use.
    a.setDelegate_(delegate)

    # Now we can can start to create the window ...
    frame = ((200.0, 300.0), (250.0, 100.0))
    # (Don't worry about these parameters for the moment. They just specify
    # the type of window, its size and position etc)
    w = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(frame, 15, 2, 0)
    # ... tell it which delegate object to use (here it happens
    # to be the same delegate as the application is using)...
    w.setDelegate_(delegate)
    # ... and set some properties. Unicode strings are preferred.
    w.setTitle_(u'Hello, World!')
    # All set.  Now we can show the window ...
    w.orderFrontRegardless()
    # ... and start the application
    AppHelper.runEventLoop()

if __name__ == '__main__':
    main()

------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security 
threats, fraudulent activity, and more. Splunk takes this data and makes 
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2

_______________________________________________
Pyobjc-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyobjc-dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.