V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
BruceWang
V2EX  ›  Python

对 wxPython wiki 上关于 MVC 的 demo 有些不理解的地方,还请指教

  •  
  •   BruceWang · 2015-12-31 11:50:55 +08:00 · 2926 次点击
    这是一个创建于 3250 天前的主题,其中的信息可能已经有所发展或是发生改变。

    wxPython 的 wiki 有一篇关于 MVC 设计模式的帖子,在这里: http://wiki.wxpython.org/ModelViewController

    其中的 Demo :

    #!/usr/bin/env python
    
    """
    An example of a Model-View-Controller architecture,
    using wx.lib.pubsub to handle proper updating of widget (View) values.
    """
    import wx
    from wx.lib.pubsub import Publisher as pub
    
    class Model:
        def __init__(self):
            self.myMoney = 0
    
        def addMoney(self, value):
            self.myMoney += value
            #now tell anyone who cares that the value has been changed
            pub.sendMessage("MONEY CHANGED", self.myMoney)
    
        def removeMoney(self, value):
            self.myMoney -= value
            #now tell anyone who cares that the value has been changed
            pub.sendMessage("MONEY CHANGED", self.myMoney)
    
    class View(wx.Frame):
        def __init__(self, parent):
            wx.Frame.__init__(self, parent, title="Main View")
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            text = wx.StaticText(self, label="My Money")
            ctrl = wx.TextCtrl(self)
            sizer.Add(text, 0, wx.EXPAND | wx.ALL)
            sizer.Add(ctrl, 0, wx.EXPAND | wx.ALL)
    
            self.moneyCtrl = ctrl
            ctrl.SetEditable(False)
            self.SetSizer(sizer)
    
        def SetMoney(self, money):
            self.moneyCtrl.SetValue(str(money))
    
    class ChangerWidget(wx.Frame):
        def __init__(self, parent):
            wx.Frame.__init__(self, parent, title="Main View")
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            self.add = wx.Button(self, label="Add Money")
            self.remove = wx.Button(self, label="Remove Money")
            sizer.Add(self.add, 0, wx.EXPAND | wx.ALL)
            sizer.Add(self.remove, 0, wx.EXPAND | wx.ALL)
            self.SetSizer(sizer)
    
    class Controller:
        def __init__(self, app):
            self.model = Model()
    
            #set up the first frame which displays the current Model value
            self.view1 = View(None)
            self.view1.SetMoney(self.model.myMoney)
    
            #set up the second frame which allows the user to modify the Model's value
            self.view2 = ChangerWidget(self.view1)
            self.view2.add.Bind(wx.EVT_BUTTON, self.AddMoney)
            self.view2.remove.Bind(wx.EVT_BUTTON, self.RemoveMoney)
            #subscribe to all "MONEY CHANGED" messages from the Model
            #to subscribe to ALL messages (topics), omit the second argument below
            pub.subscribe(self.MoneyChanged, "MONEY CHANGED")
    
            self.view1.Show()
            self.view2.Show()
    
        def AddMoney(self, evt):
            self.model.addMoney(10)
    
        def RemoveMoney(self, evt):
            self.model.removeMoney(10)
    
        def MoneyChanged(self, message):
            """
            This method is the handler for "MONEY CHANGED" messages,
            which pubsub will call as messages are sent from the model.
    
            We already know the topic is "MONEY CHANGED", but if we
            didn't, message.topic would tell us.
            """
            self.view1.SetMoney(message.data)
    
    if __name__ == "__main__":
        app = wx.App(False)
        controller = Controller(app)
        app.MainLoop()
    

    倒数第二行:
    controller = Controller(app)

    我不明白是什么原理?参数 app 看起来一点用处都没有啊。

    3 条回复    2015-12-31 17:39:04 +08:00
    TVBG
        1
    TVBG  
       2015-12-31 15:42:06 +08:00
    貌似是在当前代码中确实没有用,可能只是一个模板生成的代码而已.
    jarlyyn
        2
    jarlyyn  
       2015-12-31 16:01:42 +08:00
    我的理解是这样的。

    控制器中很可能需要用到整个 app 中的数据。

    而这个数据并不在当前文件中。

    比如站点名,站点配置。
    BruceWang
        3
    BruceWang  
    OP
       2015-12-31 17:39:04 +08:00
    我试过如果把:
    controller = Controller(app)

    写成:
    Controller(app)

    那么 MVC 的 C 就失效了。感觉被 Python 优化掉了一样。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1007 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 23:09 · PVG 07:09 · LAX 15:09 · JFK 18:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.