A checkbox is a toggle button with a text label. Checkboxes are often displayed in groups, but the toggle state of each checkbox is independent. Checkboxes are used when you have one or more options that have clear on/off states, and the state of one option doesn't affect the state of the others. Figure 7.10 displays a group of checkboxes.
Checkboxes are easy to use in wxPython. They are instances of the class wx.CheckBox, and can be displayed together by placing them inside the parent container together. Listing 7.10 provides the code that generated figure 7.10.
Listing 7.10 Inserting three checkboxes into a frame import wx class CheckBoxFrame(wx.Frame):
wx.Frame._init_(self, None, -1, 'Checkbox Example', size=(15 0, 200)) panel = wx.Panel(self, -1)
Figure 7.10 A group of wxPython checkboxes wx.CheckBox(panel, -1, "Alpha", (35, 40), (150, 20)) wx.CheckBox(panel, -1, "Beta", (35, 60), (150, 20)) wx.CheckBox(panel, -1, "Gamma", (35, 80), (150, 20))
app = wx.PySimpleApp() CheckBoxFrame().Show() app.MainLoop()
The wx.CheckBox class has a typical wxPython constructor:
wx.CheckBox(parent, id, label, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, name="checkBox")
The label argument takes the text that is displayed next to the checkbox. Checkboxes have no style flags which are unique to them, but they do trigger a unique command event, EVT_CHECKBOX. The toggle state of a wx.CheckBox can be retrieved with the methods GetValue() and SetValue(state), and its value is a Boolean. The method lsChecked() is identical to GetValue() and is included to make the code clearer.
Was this article helpful?