[wxPython-users] Re: General UI Design

Nitro nitro at dr-code.org
Wed Nov 15 12:32:05 PST 2006


Am 15.11.2006, 20:52 Uhr, schrieb Guenter Dannoritzer <dannoritzer at web.de>:

> Nitro wrote:
>>
>> Right now I don't have one. I can give you some pseudo-code excerpts  
>> though
>
> Thanks Matthias for that example.
>
>>
>> class Events(object):    # this is a module really in my code and a
>> little different
>>     createSomeObject = "createSomeObject"
>>     createSomeObjectFinished = "createSomeObjectFinished"
>>
>> class Processor(object):
>>     def addCommand(self, cmd, callbackWhenFinished):
>>         # process cmd here
>>
>> class System(object):
>>     def __init__(self):
>>         self.processor = Processor()
>>         louie.connect( self.onCommandCreateSomeObject,
>> Events.createSomeObject )
>>
>>     def onCommandCreateSomeObject(self, id, name, color):
>>         def onCommandCreateSomeObjectFinished(result):
>>             louie.send( events.createSomeObjectFinished, result =  
>> result )
>>         self.processor.addCommand( CmdCreateSomeObject( id, name, color
>> ), onCommandCreateSomeObjectFinished )
>>
>
> [...]
>
> So you have a class for each command, like here the class is
> CmdCreateSomeObject(). It has the Do() and Undo() member functions that
> are called by the Processor class. Now what I don't understand quite is
> how does the command class work with the data. All what the Processor
> seems to do is call the .Do() function and put the class instance on the
> undo stack. Now it somehow needs to call the callback function and pass
> it the return value. I guess that would work by have the return value of
> the .Do() function be the parameter for the call back function?

Yes.

> But how would each command class be connected with the model so that it
> can work on the data?

Seems I left that out in the example above. I am currently doing it along  
the lines of

class System(object):
     def __init__(self):
         self.processor = Processor()
         self.core = Core()	# core is the model
         louie.connect( self.onCommandCreateSomeObject,  
Events.createSomeObject )

     def onCommandCreateSomeObject(self, id, name, color):
          def onCommandCreateSomeObjectFinished(result):
              louie.send( events.createSomeObjectFinished, result = result )
         self.processor.addCommand( CmdCreateSomeObject( self.core, id,  
name, color), onCommandCreateSomeObjectFinished )

class CmdCreateSomeObject(object):
	def __init__(self, core, id, name, color):
		self.core = core
		self.id = id
		self.name = name
		self.color = color

	def Do(self):
		self.obj = self.core.createObject(self.id, self.name, self.color)
		return self.obj

	def Undo(self):
		self.core.destroyObject(self.obj)

At least that's the gist of it. My command classes all derive from a base  
comand class which defines an __init__(self, core, *args, **keys) and  
basically copies this into self.__dict__. I also have a special  
CompositeCommand class which you can use to define a single command that  
consists of multiple subcommands.

-Matthias




More information about the wxpython-users mailing list