[wxPython-users] Sending/Using Data with wx.lib.pubsub

chris botos chris.botos at gmail.com
Mon Oct 1 18:41:56 PDT 2007


Rich,

With Werner's comments, and the intervening time, you may have solved
this already.  I'm a little confused about your program structure, but
I have a couple of comments.

On 10/1/07, Rich Shepard <rshepard at appl-ecosys.com> wrote:
> On Mon, 1 Oct 2007, chris botos wrote:
>
> > The subscribed function should look something like this:
> >
> > def SubscribedFunction(self, message):
> >    newvar = message.data
> >    etc.
>
> Chris,
>
>   I'm missing something crucial in the syntax because I cannot get it
> working here.
>
>   I use a module, config.py, as a blackboard to share variables among
> modules. This is where the messages to be published are found. For
> example, there's the publisher message 'projVars' in this module. config.py
> is imported into all other modules and assigned to the variable
> 'self.appData'.
>
>   The sending module has the line:
>
>        Publisher().sendMessage(self.appData.projVars, data=newvar)
>
>   The listening module has this line:
>
>        Publisher().subscribe(self.loadParVar, self.appData.projVars)

The signature for the subscribe method is:
    Publisher().subscribe(listenerMethod, topic)

So your subscribe call is saying that there is a method
"self.loadParVar" that will be called when the topic
"self.appData.projVars" is published, as in your
Publisher().sendMessage(...) call.  If you keep rthe subscribe as is, then
you need a method defined like the following:

def loadParVar(self, message):
    received_newvar = message.data
    # received_newvar, should now be the value 'newvar'
    # from your sendMessage call.
    ...

>
> In this listener module I just added the function:
>
>          def projVars(self, message):
>            self.newvar = self.appData.projVars.data
>
> but the variable 'self.newvar' is not seen when I try to use it in another
> function:

As Werner mentioned, you need "self,newvar = message.data".  The
Publisher calls your listener function with the sendMessage data value
embedded in the message object that is the listener function's first
argument.

Also, the method projVars was not specified as the listener, so I
don't think it is *ever* called.  As was pointed out, when some other
function tries to use self.newvar, it crashes because it was never
created.

>
>   File "/data1/eikos/fuzSetPage.py", line 391, in loadParVar
>     self.dispVar = self.newvar
> AttributeError: 'modFzySet' object has no attribute 'newvar'
>

I'm thinking you must already have a method called loadParVar that
takes at least one argument.  It must have been called by the
Publisher, or I think python would have died when the Publisher called
it before self.newvar was ever referenced.

>From Werner's suggestion, initializing self.newvar to None would solve
the attribute error.  But I'm thinking whatever is using it must be
tolerant of the None value, or again something would have crashed
because of an illegal None type value.   I think the fact that python
doesn't crash just means that the original problem with getting your
listener called correctly is being masked.  If your widgets being
updated correctly is dependent on the value of "newvar", then then
they wouldn't be.

>   So I've missed something in your reply on implementing the function for
> the subscribed function. Is what I've missed visible in what I include
> above?

I think so, iff I am correctly interpreting what you are doing and how
your modules are scoped.  So  (1) Make sure you are calling the exact
method you specify in the subscribe, and (2) get your data from its
first argument as shown.

I hope this helps.  Or even better, I hope you've already solved it!

- Chris




More information about the wxpython-users mailing list