Radio buttons not keeping a new value without window focus

I am experiencing an issue when calling the SetValue() method of a RadioButton, if its containing window does not have focus.

When the SetValue() call is made, the value of the radio buttons in the group change immediately. However, on simply clicking the window containing the radio buttons so that it regains focus, the values are changed back to the previous ones, and a wxEVT_COMMAND_RADIOBUTTON_SELECTED event is dispatched.

I am running wxPython 2.8.7.1 and Python 2.5.2 on Windows XP.

Here is an example script that demonstrates my problem. Click one of the buttons in the "Remote" window to select the corresponding radio button in the "Main" window. Then click the "Main" window so that it regains focus. The radio buttons will revert to the previous state before the "Main" window lost focus.

Thanks for your help.

David Ward
Electronic Systems Laboratory, SEV, IOB
Georgia Tech Research Institute

···

-----

import wx

class ProblemApp(wx.App):
    optionsCount = 5

    def OnInit(self):
        # Create main frame
        self.mainFrame = wx.Frame(parent = None, title = "Main")
        self.mainSizer = wx.BoxSizer(orient = wx.VERTICAL)

        self.radios = []
        for index in range(0, self.optionsCount):
            if index == 0:
                style = wx.RB_GROUP
            else:
                style = 0
            radio = wx.RadioButton(parent = self.mainFrame,
                                   label = "Choice %d" % (index + 1),
                                   style = style)
            self.mainSizer.Add(radio)
            self.radios.append(radio)

        self.mainFrame.Bind(wx.EVT_RADIOBUTTON, self.OnRadio)
        self.mainFrame.SetSizer(self.mainSizer)
        self.mainFrame.Show()

        # Create remote frame
        self.remoteFrame = wx.Frame(parent = None, title = "Remote")
        self.remoteSizer = wx.BoxSizer(orient = wx.VERTICAL)

        self.buttons = []
        for index in range(0, self.optionsCount):
            button = wx.Button(parent = self.remoteFrame,
                               label = "Select Choice %d" % (index + 1))
            self.remoteSizer.Add(button)
            self.buttons.append(button)

        self.remoteFrame.Bind(wx.EVT_BUTTON, self.OnButton)
        self.remoteFrame.SetSizer(self.remoteSizer)
        self.remoteFrame.Show()

        return True

    def OnRadio(self, event):
        print "OnRadio: '%s' clicked" % event.GetEventObject().GetLabel()

    def OnButton(self, event):
        print "OnButton: '%s' pressed" % event.GetEventObject().GetLabel()
        index = self.buttons.index(event.GetEventObject())
        self.radios[index].SetValue(True)

if __name__ == "__main__":
    app = ProblemApp(redirect = False)
    app.MainLoop()

Not that it’s a lot of help, but your code works as you would expect it to on OSX.

···

On Mon, Jun 30, 2008 at 12:45 PM, David.Ward@gtri.gatech.edu wrote:

I am experiencing an issue when calling the SetValue() method of a RadioButton, if its containing window does not have focus.

When the SetValue() call is made, the value of the radio buttons in the group change immediately. However, on simply clicking the window containing the radio buttons so that it regains focus, the values are changed back to the previous ones, and a wxEVT_COMMAND_RADIOBUTTON_SELECTED event is dispatched.

I am running wxPython 2.8.7.1 and Python 2.5.2 on Windows XP.

Here is an example script that demonstrates my problem. Click one of the buttons in the “Remote” window to select the corresponding radio button in the “Main” window. Then click the “Main” window so that it regains focus. The radio buttons will revert to the previous state before the “Main” window lost focus.

Thanks for your help.

David Ward

Electronic Systems Laboratory, SEV, IOB

Georgia Tech Research Institute



Stand Fast,
tjg.

For those not watching Trac, David submitted a ticket about this and it has been answered there (and fixed already in the trunk.)

http://trac.wxwidgets.org/ticket/9676

···

David.Ward@gtri.gatech.edu wrote:

Not that it's a lot of help, but your code works as you would expect it to
on OSX.

Timothy, thanks for pointing that out. I just confirmed that this code works correctly under Ubuntu 8.04 as well. So is this a bug in wxWindows?

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