遵循具有多个控制器的 MVC 规则

发布于 2024-12-05 10:01:31 字数 2388 浏览 2 评论 0原文

我对在 wxPython 上编写 Python 应用程序是否遵循 MVC 规则有点怀疑。

例子:

主窗体

<块引用>

PanelLst(ListCtrl + 一些按钮)

PanelTree(TreeCtrl + ProgressBar)

我创建了3个视图(ListView,TreeView,ButtonView) 我还创建了 3 个控制器(ListController、TreeController、ButtonController) 在主应用程序中,我有一个管理一些模型的对象(objManager)。

所有 3 个控制器都有一个指向 App.objManager() 的指针,用于执行一些操作。

例如:点击ListController中的一行执行App.objManager().foo1

点击ButtonController中的按钮执行App.objManager().bar1,ecc...

这里问题...我对那些“我的”结构有疑问...

  1. 在使用该结构的每个控制器中传递指向 objManager 的指针是否正确?
  2. 视图/控制器如何以正确的方式在其他视图/控制器之间进行通信? 实际上我使用类似模型的方式。使用wxPython publisher.sendMessage(EVENT, data)publisher.subscribe(EVENT, onEvent) 但我认为这不是最好的方法..

下一个疑问:

TreeController,接收来自模特的消息。必须在树中加载一个大列表,显示进度。 我已经使用了一些 wx.PostEvent 的线程,但是什么解决方案更好?

解决方案1:

ProgressBar()

# Callback via publisher.subscrive(self.onUpdateStatus, "UPDATE_STATUS")
def onUpdateStatus(self, msg)
    nValue = msg.data
    self.gauge.setValue(nValue)

TreeView()

def loadTree(self, items):
    for nItem, item in enumerate(items):
        tree.addItem(item)
        pub.sendMessage(UPDATE_STATUS, item)

TreeCTRL()

 # Callback function via publisher.subscribe(self.onLoadTree, "LOAD_TREE")
 def onLoadTree():
     items = self.objManager.getList()
     self.hTreeView.loadTree(items)

解决方案2

ProgressBar()

# Callback via publisher.subscrive(self.onUpdateStatus, "UPDATE_STATUS")
def onUpdateStatus(self, msg)
    nValue = msg.data
    self.gauge.setValue(nValue)

TreeView()

def loadTree(self, item):
    self.addTreeItem(item)

TreeCTRL()

 # Callback function via publisher.subscribe(self.onLoadTree, "LOAD_TREE")
 def onLoadTree():
     items = self.objManager.getList()
     for nItem, item in enumerate(items):
         self.hTreeView.loadTree(item)
         pub.sendMessage(UPDATE_STATUS, item)

什么解决方案更好?第一个解决方案是 VIEW 发送更新 GUI 的消息。第二个解决方案是发送消息的控制器。 不确定这两个解决方案是否是控制器/视图之间通信的最佳实践。

感谢并抱歉我的英语不好......

I've a little doubt for writing a Python application follow the MVC rules on wxPython.

Example:

MainForm

PanelLst (ListCtrl + Some buttons)

PanelTree (TreeCtrl + ProgressBar)

I created 3 views (ListView, TreeView, ButtonView)
I created also 3 controllers (ListController, TreeController, ButtonControllor)
In the main app, I have an object (objManager) that manage some models.

All 3 controllers have a pointer to the App.objManager() for executing some stuff.

Eg: click on a row in ListController execute App.objManager().foo1

click on a button in ButtonController execute App.objManager().bar1, ecc...

Here the question...I'm in doubt about those "my" structure...

  1. is correct pass a pointer to objManager in each controller that use that?
  2. how View/Controllers can communicate between others, in correct way?
    Actually I use a similar way like model. Using wxPython publisher.sendMessage(EVENT, data) and publisher.subscribe(EVENT, onEvent) but I think isn't the best way..

Next doubt:

TreeController, receive a message from a model. Must load a big list in the tree, showing progress.
I already user a thread with some wx.PostEvent, but what solution is better?

Solution 1:

ProgressBar()

# Callback via publisher.subscrive(self.onUpdateStatus, "UPDATE_STATUS")
def onUpdateStatus(self, msg)
    nValue = msg.data
    self.gauge.setValue(nValue)

TreeView()

def loadTree(self, items):
    for nItem, item in enumerate(items):
        tree.addItem(item)
        pub.sendMessage(UPDATE_STATUS, item)

TreeCTRL()

 # Callback function via publisher.subscribe(self.onLoadTree, "LOAD_TREE")
 def onLoadTree():
     items = self.objManager.getList()
     self.hTreeView.loadTree(items)

Solution 2

ProgressBar()

# Callback via publisher.subscrive(self.onUpdateStatus, "UPDATE_STATUS")
def onUpdateStatus(self, msg)
    nValue = msg.data
    self.gauge.setValue(nValue)

TreeView()

def loadTree(self, item):
    self.addTreeItem(item)

TreeCTRL()

 # Callback function via publisher.subscribe(self.onLoadTree, "LOAD_TREE")
 def onLoadTree():
     items = self.objManager.getList()
     for nItem, item in enumerate(items):
         self.hTreeView.loadTree(item)
         pub.sendMessage(UPDATE_STATUS, item)

What solution is better? In first solution, is the VIEW that send a message for updating the GUI. In second solution is the CONTROLLER that send a message..
Not sure that those 2 solution is anyway the best-practice for communicating between controller/view..

Thanks and sorry for my bad English...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带上头具痛哭 2024-12-12 10:01:31

我认为使用 pubsub 是模型和控制器之间进行通信的完全合法的方式。几个月前,我与另一位 wxPython 开发人员合作创建了一个示例应用程序,演示了实现此目的的一种方法。您可以在这里查看我们的代码:https://bitbucket.org/driscollis/medialocker

我希望对您有所帮助组织你的代码。当然,代码设计是一个非常个人和主观的话题。您需要使用您最熟悉且最了解的内容。

I think using pubsub is a perfectly legitimate way to communicate between the model and the controllers. A couple months ago, I worked with another wxPython developer to create an example application that demonstrates one way to do this. You can see our code here: https://bitbucket.org/driscollis/medialocker

I hope that helps you organize your code. Of course, code design is a very personal and subjective topic. You need to use what you're comfortable with and understand best.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文