Found 7 entries, viewing 1 through 5.


Building Cocoa GUIs in Python with PyObjC, Part Six

Handling Images

Cocoa provides an easy interface for dealing with images, NSImage. It can be a bit tricky in Python, but once you get it right the first time, it is fairly easy. For our application we have the images stored inside the audio tags and need to populate an NSImage object from binary data stored as a Python byte-string.

There are a number of methods for initializing an NSImage object from various sources. A few of note:


All of these are worth reading about in the developer documentation. We will be using initWithData:. This accepts a NSData object to make the NSImage. This is a multiple step process, so we'll create a method to make things easier:

def buildNSImage(bytes):
    data = NSData.dataWithBytes_length_(bytes, len(bytes))
    return NSImage.alloc().initWithData_(data)

bytes is a Python byte-string containing the image. NSImage will automatically handle the formatting ...

Continue Reading

Permalink | Posted: Dec 25, 2008 | Tags: python osx programming pyobjc cocoa

Building Cocoa GUIs in Python with PyObjC, Part Five

Adding Python Modules to the Bundle

If you try to use Python modules on the standard OS X Python path import statements will work fine. However, if you have non-standard modules that might be in a different location, or ones that you want to ship with, you will notice you can't just import them.

To bring them into the application bundle you'll have to go through a couple steps, but when it is all done the application will be able to use the modules, and you don't have to require the end user to install anything extra.

  1. Add the files to the Xcode project.

    • Select 'Project -> Add to Project' (option-command-a)
    • Select the Python module (directory) that you want to add.
    • On the next screen select "Copy items into destination group's folder (if needed) Add to Project dialog
    • Select the correct targets in 'Add to Targets'
    • Select "Create Folder References for ...

Continue Reading

Permalink | Posted: Sep 29, 2008 | Tags: python osx programming pyobjc cocoa

Key-Value Coding in PyObjC

So when doing GUIs in with PyObjC you'll realize that the IBOutlet for tying objects to variables have a few limitations. The one I hit was that a single outlet can only be connected to one object. So I read up on Key-value coding. So to do this you have to add two functions per variable:

def name(self):
     return self.var
def setName(self, x):
     self.var = x

You can imagine that this becomes tedious really fast. Luckily there is a solution. As an example:

from PyObjCTools.KeyValueCoding import kvc

class controller(NSWindowController, kvc):
    title = ""
    artist = ""
    album = ""

So this ties __getattr__ and __setattr__ to valueForKey: and setValue:forKey. Making every class variable available for Key-value coding. Very handy.

Permalink | 1 comment | Posted: Sep 10, 2008 | Tags: python pyobjc cocoa

Building Cocoa GUIs in Python with PyObjC, Part Four

Creating an Open Dialog

Our controller doesn't really do anything at this point. We'll begin by adding an open dialog for the user. This will require us to really delve into the Objective C bridge provided by PyObjC. We'll start by reading some documentation that ships with Xcode.

Let's launch the local documentation browser in Xcode. Under "Help" click "Documentation". In the newly opened documentation browser select the "Mac OS X 10.5" documentation set and search for NSOpenPanel. This is the class we'll be working with to create our open dialog. Have a look through the documentation, then let's code.

First we do some basic stuff, creating the object and setting some permissions. A modified open method on our looks like this:

filetypes = ('mp3', 'ogg', 'mp4', 'flac', 'm4a', 'm4p')

def open_(self, sender):
    panel = NSOpenPanel.openPanel()
    panel ...

Continue Reading

Permalink | Posted: Jul 04, 2008 | Tags: python osx programming pyobjc cocoa

Building Cocoa GUIs in Python with PyObjC, Part Three

Writing A Python Controller

First let's add your new controller into the GUI. Add an object from "Objects & Controllers" into your GUI. Now click on it and bring up the "Identity" tab in the inspector. The first drop-down lets you select a class, find "controller" and pick it.

Now Open up your newly created with your favorite text editor, and we'll begin.

#  QuickTag
#  Created by Scott Paul Robertson on 6/11/08.
#  Copyright (c) 2008 __MyCompanyName__. All rights reserved.

from objc import YES, NO, IBAction, IBOutlet
from Foundation import *
from AppKit import *

class controller(NSWindowController):

There are two parts we will be adding, the Outlets (variables) and Actions (methods). first lets add a few outlets to the class controller.

    name = IBOutlet()
    artist = IBOutlet()
    albumArtist = IBOutlet()
    album = IBOutlet()

These class variables can now be connected to various fields in your GUI. In Interface Builder ...

Continue Reading

Permalink | Posted: Jun 23, 2008 | Tags: python osx programming pyobjc cocoa



42 | django | python | oggify | OSCON | OSCON07 | osx | utosc | mythtv | security | reading | music | gaming | programming | tips | shell | vim | pyobjc | cocoa | iphone | blog | git


<< Jan >>
Mo Tu We Th Fr Sa Su
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 1 2 3


This space reserved for a search box

A Django site. Hosted on a Slicehost Slice