468,249 Members | 1,489 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

wxPython default radiobox choice

In my GUI app, I have a radio box allowing the user to pick his/her
gender. When I click a button, I'd like the radio box to tell the
program which choice is marked. I can get it to work when I manually
pick a choice but I get an error when the choice is left at the default
value, which is "Male".

I have the radio box tied to an event to "see" when a choice is made,
but how does it work if the person just leaves the default value? I've
looked at the wxPython demo but the radio box actions only work for
changing values.

Aug 24 '06 #1
2 3855
crystalattice írta:
In my GUI app, I have a radio box allowing the user to pick his/her
gender. When I click a button, I'd like the radio box to tell the
program which choice is marked. I can get it to work when I manually
pick a choice but I get an error when the choice is left at the default
value, which is "Male".

I have the radio box tied to an event to "see" when a choice is made,
but how does it work if the person just leaves the default value? I've
looked at the wxPython demo but the radio box actions only work for
changing values.

I'm not sure if I understood your problem. You can call the
wxRadioBox.GetSelection method anytime to get the current value.
If you need that value very often (e.g. thousand times per sec) then you
can "shadow" the default value in an instance variable.

Example (untested):
class MyClass(wx.Frame):
"""In this frame, you can use the curchoice variable after
construction."""
def __init__(self):
# ... create your frame here...
r =self.radio = wx.RadioBox(self,-1,choiches=['One','Two','Three'])
# Set default value
r.SetSelection(0) # This does not cause a
wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted.
self.curchoice = 0
# Bind the event
r.Bind(
wx.EVT_COMMAND_RADIOBOX_SELECTED,
self.OnRadioChanged,
r
)
def OnRadioChanged(self,event):
self.curchoice = self.radio.GetSelection()
# Do whatever you want here...
event.Skip()

Aug 24 '06 #2

Laszlo Nagy wrote:
crystalattice írta:
In my GUI app, I have a radio box allowing the user to pick his/her
gender. When I click a button, I'd like the radio box to tell the
program which choice is marked. I can get it to work when I manually
pick a choice but I get an error when the choice is left at the default
value, which is "Male".

I have the radio box tied to an event to "see" when a choice is made,
but how does it work if the person just leaves the default value? I've
looked at the wxPython demo but the radio box actions only work for
changing values.
I'm not sure if I understood your problem. You can call the
wxRadioBox.GetSelection method anytime to get the current value.
If you need that value very often (e.g. thousand times per sec) then you
can "shadow" the default value in an instance variable.

Example (untested):
class MyClass(wx.Frame):
"""In this frame, you can use the curchoice variable after
construction."""
def __init__(self):
# ... create your frame here...
r =self.radio = wx.RadioBox(self,-1,choiches=['One','Two','Three'])
# Set default value
r.SetSelection(0) # This does not cause a
wxEVT_COMMAND_RADIOBOX_SELECTED event to get emitted.
self.curchoice = 0
# Bind the event
r.Bind(
wx.EVT_COMMAND_RADIOBOX_SELECTED,
self.OnRadioChanged,
r
)
def OnRadioChanged(self,event):
self.curchoice = self.radio.GetSelection()
# Do whatever you want here...
event.Skip()
Thanks for the response. I got it working now. I was trying to bind
the radio box to an event, rather than just calling GetSelection(). I
thought I needed to bind it to make it work (based on the wxPython demo
code).

Aug 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.