CurrencyConverter

A simple NIB based application. Start with this one. Also see the PyObjC tutorial.

Sources

CurrencyConverter.py

import time
import sys

import objc
from AppKit import NSObject, NSApplicationMain


class Converter (NSObject):
    def convertAmount(self, amt, rate):
        return amt*rate


class ConverterController (NSObject):

    # First define the IB Outlets, the 'ivar' calls below define new
    # instance variables in the objective-C class (e.g. visible
    # for introspection in objective-C)
    converter = objc.IBOutlet()
    dollarField = objc.IBOutlet()
    rateField = objc.IBOutlet()
    totalField = objc.IBOutlet()

    def awakeFromNib(self):
        # Provide some defaults for the user...
        self.dollarField.setFloatValue_(2.0)
        self.rateField.setFloatValue_(3.0)

    @objc.IBAction
    def convert_(self, sender):
        rate = self.rateField.floatValue()
        amt = self.dollarField.floatValue()

        total = self.converter.convertAmount(rate, amt)
        self.totalField.setFloatValue_(total)
        self.rateField.selectText_(self)

        #x = NSRunAlertPanel("Calculation Result",
        #    "The result is %s"%(total), "OK", None, None)


sys.exit(NSApplicationMain(sys.argv))

setup.py

"""
Script for building the example.

Usage:
    python3 setup.py py2app
"""
from setuptools import setup

setup(
    name="CurrencyConverter",
    app=["CurrencyConverter.py"],
    data_files=["English.lproj"],
    setup_requires=[
        "py2app",
        "pyobjc-framework-Cocoa",
    ]
)

Resources