[wxPython] wxRadioButtons and groups

Robin wrote:

Will Sadkin wrote:
> There was another bug in my code which was somehow having
this effect
> on the radio groups... I fixed something seemingly unrelated, and
> now my code works.

What did you change? I just tried it out and I am having the
same problem.

um, er... I'm not really sure... <:-} I had an outer checkbox that
was being used to enable/disable the two inner control groups, and
I had a bug in my event handling code for that control that
caused a traceback. Somehow, when I fixed this, the controls started
behaving the way I wanted them to...

However, because I couldn't figure out what I did to make it work,
I wrote up a wxRadioButton demo for you, along the lines of
my example, to see if it would break. It also seems to work.
Maybe you can compare it to your test code, and see if there's some
subtle easy-to-make mistake, and tell us what it is.

In any event, I think it would be a nice addition to the core controls
demos, as wxRadioButton currently has none of its own!

Regards,
/Will

···

#----------------------------------------------------------------------
from wxPython.wx import *
#----------------------------------------------------------------------

class TestPanel( wxPanel ):
    def __init__( self, parent, log ):

        wxPanel.__init__( self, parent, -1 )
        self.log = log
        panel = wxPanel( self, -1 )

        # 1st group of controls:
        self.group1_ctrls =
        radio1 = wxRadioButton( panel, 10, "Radio1", style = wxRB_GROUP )
        text1 = wxTextCtrl( panel, 20, "" )
        radio2 = wxRadioButton( panel, 30, "Radio2" )
        text2 = wxTextCtrl( panel, 40, "" )
        radio3 = wxRadioButton( panel, 50, "Radio3" )
        text3 = wxTextCtrl( panel, 60, "" )
        self.group1_ctrls.append((radio1, text1))
        self.group1_ctrls.append((radio2, text2))
        self.group1_ctrls.append((radio3, text3))

        # 2nd group of controls:
        self.group2_ctrls =
        radio4 = wxRadioButton( panel, 70, "Radio1", style = wxRB_GROUP )
        text4 = wxTextCtrl( panel, 80, "" )
        radio5 = wxRadioButton( panel, 90, "Radio2" )
        text5 = wxTextCtrl( panel, 100, "" )
        radio6 = wxRadioButton( panel, 110, "Radio3" )
        text6 = wxTextCtrl( panel, 120, "" )
        self.group2_ctrls.append((radio4, text4))
        self.group2_ctrls.append((radio5, text5))
        self.group2_ctrls.append((radio6, text6))

        # Layout controls on panel:
        vs = wxBoxSizer( wxVERTICAL )
        
        box1_title = wxStaticBox( panel, -1, "Group 1" )
        box1 = wxStaticBoxSizer( box1_title, wxVERTICAL )
        grid1 = wxFlexGridSizer( 0, 2, 0, 0 )
        for radio, text in self.group1_ctrls:
            grid1.AddWindow( radio, 0, wxALIGN_CENTRE|wxLEFT|wxRIGHT|wxTOP,
5 )
            grid1.AddWindow( text, 0, wxALIGN_CENTRE|wxLEFT|wxRIGHT|wxTOP, 5
)
        box1.AddSizer( grid1, 0, wxALIGN_CENTRE|wxALL, 5 )
        vs.AddSizer( box1, 0, wxALIGN_CENTRE|wxALL, 5 )

        box2_title = wxStaticBox( panel, -1, "Group 2" )
        box2 = wxStaticBoxSizer( box2_title, wxVERTICAL )
        grid2 = wxFlexGridSizer( 0, 2, 0, 0 )
        for radio, text in self.group2_ctrls:
            grid2.AddWindow( radio, 0, wxALIGN_CENTRE|wxLEFT|wxRIGHT|wxTOP,
5 )
            grid2.AddWindow( text, 0, wxALIGN_CENTRE|wxLEFT|wxRIGHT|wxTOP, 5
)
        box2.AddSizer( grid2, 0, wxALIGN_CENTRE|wxALL, 5 )
        vs.AddSizer( box2, 0, wxALIGN_CENTRE|wxALL, 5 )

        panel.SetAutoLayout( true )
        panel.SetSizer( vs )
        vs.Fit( panel )
        panel.Move( (50,50) )
        self.panel = panel

        # Setup event handling and initial state for controls:
        for radio, text in self.group1_ctrls:
            EVT_RADIOBUTTON( self, radio.GetId(), self.OnGroup1Select )

        for radio, text in self.group2_ctrls:
            EVT_RADIOBUTTON( self, radio.GetId(), self.OnGroup2Select )
            
        for radio, text in self.group1_ctrls + self.group2_ctrls:
            radio.SetValue(0)
            text.Enable(FALSE)

    def OnGroup1Select( self, event ):
        radio_selected = self.panel.FindWindowById( event.GetId() )
        self.log.write('Group1 %s selected\n' % radio_selected.GetLabel() )
        for radio, text in self.group1_ctrls:
            if radio == radio_selected:
                text.Enable(TRUE)
            else:
                text.Enable(FALSE)

    def OnGroup2Select( self, event ):
        radio_selected = self.panel.FindWindowById( event.GetId() )
        self.log.write('Group2 %s selected\n' % radio_selected.GetLabel() )
        for radio, text in self.group2_ctrls:
            if radio == radio_selected:
                text.Enable(TRUE)
            else:
                text.Enable(FALSE)

#----------------------------------------------------------------------

def runTest( frame, nb, log ):
    win = TestPanel( nb, log )
    return win

#----------------------------------------------------------------------

overview = """
<html>
<P>
This demo shows how individual radio buttons can be used to build
more complicated selection mechanisms...
<P>
It uses 2 groups of wxRadioButtons, where the groups are defined by
instantiation. When a wxRadioButton is created with the <I>wxRB_GROUP</I>
style, all subsequent wxRadioButtons created without it are implicitly
added to that group by the framework.
"""

Will Sadkin wrote:

Robin wrote:

What did you change? I just tried it out and I am having the same problem.

[...]

However, because I couldn't figure out what I did to make it work,
I wrote up a wxRadioButton demo for you, along the lines of
my example, to see if it would break. It also seems to work. Maybe you can compare it to your test code, and see if there's some subtle easy-to-make mistake, and tell us what it is.

It's probably because I have a wxRadioBox on the same panel.

In any event, I think it would be a nice addition to the core controls
demos, as wxRadioButton currently has none of its own!

Thanks. I've been meaning to do this for a couple years now! <wink>

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!