title: python - wx.lib.pubsub.Publisher not working when called from buttons pressed?
body: <p>I'm implementing a <a href="http://wiki.wxpython.org/ModelViewController" rel="nofollow">MVC</a> app using <a href="http://www.wxpython.org/" rel="nofollow">wxPyton</a>, and I have an issue with <a href="http://wxpython.org/docs/api/wx.lib.pubsub.PublisherClass-class.html" rel="nofollow">wx.lib.pubsub.Publisher</a>.</p>
  
  <p>I'd like to keep my Controller as separated from my View as possible. For example, I don't want my controller to 'know' the names of the buttons used in the view. On the <a href="http://wiki.wxpython.org/ModelViewController" rel="nofollow">wxPython example</a>, the controller has to know them, so it can bind actions to them.</p>
  
  <pre><code>class Controller:
  ...
  self.view2.add.Bind(wx.EVT_BUTTON, self.AddMoney)
  self.view2.remove.Bind(wx.EVT_BUTTON, self.RemoveMoney)
  </code></pre>
  
  <p>I thought about 'decoupling' things a bit more, using <code>wx.lib.pubsub.Publisher</code> messages instead:</p>
  
  <pre><code>from wx.lib.pubsub import Publisher as pub
  
  class Controller:
  ...
  pub.subscribe(self.AddMoney, "MONEY ADDED")
  pub.subscribe(self.RemoveMoney, "MONEY REMOVED")
  </code></pre>
  
  <p>The view would bind the controls to an action that would just send the messages.</p>
  
  <p>But alas, it doesn't seem to work. Either the view refuses to send events, or the controller refuses to receive messages from the view. I'm not sure.</p>
  
  <p>Here's a small sample app that illustrates my problem (I've tested it with python 2.6.6 and wxPython 2.8)</p>
  
  <pre><code>import wxversion
  wxversion.ensureMinimal('2.8')
  import wx
  from wx.lib.pubsub import Publisher as pub
  
  class View(wx.Frame):
      def __init__(self):
          wx.Frame.__init__(self, None, title="Main View")
  
          self.button = wx.Button(self, -1, "foo?")
          self.button.Bind(wx.EVT_BUTTON, self.ChangeFoo)
  
      def ChangeFoo(self, evt):
          print('Binding works')
          pub.sendMessage("FOO CHANGED")
  
  class Controller:
      def __init__(self, app):
          self.view = View()
  
          pub.subscribe(self.FooChanged, "FOO CHANGED")
  
          self.view.Show()
  
      def FooChanged(self, message):
          print('Messaging works')
  
  
  app = wx.App(False)
  Controller(app)
  app.MainLoop()
  </code></pre>
  
  <p>The app shows a window with a big button. The expected output after pressing it is:</p>
  
  <pre><code>Binding works
  Messaging works
  </code></pre>
  
  <p>But instead I get:</p>
  
  <pre><code>Binding works
  </code></pre>
  
  <p>Does anyone know how to solve this?</p>