How do I implement single click editing on cell in a table in wxPython?

32 Views Asked by At

I have a table where I originally had the issue of needing to click 3 times on a cell in order for it to show the dropdown / combobox list of devices I need it to display.

After some chatgpt, I managed to get it to work with double click as seen here:

def on_single_click_edit(self, event):
    row = event.GetRow()
    col = event.GetCol()

    # Move the grid cursor to the clicked cell
    self.rear_device_grid.SetGridCursor(row, col)
    self.front_device_grid.SetGridCursor(row, col)

    # Enable the editor for the current cell
    self.rear_device_grid.EnableCellEditControl()
    self.front_device_grid.EnableCellEditControl()

    # Force the grid to start the editor
    self.rear_device_grid.ShowCellEditControl()
    self.front_device_grid.ShowCellEditControl()

    # Skip the event to allow default processing
    event.Skip()

Here is the code of me creating the table(s):

 # Creating rear device table
    self.rear_device_grid = wx.grid.Grid(self)
    self.rear_device_grid.CreateGrid(8, 1)
    self.rear_device_grid.SetDefaultColSize(130, resizeExistingCols=False)
    self.rear_device_grid.SetColLabelValue(0, "Rear Device Slots")

    # Set the editor for "Device" column to be a choice editor with module list
    module_choice_editor = wx.grid.GridCellChoiceEditor(self.module_list, allowOthers=False)
    for row in range(self.rear_device_grid.GetNumberRows()):
        self.rear_device_grid.SetCellEditor(row, 0, module_choice_editor)

    grid.Add(self.rear_device_grid, pos=(7,0), span=(3,3))

    self.front_device_grid = wx.grid.Grid(self)
    self.front_device_grid.CreateGrid(5, 1)
    self.front_device_grid.SetDefaultColSize(130, resizeExistingCols=False)
    self.front_device_grid.SetColLabelValue(0, "Front Device Slots")
    self.front_device_grid.SetRowLabelValue(4, "Crosspoint:")

    # Change row labels of front device from 1-4 to 9-12
    start_label = 9
    for i in range(4): # Loop through first 4 rows
        self.front_device_grid.SetRowLabelValue(i, str(start_label + i))

    # Set the editor for "Device" column to be a choice editor with module list
    for row in range(self.front_device_grid.GetNumberRows() - 1):  # Exclude the last row
        self.front_device_grid.SetCellEditor(row, 0, module_choice_editor)

    # Set the editor for the last row "Crosspoint:" to be a choice editor with crosspoint list
    crosspoint_choice_editor = wx.grid.GridCellChoiceEditor(self.crosspoint_list, allowOthers=False)
    self.front_device_grid.SetCellEditor(4, 0, crosspoint_choice_editor)  # Last row for crosspoint

    grid.Add(self.front_device_grid, pos=(10,0), span=(3,3))
0

There are 0 best solutions below