0
        panel=wx.Panel(self)
        panel.SetBackgroundColour(wx.WHITE)
        font = wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD)
        self.field1 = wx.TextCtrl(panel,pos=(120,25),size=(170,20))
        self.field2 = wx.TextCtrl(panel,pos=(120,90),size=(170,20))
        self.field=[self.field1,self.field2]
        field1_lbl=wx.StaticText(panel,-1, label='path1:', pos=(25, 25))
        field1_lbl.SetFont(font)
        field2_lbl=wx.StaticText(panel,-1, label='path2:', pos=(25,90))
        field2_lbl.SetFont(font)
        self.checkbox1=wx.CheckBox(panel, -1,'Default',pos=(240,45),size=(50,25))
        self.checkbox1.SetValue(False)
        self.checkbox1.Bind(wx.EVT_CHECKBOX,self.OnDefault)

        self.checkbox2=wx.CheckBox(panel, -1,'Default',pos=(240,110),size=(50,25))
        self.checkbox2.SetValue(False)
        self.checkbox2.Bind(wx.EVT_CHECKBOX,self.OnDefault)
        self.checkbox=[self.checkbox1,self.checkbox2]
    def OnDefault(self,event):
        for checkbox in self.checkbox:
            for field in self.field:
                if self.checkbox.Value==False:
                    self.field.Enable(True)
                else:
                    self.field.Enable(False)

How can I access attributes of self.checkbox list object? I get an error saying

Traceback (most recent call last):
  File "D:\PROJECT\mypro.py", line 251, in OnDefault
    if self.checkbox.Value==False:
AttributeError: 'list' object has no attribute 'Value'
4

2 回答 2

1

Are you sure you do not want to define OnDefault as follows:

def OnDefault(self,event):
    for checkbox in self.checkbox:
        for field in self.field:
            if checkbox.Value==False:
                self.field.Enable(True)
            else:
                self.field.Enable(False)

self.checkbox is a normal Python list, its elements have a Value attribute. The way this programmed is that the Value of the self.checkbox2 controls both fields.

I don't see where self.field is defined, but if that is

self.field = [self.field1, self.field2]

then you might want OnDefault to be something like:

def OnDefault(self,event):
    for idx, checkbox in enumerate(self.checkbox):
        field = self.field[idx]:
        if checkbox.Value==False:
            field.Enable(True)
        else:
            field.Enable(False)

So the first checkbox controls the first field and the second checkbox the second field.

于 2013-03-12T23:14:25.270 回答
0

Though it worked with the below code, i found it difficult to access attributes of list object.here's my code.

def OnDefault(self,event):
    if self.checkbox1.Value==False:
        self.field1.Enable(True)
    else:
        self.field1.Enable(False)


    if self.checkbox2.Value==False:  
        self.field2.Enable(True)
    else:
        self.field2.Enable(False)

but, i have to check for all checkbox contained in the panel.Instead, i tried using list object which lead to an error saying

Traceback (most recent call last):
 File "D:\PROJECT\mypro.py", line 149, in OnDefault
   self.field.Enable(False)
AttributeError: 'list' object has no attribute 'Enable'
于 2013-03-13T10:57:57.893 回答