We've already seen numerous examples of frame creation in this book, but at the risk of repeating ourselves, we'll review the initial principles of frame creation.
Creating a simple frame
Frames are instances of the class wx.Frame. Listing 8.1 displays a very simple example of frame creation.
Listing 8.1 Basic wx.Frame creation import wx if __name__ == '__main__': app = wx.PySimpleApp()
frame = wx.Frame(None, -1, "A Frame", style=wx.DEFAULT_FRAME_STYLE, size=(2 00, 100)) frame.Show() app.MainLoop()
This creates a frame with the title A Frame, and a size of 200 by 100 pixels. The default frame style used in listing 8.1 provides the standard frame decorations like a close box and minimize and maximize boxes. Figure 8.1
displays the result. Figure 8.1
The simple frame
This constructor for wx.Frame is similar to the other widget constructors we saw in chapter 7.
wx.Frame(parent, id=-1, title="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, name="frame")
There are over a dozen style flags specific to wx.Frame, which we'll cover in the next section. The default style provides you with minimize and maximize boxes, the system pull-down menu, thick resizable borders, and a caption. This is suitable for most of your standard application window needs.
There are no event types tied to a wx.Frame beyond those that apply to any widget. However, since a wx.Frame is the one element on your screen that the user is most likely to close, you'll usually want to define a handler for the close event so that subwindows and data are properly managed.
You will rarely create wx.Frame instances directly. As we've seen in nearly every other example in this book, a typical wxPython application creates subclasses of wx.Frame and creates instances of those subclasses. This is because of the unique status of wx.Frame—although it defines very little behavior by itself, a subclass with a unique initializer is the most logical place to put information about the layout and behavior of your frame. Having to juggle your application-specific layouts and data without creating subclasses is possible, but is awkward in anything but the smallest application. Listing 8.2 displays an example of a wx.Frame subclass.
Listing 8.2 A simple frame subclass import wx class SubclassFrame(wx.Frame):
wx.Frame._init_(self, None, -1, 'Frame Subclass', size=(3 00, 100)) panel = wx.Panel(self, -1)
button = wx.Button(panel, -1, "Close Me", pos=(15, 15)) self.Bind(wx.EVT_BUTTON, self.OnCloseMe, button) self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
def OnCloseMe(self, event): self.Close(True)
def OnCloseWindow(self, event): self.Destroy()
app = wx.PySimpleApp() SubclassFrame().Show() app.MainLoop()
We've seen this basic structure in many other examples, so let's discuss some of the frame-specific aspects of this code. The call to the wx.Frame._init_ method has the same signature as the wx.Frame constructor. The constructor for the subclass itself has no arguments, which allows you as the programmer to define the arguments that get passed to the parent, and keeps you from having to specify the same arguments repeatedly.
Also noteworthy in listing 8.2, is that the subwidgets of the frame are themselves placed inside a panel. A panel is an instance of the class wx.Panel, and is a simple container for other widgets with little functionality of its own. You should almost always use a wx.Panel as the top-level subwidget of your frame. For one thing, the extra level can allow greater code reuse, as the same panel and layout could be used in more than one frame. Using a wx.Panel gives you some of the functionality of a dialog box within the frame. This functionality manifests itself in a couple of ways. One is simply that wx.Panel instances have a different default background color under MS Windows operating systems—white, instead of gray. Secondly, panels can have a default item that is automatically activated when the Enter key is pressed, and panels respond to keyboard events to tab through the items or select the default item in much the same way that a dialog does.
Was this article helpful?