Python:根据复选框选择启用Radiobox

时间:2013-08-07 01:24:45

标签: python wxpython

我有一个使用WXPython开发的简单python GUI。我有一个checkbox1,checkbox2和radiobox1。基于Checkbox2选择,我希望启用或禁用radiobox1。例如,如果未选中Checkbox2,我希望禁用radiobox1。我尝试使用getvalue()参数,但没有成功。这是示例代码。

非常感谢任何帮助。

import wx

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1CHECKBOX1, wxID_FRAME1CHECKBOX2, 
 wxID_FRAME1NOTEBOOK1, wxID_FRAME1PANEL1, wxID_FRAME1RADIOBOX1, 
 wxID_FRAME1STATICBOX1, wxID_FRAME1STATICTEXT1, 
] = [wx.NewId() for _init_ctrls in range(8)]

class Frame1(wx.Frame):
    def _init_coll_notebook1_Pages(self, parent):
        # generated method, don't edit

        parent.AddPage(imageId=-1, page=self.panel1, select=True, text='Pages0')
        parent.AddPage(imageId=-1, page=self.staticText1, select=False,
              text='Pages1')

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(470, 184), size=wx.Size(677, 492),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(661, 454))

        self.notebook1 = wx.Notebook(id=wxID_FRAME1NOTEBOOK1, name='notebook1',
              parent=self, pos=wx.Point(0, 0), size=wx.Size(661, 454), style=0)

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1',
              parent=self.notebook1, pos=wx.Point(0, 0), size=wx.Size(653, 428),
              style=wx.TAB_TRAVERSAL)

        self.staticBox1 = wx.StaticBox(id=wxID_FRAME1STATICBOX1,
              label='staticBox1', name='staticBox1', parent=self.panel1,
              pos=wx.Point(16, 16), size=wx.Size(232, 128), style=0)

        self.checkBox1 = wx.CheckBox(id=wxID_FRAME1CHECKBOX1, label='checkBox1',
              name='checkBox1', parent=self.panel1, pos=wx.Point(24, 40),
              size=wx.Size(70, 13), style=0)

        self.checkBox2 = wx.CheckBox(id=wxID_FRAME1CHECKBOX2, label='checkBox2',
              name='checkBox2', parent=self.panel1, pos=wx.Point(24, 64),
              size=wx.Size(70, 13), style=0)
        self.checkBox2.Set3StateValue(1)
        self.checkBox2.Bind(wx.EVT_CHECKBOX, self.OnCheckBox2Checkbox,
              id=wxID_FRAME1CHECKBOX2)

        self.radioBox1 = wx.RadioBox(choices=['Phase A', 'PhaseB', 'Phase C'],
              id=wxID_FRAME1RADIOBOX1, label='radioBox1', majorDimension=3,
              name='radioBox1', parent=self.panel1, pos=wx.Point(32, 88),
              size=wx.Size(200, 44), style=wx.RA_SPECIFY_COLS)

        self.staticText1 = wx.StaticText(id=wxID_FRAME1STATICTEXT1,
              label='staticText1', name='staticText1', parent=self.notebook1,
              pos=wx.Point(0, 0), size=wx.Size(653, 428), style=0)

        self._init_coll_notebook1_Pages(self.notebook1)

    def __init__(self, parent):
        self._init_ctrls(parent)

    def OnCheckBox2Checkbox(self, event):
        #event.Skip()
        if self.checkBox2.GetValue() == 'False':
            self.radioBox1.Enable(False)
            print self.checkBox2.IsChecked()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = create(None)
    frame.Show()

    app.MainLoop()

1 个答案:

答案 0 :(得分:1)

我认为你的问题是你只是朝着一个方向改变事物 - 改变:

def OnCheckBox2Checkbox(self, event):
    #event.Skip()
    if self.checkBox2.GetValue() == 'False':
        self.radioBox1.Enable(False)
        print self.checkBox2.IsChecked()

为:

def OnCheckBox2Checkbox(self, event):
    self.radioBox1.Enable(self.checkBox2.IsChecked())
    print self.checkBox2.IsChecked()