| PyObjC Tutorial
A small tutorial to start with Python and Cocoa (c) 2005 Jean Bovet |
|
Introduction |
|
|
This small tutorial will teach you how to build a small application using Python and the Cocoa framework. It is assumed that Xcode 2.1 and Python are already installed on your system (Python should be installed by default on Mac OS 10.4 and Xcode can be installed using the free developer DVD that come with your Mac box). |
|
1. Create the project in Xcode 2.1Just before starting coding, make sure you have Xcode 2.1 (or greater) and PyObjC 1.3.7 (or greater) installed. Click here to download the latest version of Xcode and here to download the latest version of PyObjC.
2. Create the custom executable (if necessary)If you have a custom executable under the Executables group (like in the image above), you can skip this chapter.
If you have any problem creating the custom executable, look at this page for more information. 3. Build and run the default applicationSelect (Command-R) to see what happen. A basic Cocoa application will show up: you can see a window, open the print panel, display the about box and so on. 4. Create the interface - part INow, let's add some elements in the window to make something more interesting ;-) We will build a small interface where a button will generated a random number in a text field. Another button will allow us to enable or disable the interface.
You will have to create a button, a text field and a check box. You have to drag each object from the Cocoa palet to the window.
To rename the button, simply double-click on it. Make sure you have the check-box selected (check the button Selected in the Attributes inspector - press Command-1 to display the inspector). First, let make an action in order for our Generate button to invoke a method in our Python object.
This seems complicated at first, but after a few times, this procedure will be really fast ;-) Let's go back to Xcode and click on the PyObjCTutorialAppDelegate.py file to display its content in the Xcode editor. We will now create the Python method that will be called by the Generate button. def generate_(self, sender):
print sender.title()
This method will print the name of the sender which is the Generate button in our example. Note that the _ (underscore) comes because the equivalent Objective-C method that we would write is the following: - (IBAction)generate:(id)sender All : (colon) in Objective-C method call are replaced by _ (underscore) in Python. Build and run the application (Command-R) and click on the Generate button. You should see the string Generate in the Console.
5. Create the interface - part IINow, let's generate the random number and put it in the text field. To do that, we have to access the content of the text field from the Python class. We will use the Cocoa bindings (see Links for more info) to do that.
Go back to Xcode and modify the PyObjCTutorialAppDelegate.py file to be the following: import random
import objc
class PyObjCTutorialAppDelegate(NibClassBuilder.AutoBaseClass):
randomValue = objc.ivar(u"randomValue")
def generate_(self, sender):
self.randomValue = random.random()
Notes
Build and run the application (Command-R). Click on the Generate button and check that the random values are automatically updated in the text field thanks to the underlying Cocoa bindings. 6. Create the interface - part IIIWe will now modify our interface and add the code required to enable or disable the Generate button and the text field when the Enable button is selected or deselected.
Now, let's go back to Xcode create the enable_() method: def enable_(self, sender):
self.generateButton.setEnabled_(sender.state() == NSOnState)
self.randomField.setEnabled_(sender.state() == NSOnState)
This method will enable the button and text field only if the sender state (the Enable check-box) has the state NSOnState (which stands for the selected state). Use NSOffState to know if the check-box is deselected. Note that you don't need to declare the generateButton or randomField members: they are automatically declared by PyObjC. Build and run the application (Command-R). Click on the Enable check-box to see the button and text field being disabled. Exercises left to the reader:
7. ConclusionWell, that's all for now! More to come over the semester... 8. LinksThe Home Page of PyObjC: Complete description of how to create a custom executable and how to deal with Xcode templates: Using PyObjC for developing Cocoa applications with Python (Apple): Intro to Cocoa bindings: Cocoa bindings examples and hints: Apple developer page about Cocoa bindings: |
|
9. Download this tutorial (09/27/2005) |
|
| Binary and source code for Mac OS X (requires Xcode 2.1):
|
|
10. Comments and suggestions |
|
| Send any comments, suggestions or bug reports to jbovet at cs.usfca.edu | |