Search code examples
model-view-controllerwxpythonshow-hidepublish-subscribe

in wxpython show and hide frames


I try to work with the mvc-model. I use pubsub to communicate between the different files. I use wxpython to build the gui of my program.

I open in my main file 2 frames: filter_view (main class of this file) and MA_view (I import this as a module):

    maview = MA_view.Main(None) 
    maview.Hide()

I hide MA_view, because I just need it to be open so I can use pubsub to communicate. Now, when a user wants to open MA_view in the menubar in filter_view, I don't want to open a new MA_view, I just want to show the hided frame.

The problem is that the menubar of the filter_view is also in a separate file so when I type

maview.Show()

of course it doesn't now maview because in this file it is not defined. But when I define it like this:

maview = MA_view.Main(None) 

It just opens a new frame.

I really don't like working with mvc (probably because my programming skills aren't good enough) but I'm working with other collegues, so I have to work this way.

tx in advance


Solution

  • MVC is super important so hang in there, you won't regret the extra learning curve. Here are the steps that use wx.lib.pubsub to support MVC on your app:

    1. In both files, add from wx.lib.pubsub import pub.
    2. In the MA_view module, make the Main.__init__ subscribe to a topic that will tell the Main instance to unhide itself, like pub.subscribe('unhide_ma', self.__unhide).
    3. In the filter_view file, send a message from menubar handler like pub.sendMessage('unhide_ma').
    4. In the Main.__unhide, you unhide the window.