Mike
Mike

Reputation: 2737

How do I move wxPython controls to another frame

My main wxPython application wx.Frame is presently full of buttons and text fields as well as the main application display area. All of the event handlers and UI code is contained in the MainFrame class which passes commands to a Control class.

I would like to move some of these controls from the MainFrame to a new SecondaryFrame that can be hidden. I create an instance of SecondaryFrame and then use a menu item to Show and Hide it.

I seem to have a couple of choices for implementation:

  1. Move all of the control create and handler code into the SecondaryFrame class.
  2. Create a blank SecondaryFrame and populate it from within MainFrame leaving all of the handlers in place and making the controls members of MainFrame rather than SecondaryFrame
  3. A combination of the two

Is seems like all of these options with require a significant refactoring of my code. Am I missing a more obvious way to move already functional controls to a new frame?

Upvotes: 1

Views: 216

Answers (1)

Mike Driscoll
Mike Driscoll

Reputation: 33071

Each frame will need to have its own widgets. If you want both frames to have the same controls for some reason, then I would create a base class frame and have the other two inherit from it.

The handler code itself could be kept in a controller module. Then you would import the controller and do something like this:

mySecondaryFramesBtn.Bind(wx.EVT_BUTTON, controller.btnHandler)

That way you can bind the button in the first frame and the second frame to the same handler, so to speak.

You might also look into the idea of model-view-controller. You can read more about it at the following links:

Upvotes: 1

Related Questions