Event triggering and weak references

Boudewijn Rempt boud at rempt.xs4all.nl
Sun Apr 16 07:50:12 EDT 2000


Ricardo Nogueira <rnog2438 at mail.usyd.edu.au> wrote:

> I am Smalltalk addicted : )  and this means that I can't do with any other
> language, ex. C, C++, Java,  : (
> This was true until I found Python 2 weeks ago at Sydney Uni. I loved its
> modularity, pluggin behaviour and freedom.
> But I am missing Smalltalk event handling... Smalltalk (like Python) support
> the call-Back mechanism, but there is a more powerful inter-object interest
> registering that I haven't found in Python (so far).

If I understand you correctly, then you need to take a look at PyQt. It
offers a signals and slot concept, whereby you have a widget emitting
signals, which you can connect to slots in other widgets.

To translate your example into actual code:


class QCar(Qobject):

  def __init__(self, *args):
	  QObject.__init__,(self, ) + args)
		self.speed=120

  def SpeedChanged(self):
	  # whenever we call self.speedChanged, all other objects
		# that have registered with the signal "speedChanged"
		# will get notified.
	  self.emit(PYSIGNAL("speedChanged", (self.speed,))
		
class QCarSpeedoMeter(QObject):

  def setSpeed(speed):
	  ...
	
class QCarTooFastFlashLight(QObject):

  def checkSpeed(speed):
	  if speed > 100:
		  ...
			
class QCarApp(QObject):
  
	def __init__(self, *args)
	  QObject.__init__,(self, ) + args)
		#
		# Create the various objects
		#
		self.car=QCar()
		self.speedometer=QCarSpeedoMeter()
		self.flashlight=QCarTooFastFlashLight()
	  #
		# Connect the signals to the slots.
		#
		self.connect( self.car
		            , PYSIGNAL("speedChanged(int)")
								, self.speedometer.setSpeed
								)
    self.connect( self.car
		            , PYSIGNAL("speedChanged(int)")
								, self.flashlight.checkSpeed
								)

Using this concept is even easier and more natural in Python than
in C++ - in C++ apps you need a precompiler to make it work, in Python
it just works.

You can find the bindings at: 
   http://www.river-bank.demon.co.uk/software/ 
	 
Qt is at: http://www.troll.no

A tutorial and example programs at my website:
   http://www.valdyas.org/python/index.html



-- 

Boudewijn Rempt  | http://www.valdyas.org



More information about the Python-list mailing list