wxPython default radiobox choice

C

crystalattice

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.
 
L

Laszlo Nagy

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()
 
C

crystalattice

Laszlo said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top