Index radio box and contained buttons

59 views
Skip to first unread message

Ibraheem Khan

unread,
Jul 18, 2014, 12:07:51 AM7/18/14
to wxpytho...@googlegroups.com
I have multiple radio boxes containing three buttons as shown below. I want to keep track of clicked radio buttons within radio boxes then do some processing. I am not sure how to index radio box and then clicked radio buttons within that radio box as there are multiple boxes. Any suggestions?
class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, "test", wx.DefaultPosition, wx.Size(900, 600))
        radio1Choices = ['F','G','P']
        # rb_list has 23 records -- basically used to set the distance b/w radio boxes
        for i in rb_list:
            self.radio1 = wx.RadioBox(scroll1, wx.ID_ANY, wx.EmptyString, (550,i), (20,5), radio1Choices, 3, wx.RA_SPECIFY_COLS | wx.NO_BORDER)
            self.radio1.SetSelection( 0 )
            grid_sizer.Add( self.radio1, 0, wx.ALL, 5)

        # Bind the radio box select event to a function
        self.radio1.Bind( wx.EVT_RADIOBOX, self.on_selected )

    def on_selected(self, event):
        if self.radio1.GetStringSelection() == 'F':
            print "F"
        elif self.radio1.GetStringSelection() == 'G':
            print "G"
        else:
            self.radio1.GetStringSelection() == 'P'
            print "P"

Steve Barnes

unread,
Jul 18, 2014, 1:08:34 AM7/18/14
to wxpytho...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
A couple of possibilities:
  1. Make your choices list have a structure [(method, [choices])....] then you can bind to a specific handler method for each group. 
  2. In your handler get the originating radio group from the event and then decide what to do.

Gadget/Steve

Ibraheem Khan

unread,
Jul 18, 2014, 1:12:25 AM7/18/14
to wxpytho...@googlegroups.com
I am not very good at wxpython so a little bit more explanation about possibilities would be really helpful with some form of simple examples. Appreciate it.


--
You received this message because you are subscribed to a topic in the Google Groups "wxPython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wxpython-users/Sju8vsYe7cM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wxpython-user...@googlegroups.com.

Steve Barnes

unread,
Jul 18, 2014, 1:35:13 AM7/18/14
to wxpytho...@googlegroups.com
On 18/07/14 06:12, Ibraheem Khan wrote:
I am not very good at wxpython so a little bit more explanation about possibilities would be really helpful with some form of simple examples. Appreciate it.

A couple of possibilities:
  1. Make your choices list have a structure [(method, [choices])....] then you can bind to a specific handler method for each group. 
  2. In your handler get the originating radio group from the event and then decide what to do.

Gadget/Steve

--
You received this message because you are subscribed to a topic in the Google Groups "wxPython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wxpython-users/Sju8vsYe7cM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wxpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Option 1:


        rb_list = [(self.OnRadio1, ['a', 'b', 'c']), (self.OnRadio2, ['d', 'e', 'f', 'g']), ......]
        for (method, choices) in rb_list:
            radio_item = wx.RadioBox(scroll1, wx.ID_ANY, wx.EmptyString, (-1, -1), (-1, -1),
                                     radio1Choices, wx.RA_SPECIFY_COLS|wx.NO_BORDER)
            radio_item.SetSelection( 0 )
            grid_sizer.Add(radio_item, 0, wx.ALL, 5)

            # Bind the radio box select event to a function

            self.Bind(wx.EVT_RADIOBOX, method, source=radio_item)

   def OnRadio1(self, evt):
      """ Handler for Radio Group 1 """
      :
      :

   def OnRadio2(self, evt):
      :

In option 2:
   def OnRadioHandler(self, evt):
      """ Handler for Radio Groups """
      where_from = evt.GetEventObject()
      # Code based on which object & object contents caused the event.

Of course this being python you can also use a similar method to that in 1 to add additional information to each radio group that is then retrieved and used in a single handler using option 2.

Personally I would say that option 1 results in clearer code for a relative beginner.

Gadget/Steve
  

Ibraheem Khan

unread,
Jul 18, 2014, 1:44:55 AM7/18/14
to wxpytho...@googlegroups.com
In 2nd option, when I printed:

print event.GetEventObject()

Then I get this:

<wx._controls.RadioBox; proxy of <Swig Object of type 'wxRadioBox *' at 0x2c36230> >

Please suggest how do I get index of radiobox within which I clicked radio button, if possible.

Tim Roberts

unread,
Jul 18, 2014, 11:56:33 AM7/18/14
to wxpytho...@googlegroups.com
Ibraheem Khan wrote:
> In 2nd option, when I printed:
>
> print event.GetEventObject()
>
> Then I get this:
>
> <wx._controls.RadioBox; proxy of <Swig Object of type 'wxRadioBox *'
> at 0x2c36230> >
>
> Please suggest how do I get index of radiobox within which I clicked
> radio button, if possible.

Did you check the documentation even once? You use GetSelection to
fetch the currently selected button.

choice = event.GetEventObject().GetSelection()

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Ibraheem Khan

unread,
Jul 18, 2014, 12:09:01 PM7/18/14
to wxpytho...@googlegroups.com
Tim: I exactly tried that before posting this question. The only missing link is how to index that box first within which I did selection. 

print event.GetEventObject().GetSelection()

gives only whether I clicked on radio button 1,2, or 3 but it doesn't tell about which row/radiobox as there are multiple boxes. 


Tim Roberts

unread,
Jul 18, 2014, 12:32:06 PM7/18/14
to wxpytho...@googlegroups.com
Ibraheem Khan wrote:
> Tim: I exactly tried that before posting this question. The only
> missing link is how to index that box first within which I did
> selection.
>
> print event.GetEventObject().GetSelection()
>
> gives only whether I clicked on radio button 1,2, or 3 but it doesn't
> tell about which row/radiobox as there are multiple boxes.

I'm sorry, I misunderstood. So, the issue is NOT finding out which
button was clicked, but rather finding out which BOX that button was in,
right?

What you get back from GetEventObject() is the original RadioBox object
you created. So, if you add your own private store:
self.radio1.my_identity = i

Then in the event handler you can refer to
event.GetEventObject().my_identity

Probably "i" is not the piece of information you want, but I think you
get the idea.

Ibraheem Khan

unread,
Jul 18, 2014, 4:39:36 PM7/18/14
to wxpytho...@googlegroups.com
Hmm. This is interesting, instead of giving me identity (sequential number) it gives me its unique position where I placed it in split window. 


Tim Roberts

unread,
Jul 18, 2014, 4:50:14 PM7/18/14
to wxpytho...@googlegroups.com
Ibraheem Khan wrote:
Hmm. This is interesting, instead of giving me identity (sequential number) it gives me its unique position where I placed it in split window.

Yes, of course it does, because that's what "i" contains in your original code.  That's exactly why I said this:


Probably "i" is not the piece of information you want, but I think you
get the idea.

You must learn to understand code suggestions before you implement them.
Reply all
Reply to author
Forward
0 new messages