2016-09-16 72 views
0

下面的代码在每次使用组合框选择新值时填充网格。 第一次代码运行时,它工作正常,并创建一个填充网格,并在第4列中的每个单元格中都有一个下拉列表。但是,当我选择第二个新值并且该函数执行self.m_grid_3.ClearGrid()并重新填充时,以下错误。为什么SetCellEditor在self.m_grid_3.ClearGrid()后重新应用时会导致错误()

self.m_grid3.SetCellEditor(row, col, self.choice_editor) 
    File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\grid.py", line 2000, in SetCellEditor 
    return _grid.Grid_SetCellEditor(*args, **kwargs) 
    TypeError: in method 'Grid_SetCellEditor', expected argument 4 of type 'wxGridCellEditor *' 

在col4中选择下拉列表,然后崩溃python。

任何想法如何解决这个问题。

这里是有问题的代码。

class Inspection(BulkUpdate): 
    def __init__(self, parent): 
     BulkUpdate.__init__(self, parent) 
     list = EmployeeList() 
     list_climbers = list.get_climbers() 
     for name in list_climbers: 
      self.edit_kit_comboBox.Append(str(name.employee)) 
     choices = ["Yes", "No", "Not Checked"] 
     self.choice_editor = wx.grid.GridCellChoiceEditor(choices, True) 


    def on_engineer_select(self, event): 
     self.m_grid3.ClearGrid() 
     person = self.edit_kit_comboBox.GetValue() 
     list = KitList() 
     equipment = list.list_of_equipment(person, 1) 
     rows = len(equipment) 

     for row in range(0, rows): 
      for col in range(0, 5): 
       print "row = %s col = %s" % (row, col) 
       if col == 4: 
        self.m_grid3.SetCellValue(row, col+2, str(equipment[row][col])) 
        self.m_grid3.SetCellValue(row, col, "Pass") 
        self.m_grid3.SetCellEditor(row, col, self.choice_editor) 
       else: 
        self.m_grid3.SetCellValue(row, col, str(equipment[row][col])) 

当第二次填充网格时,代码在第二个循环停止。 我一直在努力工作这几天。

回答

0

尝试增加这__init__

self.choice_editor.IncRef() 

我的猜测是,当你调用ClearGrid编辑对象的C++部分被删除。给它额外的参考告诉网格,你想要坚持下去。

0

以增加

self.choice_editor.IncRef() 

的回答让我感动的选择列表定义成函数

self.choice_editor.IncRef() 

所以现在它看起来像这样

def on_engineer_select(self, event): 
    self.m_grid3.ClearGrid() 
    choices = ["Pass", "Fail", "Not Checked"] 
    self.choice_editor = wx.grid.GridCellChoiceEditor(choices, False) 
    person = self.edit_kit_comboBox.GetValue() 
    list = KitList() 
    equipment = list.list_of_equipment(person, 1) 
    print "Length of equipment = %s" % len(equipment) 
    rows = len(equipment) 
    for row in range(0, rows): 
     for col in range(0, 5): 
      print "row = %s col = %s" % (row, col) 
      if col == 4: 
       self.choice_editor.IncRef() 
       self.m_grid3.SetCellValue(row, col+2, str(equipment[row][col])) 
       self.m_grid3.SetCellValue(row, col+1, str(date.today())) 
       self.m_grid3.SetCellEditor(row, col, self.choice_editor) 
       self.m_grid3.SetCellValue(row, col, "Pass") 

      else: 
       self.m_grid3.SetCellValue(row, col, str(equipment[row][col])) 

现在的代码一起按需要工作。

相关问题