Search code examples
wxpython

How can I retrieve a value from wx.RadioBox? I have the following code


How can I retrieve a value from wx.RadioBox? I have the following code:

import wx

class RadioBoxFrame(wx.Frame):
def __init__(self):
    wx.Frame.__init__(self, None, -1, 'Radio Box Example', 
            size=(350, 200))
    panel = wx.Panel(self, -1)
    sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
                  'six', 'seven', 'eight']
    self.button=wx.RadioBox(panel, -1, "A Radio Box", (10, 10), wx.DefaultSize,
                    sampleList, 2, wx.RA_SPECIFY_COLS)



if __name__ == '__main__':
    app = wx.PySimpleApp()
    RadioBoxFrame().Show()
    app.MainLoop()             

I tried below code but got some error

 self.Bind(wx.EVT_RADIOBOX, self.SetVal)
    def SetVal(self, event):
        state1 = self.button.GetSelection()
        print state1

AttributeError: 'RadioBoxFrame' object has no attribute 'SetVal'


Solution

  • It looks like your SetVal method is indented incorrectly, the following works

    import wx
    
    class RadioBoxFrame(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self, None, -1, 'Radio Box Example',
                    size=(350, 200))
            panel = wx.Panel(self, -1)
            sampleList = ['zero', 'one', 'two', 'three', 'four', 'five',
                          'six', 'seven', 'eight']
            self.button=wx.RadioBox(panel, -1, "A Radio Box", (10, 10), wx.DefaultSize,
                            sampleList, 2, wx.RA_SPECIFY_COLS)
    
            self.Bind(wx.EVT_RADIOBOX, self.SetVal)
    
        def SetVal(self, event):
            state1 = self.button.GetSelection()
            print state1
    
    
    
    if __name__ == '__main__':
        app = wx.App()
        RadioBoxFrame().Show()
        app.MainLoop()