Question on MVC and multiple wx.Panels

Hi David!

Hi all,
I have a question/problem with an application that i'm trying to refactor into an MVC model using ex.lib.pubsub.

Currently the application has a single frame with about 6 different wx.Panels. I have a "base" panel that shows the common widgets that I need for all panels.
Depending on widgets clicked I will show or hide an additional wx.Panel on top of the base panel.

The Controller in the program has a reference to the wx.Frame - but not to the panels that get overlayed onto the wx.Frame. I'm stuck on trying to figure out how I have the panels interact with the controller.

Here is an example:

class DataCollectorModel:
def __init__(self):
self.data = []
def add(self, value):
self.data.append(value)

class Controller:
def __init__(self, app):
self.model = DataCollectorModel()
self.view = MyFrame(parent=none, id=wx.ID_ANY)
self.view.Show()

class MyView(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,"My Frame")
self.CenterOnScreen()
Publisher.subscribe(self.watch, "data")
self.basepanel = BasePanel(self)
self.Fit()
self.panel.Show()
self.panel.Layout()
def watch(self,message):
print "called watch with message: %s" % message

class BasePanel(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self,parent,wx.ID_ANY):

def \_\_do\_\_layout\(self\):
\.\.\. add mainsizer, buttons, event handlers, etc\.\.\.

\#add second panel with other background fields
self\.newpanel = ItemPanel\(self\)

\.\.\. add second panel to mainsizer and display \.\.\.

# This overlayed panel will collect data inputted from the user. This is
# the panel where i'm having problems with. Specifically getting this data # either to or from the controller.

class ItemPanel(wx.Panel):
... display input fields, widgets, etc ...

Any help would be appreciated! Would it make sense to pass a reference to
the top level wx.Frame to all panels displayed on the BasePanel?

thanks!

DLP

What you probably want is a pubsub receiver in your controller.
Something like this:

<code>
from wx.lib.pubsub import Publisher

# subscribe to something here
Publisher().subscribe(self.__panelChangeListener, ("panel.changed"))

</code>

Then when you change panels, you can publish a message...

<code>
# send a message to the receiver. Note the first
# argument is the same as the receiver
# the second is the message.
Publisher().sendMessage("panel.changed", "Panel 1")
</code>

Then you just need a function to do something when the controller
receives a message.

<code>
def __panelChangeListener(self, msg):
    """"""
    panel = msg[0]
    if panel == "Panel 1":
        # do something
        pass
    elif panel == "Panel 2":
        # do something else
        pass
    else:
        # yada yada yada
</code>

Of course, it is very possible that I am completely misunderstanding
your question and if so, then feel free to disregard this. Here's some
docs though:

http://wiki.wxpython.org/PubSub

···

On Oct 8, 5:16 pm, David LePage <dwlep...@yahoo.com> wrote:

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org