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…
- is correct pass a pointer to objManager in each controller that use that?
- 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…
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.