如何从wx.RadioBox中检索值?我有以下代码

时间:2016-03-20 07:42:39

标签: wxpython

如何从wx.RadioBox中检索值?我有以下代码:

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

我尝试了下面的代码但是出现了一些错误

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

AttributeError:' RadioBoxFrame'对象没有属性' SetVal'

1 个答案:

答案 0 :(得分:0)

看起来您的SetVal方法缩进不正确,以下工作

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