Marco De Felice wrote:
> C M ha scritto:
>> On Fri, Oct 9, 2009 at 3:55 PM, Mike Driscoll <kyoso...@gmail.com> wrote:
>>> On Oct 9, 2:31 pm, C M <cmpyt...@gmail.com> wrote:
>>>> On Fri, Oct 9, 2009 at 2:52 PM, David LePage <dwlep...@yahoo.com> wrote:
>>>>> Thanks guys, this has been helpful, and i'm glad i'm not the only one who finds this a difficult task..
>>>>> If you dont mind, I have one additional q, i'm hoping someone can point out my problem.. I can't seem to get my pubsub messages in the controller for some reason.. I put together a short sample program that illustrates this. Can someone find my error?
>>>>> class DataCollectorModel:
>>>>> def __init__(self):
>>>>> self.junk = [2500]
>>>>> def add(self, value):
>>>>> self.junk.append(value)
>>>>> print "Added: %s" % value
>>>>> class Controller:
>>>>> def __init__(self, app):
>>>>> Publisher().subscribe(self.__panelChangeListener, ("panel.changed"))
>>>>> self.model = DataCollectorModel()
>>>>> self.view = View(None)
>>>>> self.view.Show()
>>>>> def __panelChangeListener(self, message):
>>>>> print "*****Panel changed!"
>>>>> class View(wx.Frame):
>>>>> def __init__(self, parent):
>>>>> wx.Frame.__init__(self, parent, -1, 'My Application',
>>>>> style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX), size=(697,575))
>>>>> self.CenterOnScreen()
>>>>> self.basepanel = BasePanel(self)
>>>>> self.Fit()
>>>>> class BasePanel(wx.Panel):
>>>>> def __init__(self, parent):
>>>>> wx.Panel.__init__(self, parent, wx.ID_ANY)
>>>>> self.parent = parent
>>>>> self.__do_layout()
>>>>> def __do_layout(self):
>>>>> #top level vertical sizer
>>>>> vmainbox = wx.BoxSizer(wx.VERTICAL)
>>>>> next = wx.Button(self, wx.ID_ANY, "Next")
>>>>> vmainbox.Add(next, 0, wx.ALL)
>>>>> #Bind listener
>>>>> self.Bind(wx.EVT_BUTTON, self.OnNextClick, next)
>>>>> self.Fit()
>>>>> def OnNextClick(self, evt):
>>>>> Publisher().sendMessage("panel.changed", "Test")
>>>>> if __name__ =='__main__':
>>>>> app = wx.PySimpleApp()
>>>>> Controller(app)
>>>>> app.MainLoop()
>>>>> thanks!
>>>> Is it because your topic is "panel.changed" when you send the msg, but
>>>> ("panel.changed") when you subscribe?
>>> I noticed that too, but fixing it didn't solve the issue...I tried
>>> sticking the subscribe line and it's method into the wx.Frame instead,
>>> and it worked there...but I think that probably defeats the purpose.
>>> -------------------
>>> Mike Driscoll
>>> Blog: http://blog.pythonlibrary.org
>> I cannot see the problem even with having run this code (with the needed
>> imports). It's bugging me--can anyone explain why this doesn't work?
> I don't know the internals, but putting the subscription outside the
> Controller.__init__ works (perhaps some window is not completely
> initialized inside the Controller.__init__()???) like the following:
> class Controller:
> def __init__(self, app):
> self.model = DataCollectorModel()
> self.view = View(None)
> self.view.Show()
> def postinit(self):
> pubsub.Publisher().subscribe(self.__panelChangeListener,
> "panel.changed")
> if __name__ =='__main__':
> app = wx.PySimpleApp()
> c = Controller(app)
> c.postinit()
> app.MainLoop()
This really intrigued me.
Seeing Marcos solution pointed me in the right way.
At the end the only thing needed to be changed was.
Controller(app)
change to:
c = Controller(app)
I can only guess that if you don't keep a reference the controller is
initialized and immediately destroyed.
Werner