Problem with event handler radiobutton

Hi, I have a problem with the event handler of
radiobutton.

I have two buttons, the first one is automaticly
select on start.
When I click it, it does not generate any event, when
I click on the second one, is select it and the event
is throw and the method is called.
But if I reclick it, no method is called. I still can
select one or the other, but the methond is called no
more.

Here is the part of the code related:

   def __init__(self, *args, **kwds):
     self.server_name_rb =
wx.RadioButton(self.main_panel, 100, " Nom du Serveur
", style = wx.RB_GROUP)
     self.list_rb = wx.RadioButton(self.main_panel,
101, " Liste ")

      # events
      wx.EVT_RADIOBUTTON(self, 100,
self.server_media_select)
      wx.EVT_RADIOBUTTON(self, 101,
self.server_media_select)

    def server_media_select(self, event):
        radio_selected = event.GetEventObject()
        print str(radio_selected.GetLabel())
        configdict['type'] = 'file'
        configdict['host_filename'] = ''
        
any hint will be appreciated

Thank you

···

__________________________________________________________
Lèche-vitrine ou lèche-écran ?
magasinage.yahoo.ca

Francis Lavoie wrote:

Hi, I have a problem with the event handler of
radiobutton.

I have two buttons, the first one is automaticly
select on start.
When I click it, it does not generate any event, when
I click on the second one, is select it and the event
is throw and the method is called.
But if I reclick it, no method is called. I still can
select one or the other, but the methond is called no
more.

Here is the part of the code related:

Please specify the version and platform you are using. Also a small but complete sample that shows the problem would be most helpful.

···

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

I have noticed the same.

Basically, once a button is clicked / selected clicking it again does not fire an event
And buttons only have one event.

I have been told this is a feature not a bug, and is expected functionality

I have been struggling to find a way around this but not successful yet

Perhaps I am misunderstanding the problem, but I have just run the RadioButton example from the wxPython Demo and the EVT_RADIOBUTTON events are being triggered everytime I click on any of the RadioButtons, irrespective of whether they are already selected or not.

Tested using wxPython 4.3.1 gtk3 (phoenix) wxWidgets 3.3.3 + Python 3.12.3 + Linux Mint 22.3.

Here is a standalone version of the example from the wxPython Demo. Note: I changed the parents of the radio buttons and text controls to be their respective static boxes in order to get rid of some warning messages. I also changed the log output to be stdout.

#!/usr/bin/env python

import wx
import sys

class TestPanel( wx.Panel ):
    def __init__( self, parent, log ):

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

        # Layout controls on panel:
        vs = wx.BoxSizer( wx.VERTICAL )

        box1_title = wx.StaticBox( panel, -1, "Group 1" )
        box1 = wx.StaticBoxSizer( box1_title, wx.VERTICAL )
        grid1 = wx.FlexGridSizer( cols=2 )

        # 1st group of controls:
        self.group1_ctrls = []
        radio1 = wx.RadioButton( box1_title, -1, " Radio1 ", style = wx.RB_GROUP )
        radio2 = wx.RadioButton( box1_title, -1, " Radio2 " )
        radio3 = wx.RadioButton( box1_title, -1, " Radio3 " )
        text1 = wx.TextCtrl( box1_title, -1, "" )
        text2 = wx.TextCtrl( box1_title, -1, "" )
        text3 = wx.TextCtrl( box1_title, -1, "" )
        self.group1_ctrls.append((radio1, text1))
        self.group1_ctrls.append((radio2, text2))
        self.group1_ctrls.append((radio3, text3))

        for radio, text in self.group1_ctrls:
            grid1.Add( radio, 0, wx.ALIGN_CENTRE|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
            grid1.Add( text, 0, wx.ALIGN_CENTRE|wx.LEFT|wx.RIGHT|wx.TOP, 5 )

        box1.Add( grid1, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
        vs.Add( box1, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )

        box2_title = wx.StaticBox( panel, -1, "Group 2" )
        box2 = wx.StaticBoxSizer( box2_title, wx.VERTICAL )
        grid2 = wx.FlexGridSizer( cols=2 )

        # 2nd group of controls:
        self.group2_ctrls = []
        radio4 = wx.RadioButton( box2_title, -1, " Radio1 ", style = wx.RB_GROUP )
        radio5 = wx.RadioButton( box2_title, -1, " Radio2 " )
        radio6 = wx.RadioButton( box2_title, -1, " Radio3 " )
        text4 = wx.TextCtrl( box2_title, -1, "" )
        text5 = wx.TextCtrl( box2_title, -1, "" )
        text6 = wx.TextCtrl( box2_title, -1, "" )
        self.group2_ctrls.append((radio4, text4))
        self.group2_ctrls.append((radio5, text5))
        self.group2_ctrls.append((radio6, text6))

        for radio, text in self.group2_ctrls:
            grid2.Add( radio, 0, wx.ALIGN_CENTRE|wx.LEFT|wx.RIGHT|wx.TOP, 5 )
            grid2.Add( text, 0, wx.ALIGN_CENTRE|wx.LEFT|wx.RIGHT|wx.TOP, 5 )

        box2.Add( grid2, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
        vs.Add( box2, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )

        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:
            self.Bind(wx.EVT_RADIOBUTTON, self.OnGroup1Select, radio )

        for radio, text in self.group2_ctrls:
            self.Bind(wx.EVT_RADIOBUTTON, self.OnGroup2Select, radio )

        for radio, text in self.group1_ctrls + self.group2_ctrls:
            radio.SetValue(0)
            text.Enable(False)

    def OnGroup1Select( self, event ):
        radio_selected = event.GetEventObject()
        self.log.write('Group1 %s selected\n' % radio_selected.GetLabel() )

        for radio, text in self.group1_ctrls:
            if radio is radio_selected:
                text.Enable(True)
            else:
                text.Enable(False)

    def OnGroup2Select( self, event ):
        radio_selected = event.GetEventObject()
        self.log.write('Group2 %s selected\n' % radio_selected.GetLabel() )

        for radio, text in self.group2_ctrls:
            if radio is radio_selected:
                text.Enable(True)
            else:
                text.Enable(False)

class TestFrame(wx.Frame):
    def __init__(self, log):
        wx.Frame.__init__(self, None)
        self.SetTitle("Test Radio Buttons")
        self.SetSize((400, 400))
        TestPanel(self, log)
        
        
if __name__ == "__main__":
    app = wx.App()
    frame = TestFrame(sys.stdout)
    frame.Show()
    app.MainLoop()

Yes but; I am working on a edge case to build a macro recorder. Even in the Demo for the RadioButton or RadioBox if you click on an already selected button - no event is trigger, the button does its little partial fade redraw but no event.

Why would you try to select a selected button - it is already selected, right? So the behavior makes sense.

But I am trying to build a macro where the use selectes a series of widgets to fire in sequence, linked to a hotkey etc. And, in the selection process a button may already be selected and as such the macro building program never sees the selection. Looks like will have to be a multistep prcess.

Use hovers over the widget enters some key, and the macro program finds the widget under the mouse, then quieres what state the user wants the widget in, and on to the next widget the user wants to sore in the macro que.

@RichardT: This might be a platform dependent behaviour. On Windows I don’t see a EVT_RADIOBUTTON on a selected radio button, which is good…

@DrMatrix: You always can handle lower level events like EVT_LEFT_DOWN. On an un-selected button, call event.Skip() for the default handler, on a selected, handle it like a repeated EVT_RADIOBUTTON.

Ahhhhh
I am on Windows 11, Python 13.12 and wxPython 4.??

So my be an OS thing

WJ