Working with wxFrame

In wxPython parlance, a frame is the name given to what a GUI user normally calls a window. That is to say, a frame is a container that the user can generally move freely around on the screen, and which often includes such decorations as a title bar, menubar, and resize targets in the corners. The class wx.Frame is the parent class of all frames in wxPython. There are also a few specialized subclasses of wx.Frame that you may use. This section will give an overview of the wx.Frame family—enough for you to get started using them. A more complete description of the wx.Frame class will be presented in chapter 8.

When you create subclasses of wx.Frame, the_init_() method of your class should call the parent constructor wx.Frame._init_(). The signature of that constructor is as follows.

wx.Frame(parent, id=-1, title="", pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE, name="frame")

This constructor takes several parameters. In normal use, however, at least some of the defaults are reasonable options. We will see parameters similar to this constructor again and again in other widget constructors—it's a very similar pattern in wxPython. Table 2.1 describes each of the parameters.

Remember, these are the parameters as passed to the parent constructor method, wx.Frame._init_(). The argument signature of the constructor to your

Table 2.1 Parameters of the wx.Frame constructor method

Parameter

Description

parent

The parent window of the frame being created. For top-level windows, the value is None. If another window is used for the parent parameter then the new frame will be owned by that window and will be destroyed when the parent is. Depending on the platform, the new frame may be constrained to only appear on top of the parent window. In the case of a child MDI window, the new window is restricted and can only be moved and resized within the parent.

id

The wxPython ID number for the new window. You can pass one in explicitly, or pass -1 which causes wxPython to automatically generate a new ID. See the section "Working with wxPython ID" for more information.

title

The window title—for most styles, it's displayed in the window title bar.

pos

A wx.Point object specifying where on the screen the upper left-hand corner of the new window should be. As is typical in graphics applications, the (0, 0) point is the upper left corner of the monitor. The default is (-1, -1), which causes the underlying system to decide where the window goes. See the section "Working with wx.Size and wx.Point" for more information.

size

A wx.Size object specifying the starting size of the window. The default is (-1, -1), which causes the underlying system to determine the starting size. See the section "Working with wx.Size and wx.Point" for more information.

style

A bitmask of constants determining the style of the window. You may use the bitwise or operator (|) to combine them when you want more than one to be in effect. See the section "Working with wx.Frame styles" for usage guidelines.

name

An internal name given to the frame, used on Motif to set resource values. Can also be used to find the window by name later.

class can, and often will, be different. This allows you to conveniently ensure default values for your own frame by not allowing them to be modified by a call to your constructor. For example, you might want your frame class to always be a 300 pixel square. In that case, you probably wouldn't have a size argument in your class constructor, but would just explicitly pass (300, 300) to the wx.Frame.__

init_() method. Listing 2.2 shows a frame class that does not allow any of the attributes of the window to be passed as an argument.

Listing 2.2 A frame subclass which sets its own defaults class MyFrame(wx.Frame):

wx.Frame._init_(self, None, -1, "My Friendly Window",

In listing 2.2, the__init__() method of MyFrame does not take any arguments.

This means that MyFrame users cannot override the hardwired arguments that

MyFrame.__init__() passes to the superclass wx.Frame.__init__(). Remember that a determined user of your class can always change the default values by calling setter methods after the frame is instantiated.

Was this article helpful?

0 -1

Responses

  • abel
    How to pass parent parameter to frame wxpython?
    8 years ago

Post a comment