All of the different windows and controls in wxPython have a hierarchy of containment. Some controls can be containers for other controls and some cannot. This recipe is geared towards giving an understanding of this hierarchy.
We will be making just a minor change to the Frame
from the previous recipe, so let's open the code from that recipe to get ready for the new changes.
Here is the new code that will replace our existing Frame class.
class MyFrame(wx.Frame): def __init__(self, parent, id=wx.ID_ANY, title="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, name="MyFrame"): super(MyFrame, self).__init__(parent, id, title, pos, size, style, name) # Attributes self.panel = wx.Panel(self) self.panel.SetBackgroundColour(wx.BLACK) self.button = wx.Button(self.panel, label="Push Me", pos=(50, 50))
Basically, there are three general categories of window objects that are tiered, in the following containment order:
Top-Level Windows (Frames and Dialogs)
General Containers (Panels and Notebooks, …)
Controls (Buttons, CheckBoxes, ComboBoxes, …)
The Top-Level Window is at the top of the hierarchy and it can contain any kind of window except another Top-Level Window. General Containers come next, and they can arbitrarily hold any other General Container or Control. Finally, at the bottom of the Hierarchy are the Controls. These are the functional part of a UI that the user will interact with. They can, in some cases, be used to hold other controls, but typically will not. The containment hierarchy is connected to the parental hierarchy of controls. A parent will be the container for its children.
When running the previous sample, this hierarchy becomes apparent. The Frame
, as we have previously seen, is the outer-most container object; next you can see the Panel
, which we turned black to make it more visible; finally you can see the Button
, which was added as a child of the Panel
.