Whats a static box sizer

A static box sizer combines a box sizer with a static box. The static box provides a nice-looking border and text label around the sizer. Figure 11.15 shows three static box sizers in action.

Tkinter Staticbox
Figure 11.15 Three static box sizers

Listing 11.10 shows the code used to create the static box sizers. There are two interesting things to notice here. The first is that you must create the static box object separately from the sizer, and the second is that this example shows how you might use nested box sizers. In this case, there are three vertical static box siz-ers placed inside a horizontal box sizer.

Listing 11.10 An example of a static box sizer import wx from blockwindow import BlockWindow labels = "one two three four five six seven eight nine".split()

class TestFrame(wx.Frame):

wx.Frame._init_(self, None, self.panel = wx.Panel(self)

-1, "StaticBoxSizer Test")

box1 = self.MakeStaticBoxSizer("Box 1", labels[0:3]) box2 = self.MakeStaticBoxSizer("Box 2", labels[3:6]) box3 = self.MakeStaticBoxSizer("Box 3", labels[6:9])

Make static boxes sizer = wx.BoxSizer(wx.HORIZONTAL) sizer.Add(box1, 0, wx.ALL, 10) sizer.Add(box2, 0, wx.ALL, 10) sizer.Add(box3, 0, wx.ALL, 10)

self.panel.SetSizer(sizer) sizer.Fit(self)

def MakeStaticBoxSizer(self, boxlabel, itemlabels) box = wx.StaticBox(self.panel, -1, boxlabel) sizer = wx.StaticBoxSizer(box, wx.VERTICAL)

Use sizer to manage others

Make static box for label in itemlabels: —i

I Add items bw = BlockWindow(self.panel, label=label) sizer.Add(bw, 0, wx.ALL, 2)

return sizer

TestFrame().Show()

app.MainLoop()

Static box sizers are instances of the class wx.StaticBoxSizer, which is a child class of wx.BoxSizer. The constructor takes the static box and the orientation:

wx.StaticBoxSizer(box, orient)

In this constructor orient has the same meaning as it does for an ordinary wx.BoxSizer, and the box parameter is a wx.StaticBox. There is only one other method defined for static box sizers, GetStaticBox(), which returns the wx.StaticBox used to build the sizer. You cannot change the static box once the sizer is created.

The wx.StaticBox class has a typical constructor for wxPython controls, but in practice, many of the parameters have useful defaults and can be ignored.

wx.StaticBox(parent, id, label, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, name="staticBox")

For use in a static box sizer, you do not need to set the pos, size, style, or name attributes, since the position and size will be managed by the sizer, and there are no unique style flags for a wx.StaticBox. This makes the constructor simpler:

box = wx.StaticBox(self.panel, -1, boxlabel)

Now that we've shown you what the various kinds of sizers are, we're going to show you how you might use them in a real layout. See chapter 6 for another example of sizers being used to create a complex layout.

Was this article helpful?

0 -2

Responses

  • evelyn
    How to change staticbox position wxpython?
    8 years ago

Post a comment