Working with wxFrame styles

The wx.Frame constructor takes a bitmask as a style parameter. Every wxPython widget object takes a similar style parameter, although the exact values that are defined are different for each type of widget. This section will discuss the styles used for wx.Frame. At least some of this is applicable to other wxPython widgets. The widget definitions in part 2 will discuss styles applicable to each class.

WHAT'S A A bitmask is a way of compactly storing information about system

BITMASK? J

attributes that is especially useful when there are a limited number of attributes with boolean values and the values are more or less mutually independent. In wxPython, bitmasks are used to manage a number of different attributes throughout the framework, most notably style information.

In a bitmask, the individual attributes are assigned constant values corresponding to powers of two, and the value of the bitmask is the sum of all the attributes which are "turned on". In binary notation, the power of two system guarantees that each attribute corresponds to a single bit in the total sum, allowing all of the attribute state to be compactly stored in a single integer or long value. For example, if attribute a=1, b=2, c=4, and d=8, then any combination of the group has a unique sum that can be stored in an integer. The pair a and c would be 5 (binary 0101), while b, c, and d would be 14 (binary 1110). In wxPython, the attributes have symbolic constants, so you don't need to worry about the individual bit values.

Styles are defined for all wxPython widgets by passing a bitmask to the style parameter of the constructor. Some widgets also define a SetStyle() method, allowing you to change the style after the widget is created. All the individual style elements that you might use have a predefined constant identifier (such as wx.minimize_box). To add multiple styles together, you use the Python bitwise OR operator, |. For example, the constant wx.default_frame_style is defined as a combination of basic style elements:

wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX | wx.RESIZE_BORDER | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX

To remove individual style bits from a composed style, you use the bitwise exclusive or (XOR) operator, For example, to create a window that is based on the default, but which is not resizable by the user, you could do this:

wx.DEFAULT_FRAME_STYLE * (wx.RESIZE_BORDER | wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX)

It is highly recommended that you use the default style on top-level frames so that the user can easily recognize them as being top level. At the very least, you need to ensure that there is some way for a top-level frame to be closed. This is most easily done by including the wx.SYSTEM_MENU style. Also be aware that by inadvertently using a bitwise AND (&) operation, instead of a bitwise OR, you can easily wind up with no styles chosen, resulting in an unbordered frame that you cannot move, resize, or close. This is, of course, not recommended. Table 2.2 has a listing of the most important styles for wx.Frame.

Table 2.2 Some of the most commonly used style parameters for wx.Frame

Style

Description

wx.CAPTION

Adds a title bar on the frame, which displays the frame's Title property.

wx.CLOSE_BOX

Instructs the system to display a close box on the frame's title bar, using the system defaults for placement and style. Also enables the close item on the system menu if applicable.

continued on next page continued on next page

Table 2.2 Some of the most commonly used style parameters for wx.Frame (continued)

Style

Description

wx.DEFAULT_FRAME_STYLE

As you might expect from the name, this is the default if no style is specified. It is defined as wx.maximize_box | wx.minimize_box

| wx.RESIZE_BORDER | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX.

wx.FRAME_SHAPED

Frames created with this style can use the SetShape() method to create a window with a non-rectangular shape.

wx.FRAM E_TOOL_WINDOW

Makes the frame look like a toolbox window by giving it a smaller titlebar than normal. Under Windows a frame created with this style does not show in the taskbar listing of all open windows.

wx.MAXIMIZE_BOX

Adds a maximize box on the frame, using the system parameters for the look and placement of the box. Also enables maximize functionality in the system menu if applicable.

wx.MINIMIZE_BOX

Adds a minimize box on the frame, using the system parameters for the look and placement of the box. Also enables minimize functionality in the system menu if applicable.

wx.RESIZE_BORDER

Adds a resizable border to the frame.

wx.SIMPLE_BORDER

A plain border without decoration. May not work on all platforms.

wx.SYSTEM_MENU

Adds the system menu (with close, move, resize, etc. functionality, using system look and feel) and the close box to the window. The availability of resize and close operations within this menu depends on the styles wx.maximize_box, wx.minimize_box and wx.close box being chosen.

The next four figures show a few common frame styles.

Figure 2.4 was created with wx.default_style. Figure 2.5 is a frame created using the non-resizable style combination shown in the previous code snippet.

Figure 2.4 A frame created with Figure 2.5 A frame created to the default style be non-resizable. Notice the lack of minimize/maximize buttons.

Figure 2.4 A frame created with Figure 2.5 A frame created to the default style be non-resizable. Notice the lack of minimize/maximize buttons.

Figure 2.6 uses style=wx.DEFAULT_FRAME_STYLE | wx.FRAME_TOOL_WINDOW.

Figure 2.6 A toolbar frame, with a smaller title Figure 2.7 A frame bar and no system menu with a help button

Figure 2.6 A toolbar frame, with a smaller title Figure 2.7 A frame bar and no system menu with a help button

Figure 2.7 uses the extended style wx.help.FRAME_EX_CONTEXTHELP, which is described in chapter 8.

Now that we've seen how to create wx.Frame objects, we'll start to show how to make them useful, by adding additional widgets inside the frame.

Was this article helpful?

0 -1

Post a comment