控制器无法从 View PyPubsub 获取消息

Controller cant get messege from View PyPubsub

您好,我正在尝试使用 wxPython 和 PyPubsub 编写一个简单的 MVC 应用程序。在我的视图文件中,我使用带有一个字段的 simle window 向 Controller 发送消息,在这个字段中我输入了一个新用户的名称,当我点击按钮 'Create' 时,它应该发送带有名称的消息,但是 Controller没有收到此消息。我做错了什么?

这是我的视图文件的一部分:

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()

这是我的控制器:

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__

虽然不是确定的答案,但作为通用调试器,它可能会有所帮助。

pubsub 有一个名为 pub.ALL_TOPICS 的专题。订阅此主题的侦听器将收到每个主题的所有消息。

>>> 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'}
>>>