can set Label of button, but cannot Enable it. Using pubsub.

Complete code sample:: http://pastebin.com/767zTktb

My function onCompleted gets called when a pubsub message is received.
It can set the label of a button on my Panel, but cannot enable this
button. I also cannot change the label of a StaticText.

What am I doing wrong??

wxPython version: 2.8.10.1

SNIPPET:

···

##########################################################################
class DemoPanel(wx.Panel):
    def __init__(self, parent, *args, **kwargs):
        wx.Panel.__init__(self, parent, *args, **kwargs)
        self.parent = parent # Sometimes one can use inline Comments

        # create a pubsub receiver
        pub.subscribe(self.onCompleted, 'completed')

        self.newValueBtn = wx.Button(self,id=-1, label="Request new
value")
        self.Bind(wx.EVT_BUTTON, self.OnBtn, self.newValueBtn )
       #self.newValueBtn.Bind(wx.EVT_BUTTON, self.OnBtn)
        self.displayLbl = wx.StaticText(self,id=-1, label="Value goes
here")
        Sizer = wx.BoxSizer(wx.VERTICAL)
        Sizer.Add(self.newValueBtn, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        Sizer.Add(self.displayLbl, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizerAndFit(Sizer)

    def onCompleted(self, message):
     # data passed with your message is put in message.data.
        print 'pubsub', message.data, "this works" # so the function
is executed
        self.newValueBtn.Enable() # but this doesn't work
        self.newValueBtn.SetLabel("This Works")
        self.displayLbl.SetLabel("This doesn't work")
##########################################################################

Hi,

Complete code sample:: http://pastebin.com/767zTktb

My function onCompleted gets called when a pubsub message is received.
It can set the label of a button on my Panel, but cannot enable this
button. I also cannot change the label of a StaticText.

What am I doing wrong??

<snip>

def onCompleted(self, message):
# data passed with your message is put in message.data.
print 'pubsub', message.data, "this works" # so the function
is executed
self.newValueBtn.Enable() # but this doesn't work
self.newValueBtn.SetLabel("This Works")
self.displayLbl.SetLabel("This doesn't work")

Try and call:

self.newValueBtn.Refresh()
self.displayLbl.Refresh()

(or simply DemoPanel.Refresh(), if the content of your pane is not
expensive to repaint), after you have made your changes (i.e., at the
end of the onCompleted method).

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

···

On 7 April 2012 00:14, conjac wrote:

conjac wrote:

Complete code sample:: http://pastebin.com/767zTktb

My function onCompleted gets called when a pubsub message is received.
It can set the label of a button on my Panel, but cannot enable this
button. I also cannot change the label of a StaticText.

Are you using the wx version (wx.pubsub)?

Do things change if you call self.Refresh() after your updates?

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Thanks for the replies (Andrea Gavana and Tim Roberts).

I found the problem: Pubsub publishes the event too fast.

When the button is clicked, I programmed:
1. send message to pubsub
2. set label
3. disable button

(assuming the method would complete before the pubsub event got
processed)

when the "button-is-clicked" message is received, another pubsub
message is sent back to the panel to start onCompleted:
4.set label
5 enable button again

But what happened is a little bit different:
1 -> 4 -> 5 -> 2 -> 3

I also don't have to Refresh() anything. Sweet! :slight_smile:

Hi,

You seem to have solved your problem.

If you just start with pubsub you might want to consider to use the newer protocol, i.e. the 'kwargs" messaging protocol and if you have a larger application define the topics in a topic tree.

A topic tree you would define like this:

myTopics.py:
class statusText:
     """a status text needs to be shown

     :param string msg: the msg to be shown
     :param flag: wx.ICON_something, it is assumed that the listing handler
         will default to wx.ICON_INFORMATION, depending on where the statusText
         is shown it could also ignore this flag (e.g. when using wx.StatusText)
     """

     def msgDataSpec(msg, flag=None):
         """params doc defined on class above"""

Usage:
import wx.lib.pubsub.setupkwargs # only needed if you are useing < 2.9.3 wxPython
from wx.lib.pubsub import pub
import myTopics
pub.importTopicTree(pTopics, format=pub.TOPIC_TREE_FROM_CLASS) # enforces that topics are defined

pub.subscribe(listenerTest, pTopics.statusText)
pub.sendMessage(pTopics.statusText, msg='message for statusText',
                                             flag='something')

This way you get code completion on topics and don't have to remember the spelling etc you use for your topics and it allows you to enforce what needs to be passed, like in the above setup the "flag" parameter is optional.

You can find more details in this in the pubsub documentation and the examples.

Werner

Thank you for the tips, Werner.

- I update my wxPython (see procedure below) to use the new mechanism
- Will check out the topics as well. They look usefull.

Squeeze comes with an older version of wxPython which doesn't support
the new pubsub.
I update my wxPython using: InstallingOnUbuntuOrDebian - wxPyWiki.
Next i modified my import statements according to
http://pubsub.sourceforge.net/recipes/upgrade_v1tov3.html.
So it now looks like this:

···

--------------------------------------------------------------------------------
#The new pubsub mechanism requires wxPython 2.8.11 or greater (which
is not in squeeze)
#Enable these lines for pubsub v1.x / disable for pusub >= 3.0
#from wx.lib.pubsub import setupv1
#from wx.lib.pubsub import Publisher as pub
#Disable these lines for pubsub v1.x / enable for pusub >= 3.0
from wx.lib.pubsub import setuparg1
from wx.lib.pubsub import pub
--------------------------------------------------------------------------------

This way I use pusbsubversion:

from wx.lib.pubsub import setuparg1
from wx.lib.pubsub import pub
pub.VERSION_STR

'3.1.1b1.201005.r243'

If you just start with pubsub you might want to consider to use the
newer protocol, i.e. the 'kwargs" messaging protocol and if you have a
larger application define the topics in a topic tree.

A topic tree you would define like this:

myTopics.py:
class statusText:
"""a status text needs to be shown

 :param string msg: the msg to be shown
 :param flag: wx\.ICON\_something, it is assumed that the listing handler
     will default to wx\.ICON\_INFORMATION, depending on where the

statusText
is shown it could also ignore this flag (e.g. when using
wx.StatusText)
"""

 def msgDataSpec\(msg, flag=None\):
     &quot;&quot;&quot;params doc defined on class above&quot;&quot;&quot;

Usage:
import wx.lib.pubsub.setupkwargs # only needed if you are useing < 2.9.3
wxPython
from wx.lib.pubsub import pub
import myTopics
pub.importTopicTree(pTopics, format=pub.TOPIC_TREE_FROM_CLASS) #
enforces that topics are defined

Werner

I can not get pub.importTopicTree() working. Here is the code I try to
run:

http://pastebin.com/vhaDSQm2

Hi,

...

I can not get pub.importTopicTree() working. Here is the code I try to run: http://pastebin.com/vhaDSQm2

Hhm, looks like you don't have the latest version of pubsub.

The copy of pubsub I use came from the pubsub SVN, so you can get it from Pypubsub - Browse Files at SourceForge.net and just replace the wx.lib.pubsub (it is all Python so doesn't need any install).

I believe that wxPython 2.9.3 should have the same as what is in the wxPython and the pubsub SVN repos.

Attached works for me, some small changes compared to yours.

Which gives me this as output:

3.1.1b1.201005.r243 - which is probably wrong and probably due to me using a SVN copy of pubsub, waiting for 2.9.4 to get this all cleaned up on my machine.
'message for statusText'
'something'

If I run it against my Py 2.6 with an older pubsub I get the exception you get.

Sorry for all this confusion.
Werner

pubsubtest.py (517 Bytes)

topics.py (458 Bytes)

···

On 07/04/2012 15:06, conjac wrote:

The copy of pubsub I use came from the pubsub SVN, so you can get it
fromhttp://sourceforge.net/projects/pubsub/files/pubsub/and just
replace the wx.lib.pubsub (it is all Python so doesn't need any install).

I try to use my package manager as much as I can, so I'm a bit scared
of going this route:
- How do you deal with dependencies?
- Do you distribute all pubsub code along with your programs?

If I run it against my Py 2.6 with an older pubsub I get the exception
you get.

For now I changed the following to be future-proof:
1. disable the import statements:
#pub.importTopicTree(topics, format=pub.TOPIC_TREE_FROM_CLASS)
#pub.setTopicUnspecifiedFatal()

2. redefine the Class as a string at the end of the topics.py module:
statusText = "statusText"

This way I can use the same syntax in pubsubtest.py.

Hi,

The copy of pubsub I use came from the pubsub SVN, so you can get it
fromhttp://sourceforge.net/projects/pubsub/files/pubsub/and just
replace the wx.lib.pubsub (it is all Python so doesn't need any install).

I try to use my package manager as much as I can, so I'm a bit scared
of going this route:

Did you try it with wxPython 2.9.3 with the install steps you did the other day?

- How do you deal with dependencies?
- Do you distribute all pubsub code along with your programs?

I only distribute with py2exe at this point. Whenever I get around to do a *nix/Mac version I would probably use py2app or cxfreeze or something along these lines as I don't think one can depend on having the correct version on a users machine.

If I run it against my Py 2.6 with an older pubsub I get the exception
you get.

For now I changed the following to be future-proof:
1. disable the import statements:
#pub.importTopicTree(topics, format=pub.TOPIC_TREE_FROM_CLASS)
#pub.setTopicUnspecifiedFatal()

2. redefine the Class as a string at the end of the topics.py module:
statusText = "statusText"

This way I can use the same syntax in pubsubtest.py.

Wouldn't you have to comment 2 if you uncomment 1? Maybe do both of these things based on what version of pubsub is loaded.

if pub.VERSION_STR > '3.1.1b1.201005.r243':
     do importtopic
else:
    do statusText = "statusText"

Or something along those lines.

Werner

···

On 07/04/2012 17:02, conjac wrote:

Z

···

On Apr 6, 2012 6:36 PM, “conjac” jancostermans@gmail.com wrote:

Complete code sample:: http://pastebin.com/767zTktb

My function onCompleted gets called when a pubsub message is received.
It can set the label of a button on my Panel, but cannot enable this
button. I also cannot change the label of a StaticText.

What am I doing wrong??

wxPython version: 2.8.10.1

SNIPPET:
##########################################################################
class DemoPanel(wx.Panel):
def init(self, parent, *args, **kwargs):
wx.Panel.init(self, parent, *args, **kwargs)
self.parent = parent # Sometimes one can use inline Comments

   # create a pubsub receiver
   pub.subscribe(self.onCompleted, 'completed')

   self.newValueBtn = wx.Button(self,id=-1, label="Request new

value")
self.Bind(wx.EVT_BUTTON, self.OnBtn, self.newValueBtn )
#self.newValueBtn.Bind(wx.EVT_BUTTON, self.OnBtn)
self.displayLbl = wx.StaticText(self,id=-1, label=“Value goes
here”)
Sizer = wx.BoxSizer(wx.VERTICAL)
Sizer.Add(self.newValueBtn, 0, wx.ALIGN_CENTER|wx.ALL, 5)
Sizer.Add(self.displayLbl, 0, wx.ALL|wx.CENTER, 5)
self.SetSizerAndFit(Sizer)

def onCompleted(self, message):
# data passed with your message is put in message.data.
print ‘pubsub’, message.data, “this works” # so the function
is executed
self.newValueBtn.Enable() # but this doesn’t work
self.newValueBtn.SetLabel(“This Works”)
self.displayLbl.SetLabel(“This doesn’t work”)
##########################################################################


To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=enju