CurrencyConvBinding

A rewrite of the CurrencyConverter example using Cocoa Bindings.

Originally from Introduction to Developing Cocoa Applications Using Bindings, converted to PyObjC by u.fiedler.

Sources

Converter.py

from objc import super
import objc
from Foundation import NSObject

class Converter (NSObject):
    exchangeRate = objc.ivar.double()
    dollarsToConvert = objc.ivar.double()

    def init(self):
        self = super(Converter, self).init()
        self.exchangeRate = 3
        self.dollarsToConvert = 4
        return self

    def amountInOtherCurrency(self):
        return self.dollarsToConvert * self.exchangeRate

Converter.setKeys_triggerChangeNotificationsForDependentKey_(
    ["dollarsToConvert", "exchangeRate"], "amountInOtherCurrency"
)

CurrencyConvBinding.py

from PyObjCTools import AppHelper

# import classes required to start application
import Converter
import CurrencyConvBindingDocument

if __name__ == '__main__':
    AppHelper.runEventLoop()

CurrencyConvBindingDocument.py

from Cocoa import NSDocument

class CurrencyConvBindingDocument (NSDocument):
    def windowNibName(self):
        return "CurrencyConvBindingDocument"

setup.py

"""
Script for building the example:

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

plist = dict(
    CFBundleDocumentTypes=[
        dict(
            CFBundleTypeExtensions=[
                "CurrencyConvBinding",
                "*",
            ],
            CFBundleTypeName="CurrencyConvBinding File",
            CFBundleTypeRole="Editor",
            NSDocumentClass="CurrencyConvBindingDocument",
        ),
    ],
)

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

Resources