Search code examples
model-view-controllerwxpythonpypubsub

Controller cant get messege from View PyPubsub


Hi i trying to write a simple MVC application with wxPython and PyPubsub. In my View file I send message to Controller using simle window with one filed, in this field i put name of the a new user, when i hit button 'Create' it should send messege with name, but Controller doesn't get this message. What I'm do wrong ?

This is part of my View file:

class AddNewOperator(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Add new record', size=(250, 150))
        wx.Frame.CenterOnScreen(self)
        #self.controller = controller
        self.InitUI()
        self.Centre(

    def InitUI(self):

        panel = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        st1 = wx.StaticText(panel, label='New user name')
        hbox1.Add(st1, flag=wx.RIGHT, border=8)
        self.tc = wx.TextCtrl(panel)
        hbox1.Add(self.tc, proportion=1)
        vbox.Add(hbox1, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        vbox.Add(-1, 10)
        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
        btn1 = wx.Button(panel, label='Create', size=(70,30))
        hbox2.Add(btn1)
        btn2 = wx.Button(panel, label='Cancel', size=(70,30))
        hbox2.Add(btn2, flag=wx.LEFT|wx.BOTTOM, border=5)
        vbox.Add(hbox2, flag=wx.ALIGN_CENTER|wx.RIGHT, border=10)
        panel.SetSizer(vbox)

        self.Bind(wx.EVT_BUTTON, self.OnCreate, btn1, id=ID_ADD_NEW_USER)
        self.Bind(wx.EVT_BUTTON, self.OnDestroyAddNewUser, btn2,id=ID_DESTROY_WINDOW)

    def OnCreate(self, e):
        name = self.tc.GetValue()
        pub.sendMessage('send name', message=name, listener='send name')
        print(name)
    def OnDestroyAddNewUser(self, e):
        self.Destroy()
if __name__ == '__main__':
    MainWindow.main()

And this is my Controller:

from View import MainWindow,AddNewOperator
from Model import Model
from pubsub import pub
from pubsub.utils.notification import useNotifyByWriteFile
import sys
useNotifyByWriteFile(sys.stdout)

class ApplicationController:
    def __init__(self):
        self.main_window_view = MainWindow(self)
        #self.add_new_user_view = AddNewOperator(self)
        self.model = Model()
        pub.subscribe(self.on_button_add_new_user_click, topicName='send name')

    def on_button_add_new_user_click(self, message):
        if message is not None:
            self.model.add_new_operator(message)
        else:
            print('Somthing goes wrong')




    def main(self):
        self.main_window_view.main()

if __name__ == '__main__':
    MainWindow.main()
    sys.stdout = sys.__stdout__


Solution

  • Whilst not a definitive answer, perhaps it will help, as a generic debugger.

    pubsub has a special topic named pub.ALL_TOPICS. A listener that subscribes to this topic will receives all messages of every topic.

    >>> from pubsub import pub
    >>> def snoop(topicObj=pub.AUTO_TOPIC, **mesgData):
    ...     print ('topic "%s": %s' % (topicObj.getName(), mesgData))
    ... 
    >>> 
    >>> pub.subscribe(snoop, pub.ALL_TOPICS)
    (<pubsub.core.listener.Listener object at 0x7f5cd7804550>, True)
    >>> 
    >>> pub.sendMessage('a random message', message="Rolf woz here", listener='send name')
    topic "a random message": {'message': 'Rolf woz here', 'listener': 'send name'}
    >>> 
    >>> pub.sendMessage('another random message', message="Rolf didn't get this far", listener='any listener')
    topic "another random message": {'message': "Rolf didn't get this far", 'listener': 'any listener'}
    >>>