python-3.xwxpythonwxformbuilder

Trying to trigger event when a CellEditor in a Grid cell is changed


I have a table that displays Job details in a window created with wxFormBuilder. The table has a financials column labelled Finance that contains a comboBox (SetCellEditor) in each row (record).

The comboBox holds the values from the array

finance_options = ['Not Started', 'Preparing Day Sheets', 'Day Sheets Sent', 'PO Received', 'Partially Invoiced', 'Fully Invoiced']

When I change this dropdown I want to call a method called onFinanceChanged.

I would normally use the option in wxFormBuilder to assign the method to a cellChanged event, but that triggers if any cell in the table is changed.

    def populate_job_grid(self, jobs):
        finance_options = ['Not Started', 'Preparing Day Sheets', 'Day Sheets Sent', 'PO Received',
                           'Partially Invoiced', 'Fully Invoiced']
        for i in range(0, len(jobs)):
            self.jobNumberGrid.InsertRows(0)
        row = 0
        for job in jobs:
            year = arrow.get(job.created_at).date()
            job_year = str(year.year)[2:]
            self.jobNumberGrid.SetCellValue(row, 0, str(job.job_no))
            self.jobNumberGrid.SetCellValue(row, 1, job_year)
            self.jobNumberGrid.SetCellValue(row, 2, str(job.site))
            self.jobNumberGrid.SetCellValue(row, 3, str(job.client.name))
            self.jobNumberGrid.SetCellValue(row, 4, str(job.job_description))
            self.jobNumberGrid.SetCellValue(row, 5, str(job.user.name))
            created = arrow.get(job.created_at).date()
            created_at = datetime.datetime.strptime(str(created), "%Y-%m-%d").strftime("%d-%m-%Y")
            self.jobNumberGrid.SetCellValue(row, 6, created_at)
            self.jobNumberGrid.SetCellValue(row, 7, job.finance)
            finance = wx.grid.GridCellChoiceEditor(finance_options, allowOthers=True)
            self.jobNumberGrid.SetCellEditor(row, 7, finance).Bind(self.jobNumberGrid.EVT_GRID_CELL_CHANGED,
                                                                   self.onFinanceChange)

            row = row + 1

As you can see I tried to bind EVT_GRID_CELL_CHANGED to the cell but this gives "AttributeError: 'NoneType' object has no attribute 'Bind'" error.

Any ideas?

Thanks Paul.


Solution

  • SetCellEditor has no return value, so there is nothing there to use to call Bind. Instead you should use self.jobNumberGrid to bind the event handler. It will be called for any cell value change, but you can get the row,col of the changed cell and then either ignore it, or call self.onFinanceChange based on which column the event is coming from.