How do you add or remove children from a sizer

The order in which child widgets are added to the sizer is very important. This is different from the general case of adding children to a parent widget. The typical layout algorithm for a sizer takes each child one at a time in order to determine its place in the display. The placement of the next item is dependent on where the previous items have already gone. For example, the grid sizer moves left to right and top to bottom based on the order of the widgets. In most cases, when you create the sizer in the parent widget constructor, you will be able to add the items in the correct order. However, in some cases, you'll need more flexibility, particularly if you are dynamically changing your layout at runtime.

Using the Add() method

The most common method for adding a widget to a sizer is Add(), which appends the new widget to the end of the sizer's child list. The exact meaning of being at the end of the list depends on the sizer type, but in general it means that the new widget will display toward the lower right of the display. The Add() method has three distinct styles:

Add(window, proportion=0, flag=0, border=0, userData=None)

Add(sizer, proportion=0, flag=0, border=0, userData=None)

Add(size, proportion=0, flag=0, border=0, userData=None)

The first version is the one you will use most often, and it allows you to append a widget to the sizer. The second version is used to nest one sizer inside another— this is most commonly done with box sizers, but you can do it with any sizer type. The third version allows you to add an empty space the size of the wx.Size object or (width, height) tuple to the sizer, generally used as a separator (for example, in a toolbar). Again, this is most often used in box sizers, but can be used in any sizer to make an area of the window blank or to enforce a separation between the other widgets.

The other parameters affect how the item is displayed within the sizer. Some of these items are only valid for certain sizers. The proportion element is only used by box sizers, and affects how much an item is stretched when the parent window changes size. This will be discussed, along with box sizers, later in this chapter.

The flag option is a place to put any of a number of bit flags which control alignment, border, and resizing. Those options will be discussed in a later section. The border parameter contains the width of the border, if a border is specified in the flag option. The userData parameter can be used to pass extra data if needed by a sizer for its algorithm. You might use this if you were designing a custom sizer.

Using the Insert() method

As you might expect if you've read the Menus chapter (chapter 10), there are related methods for inserting the new widget at other places in the sizer. The Insert() method allows you to place the new widget at an arbitrary index. It also has three flavors:

Insert(index, window, proportion=0, flag=0, border=0, userData=None) Insert(index, sizer, proportion=0, flag=0, border=0, userData=None) Insert(index, size, proportion=0, flag=0, border=0, userData=None)

Using the Prepend() method

There is also a Prepend() method, which adds the new widget, sizer, or space at the beginning of the sizer list, meaning that it will tend to display toward the upper left:

Prepend(window, proportion=0, flag=0, border= Prepend(sizer, proportion=0, flag=0, border=0 Prepend(size, proportion=0, flag=0, border=0,

Figure 11.3 shows what the grid layout from listing 11.1 would look like if Prepend() had been used instead of Add().

If you add new items to the sizer after it has already been displayed on screen, you need to call the sizer method Layout() to force the sizer to rearrange itself to accommodate the new item.

Using the Detach() method

In order to remove an item from the sizer, you call the Detach() method, which removes the item from the sizer, but does not destroy it. This is especially useful if you want to hold on to the item you are removing for potential future use. There are three ways to use Detach(). You can pass it the window or sizer object that you want to detach, or you can pass it the integer index of the object:

0, userData=None) , userData=None) userData=None)

0, userData=None) , userData=None) userData=None)

Figure 11.3

A grid layout with items prepended

Figure 11.3

A grid layout with items prepended




In all three cases, the Detach() method returns a Boolean representing whether the item was actually removed—it will return false if you try to detach an item that is not in the sizer. Unlike some other removal methods we've seen, Detach() does not return the item being removed, so if you want to keep it around, you need to already have a variable referencing the object.

Removing the item from the sizer does not change the on-screen display automatically. You need to call the Layout() method to force a resize and redraw.

You can always get a reference to the sizer that a window is in by using the wx.Window method GetContainingSizer(). The method returns None if the widget is not contained by a sizer.

Was this article helpful?

0 0

Post a comment