[Pyobjc-dev] CurrencyConverter for XCode/IB 3.0
Brought to you by:
ronaldoussoren
|
From: Steve S. <sst...@ma...> - 2008-05-01 02:30:38
|
Sorry if this is a dumb question, I'm recently resubscribed to the
list and didn't find anything about my question it in the archives.
So...
I'm using XCode/IB 3.0.
I want to make the CurrencyConverter example code as clean as possible
by removing all the no-longer-necessary cruft.
In the code below, I've eliminated a ton of imports, and removed the
Nibclassbuilder references.
I _think_ I had to add the objc.ivar calls that were handled by the
Nibclassbuilder but I'd love to be enlightened if there's a better way
to do it.
I'm a little confused since the comments reference 'ivar' calls that
were not in the actual code. When I put them in, I stopped getting
errors related to the ivars not existing.
At this point, the code fails with:
TypeError: Use class methods to instantiate new Objective-C objects
Console says:
4/30/08 10:25:20 PM CurrencyConverter[9877] CurrencyConverterObjC Error
An unexpected error has occurred during execution of the main script
TypeError: Use class methods to instantiate new Objective-C objects
4/30/08 10:26:14 PM com.apple.launchd[113]
([0x0-0x3f13f1].org.pythonmac.unspecified.CurrencyConverter[9877])
Exited with exit code: 255
when trying to create the self.converter (is it an IBOutlet?)
Any help would be greatly appreciated.
I'm planning on going through each example, bringing them into an
XCode project and de-cruftifying them so that I understand everything
that's going on. Sure would be nice to have in the official
distribution...
Thanks in advance for any assistance.
S
#-------------------------------------------------------------------
import time
import sys
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper
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)
dollarField = objc.ivar (u'dollarField')
rateField = objc.ivar (u'rateField')
converter = Converter()
def __init__(self):
self.converter = Converter()
self = super(ConverterController, self).init()
return self
def awakeFromNib(self):
# Provide some defaults for the user...
#self.converter = Converter()
self.dollarField.setFloatValue_(2.0)
self.rateField.setFloatValue_(3.0)
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)
# pass control to AppKit
AppHelper.runEventLoop()
|