【问题标题】:Controller cant get messege from View PyPubsub控制器无法从 View PyPubsub 获取消息
【发布时间】:2021-04-05 15:13:32
【问题描述】:

您好,我正在尝试使用 wxPython 和 PyPubsub 编写一个简单的 MVC 应用程序。在我的视图文件中,我使用带有一个字段的简单窗口向控制器发送消息,在此字段中,我输入了新用户的名称,当我点击“创建”按钮时,它应该发送带有名称的消息,但控制器没有收到此消息.我做错了什么?

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

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__

【问题讨论】:

    标签: model-view-controller wxpython pypubsub


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢它实际上帮助我找出我的代码有什么问题:)
    猜你喜欢
    • 2011-02-18
    • 1970-01-01
    • 2019-09-01
    • 2020-09-13
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    相关资源
    最近更新 更多