How do sizers manage the border around each child

A wxPython sizer can place a border around any or all of its children. The border is a consistent amount of empty space separating the widget from its neighbors. The size of the border is taken into account when the sizer calculates the placement of its children—the child is not made smaller to accommodate the width of the border. The size of the border does not change when the sizer resizes.

Figure 11.6 shows a 10-pixel border placed around all or part of the widgets in our basic grid layout. In each row, the middle element has the border around all four sides, while the other widgets have a border around only some sides. Adding the border does not make the widgets smaller; rather, it makes the frame larger.

Listing 11.5 has the code relevant to figure 11.6. Again, it's similar to the basic grid sizer, but now we've added a dictionary of border values, and a 10-pixel border argument to the Add() statement.

Figure 11.6 The grid sizer with a border

Listing 11.5 The grid sizer code with borders import wx from blockwindow import BlockWindow labels = "one two three four five six seven eight nine".split() flags = {"one": wx.BOTTOM, "two": wx.ALL, "three": wx.TOP,

"four": wx.LEFT, "five": wx.ALL, "six": wx.RIGHT, Border

"seven": wx.BOTTOM | wx.TOP, "eight": wx.ALL, flags

class TestFrame(wx.Frame):

wx.Frame._init_(self, None, -1, "GridSizer Borders")

sizer = wx.GridSizer(rows=3, cols=3, hgap=5, vgap=5) for label in labels:

bw = BlockWindow(self, label=label) flag = flags.get(label, 0) sizer.Add(bw, 0, flag, 10) self.SetSizer(sizer) self.Fit()

Adding widgets with border specified app = wx.PySimpleApp()

TestFrame().ShowO

app.MainLoop()

Putting a border around a widget inside a sizer is a two-step process. The first step is to pass additional flags to the flags parameter when the widget is added to the sizer. You can specify a border around the entire widget using the flag wx.ALL, or you can border on a specific side using wx.BOTTOM, wx.LEFT, wx.RIGHT, or wx.TOP. Naturally, the flags can be combined for any set of border sides you would like, such as wx.RIGHT | wx.BOTTOM which would give you a border on the right and bottom sides of the widget. Since border, sizing, and alignment information is all sent via the same flags parameter, you will often have to use the bitwise or operation to combine border information with sizing and alignment information for the same widget.

After you send the border information to the flags parameter, you also need to pass the width of the border, in pixels, to the border parameter. For example, the following call will add the widget to the end of the sizer list, placing a five pixel border around the whole thing:

The widget will then expand to fill its available space, always leaving five blank pixels around it.

Was this article helpful?

0 0

Post a comment