Life is really simple, but we insist on making it complicated.  -Confucius




BoolChoice

BoolChoice is very simple in that there are only two differences between it and the native wx.Choice. One, its choices list is implicitly, (but should not be changed) [0] = "False" and [1] = "True". The second difference is the addition of the GetBoolValue() function. It simply gets the index of the current selection, and returns the corresponding boolean value. After a few road tests and maybe some feedback, I hope to have this widget included in the wxPython distribution.

Download

The current version is 0.1.

Prerequisites

BoolChoice 0.1


Source Code

""" BoolChoice, a wx.Choice with Boolean choices. """

import wx    

class BoolChoice(wx.Choice):
    """BoolChoice(wx.Choice).

BoolChoice is very simple in that there are only two differences
between it and the native wx.Choice. One, its choices list is
implicitly, (but should not be changed) [0] = "False" and [1] =
"True". The second difference is the addition of the GetBoolValue()
function. It simply gets the index of the current selection, and
returns the corresponding boolean value.
"""
    def __init__(self, parent, id, pos=wx.DefaultPosition, size=wx.DefaultSize,
                 choices=["False", "True"], style=0,
                 validator=wx.DefaultValidator, name=wx.ChoiceNameStr):
        wx.Choice.__init__(self, parent, id, pos, size, choices, style, validator, name)

    def GetBoolValue(self):
        if self.GetCurrentSelection() == 1:
            return True
        else:
            return False

class TestFrame(wx.Frame):
    def __init__(self, parent, id, title, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE):
        wx.Frame.__init__(self, parent, id, title, pos, size, style)
        
        panel = wx.Panel(self, -1)
        self.ch1 = BoolChoice(panel, 201)
        self.ch2 = BoolChoice(panel, 202)
        
        sframe = wx.StaticBox(panel, -1, "")
        sfsizer = wx.StaticBoxSizer(sframe, wx.VERTICAL)
        st = wx.StaticText(panel, -1, "")
        sfsizer.Add(st, 0, wx.EXPAND, 0)
        st = wx.StaticText(panel, -1, BoolChoice.__doc__, style=wx.ALIGN_CENTER)
        sfsizer.Add(st, 1, wx.EXPAND | wx.ALL, 2)

        st = wx.StaticText(panel, -1, "Choice 1: ", style=wx.ALIGN_RIGHT)
        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        hsizer.Add(st, 1, wx.EXPAND | wx.TOP, 5)
        hsizer.Add(self.ch1, 1, wx.EXPAND| wx.LEFT, 4)
        sfsizer.Add(hsizer, 0, wx.EXPAND | wx.ALL, 2)

        st = wx.StaticText(panel, -1, "Choice 2: ", style=wx.ALIGN_RIGHT)
        hsizer = wx.BoxSizer(wx.HORIZONTAL)
        hsizer.Add(st, 1, wx.EXPAND | wx.TOP, 5)
        hsizer.Add(self.ch2, 1, wx.EXPAND | wx.LEFT, 4)
        sfsizer.Add(hsizer, 0, wx.EXPAND | wx.ALL, 2)
        st = wx.StaticText(panel, -1, "")
        sfsizer.Add(st, 0, wx.EXPAND, 0)
        
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(sfsizer, 1, wx.EXPAND | wx.ALL, 5)
        panel.SetSizer(sizer)
        
        self.Bind(wx.EVT_CHOICE, self.OnChoice)
        self.Bind(wx.EVT_CHOICE, self.OnChoice)

    def OnChoice(self, event):
        if event.GetId() == self.ch1.GetId():
            print "Choice 1: " + str(self.ch1.GetBoolValue())
            print "Bool check: " + str(bool(self.ch1.GetBoolValue()))
        else:
            print "Choice 2: " + str(self.ch2.GetBoolValue())
            print "Bool check: " + str(bool(self.ch2.GetBoolValue()))
            
if __name__ == "__main__":
    app = wx.App()
    frame = TestFrame(None, -1, "BoolChoice Test")
    frame.Show()
    app.MainLoop()
all original content copyright © colin barnette 2008-10. all rights reserved. xhtml. css.