How get a value from wx.grid.Grid?


I need print cell value in console when I am navigating through the cells, but I don’t know way somme time print correctly and another time not working.

import wx
import wx.grid

class GridWindow(wx.Frame):
	def __init__(self):
		wx.Frame.__init__(self, None, wx.ID_ANY, "Grid Window")

	def initialize_components(self):
		# Crear la grilla
		self.grid = wx.grid.Grid(self, wx.ID_ANY)
		self.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.imprimir_celdas, self.grid)
		# Definir el tamaño de las etiquetas de fila y columna
		# Agregar la grilla a la ventana
		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(self.grid, 1, wx.EXPAND)
		# Popula la grilla
		self.grid.CreateGrid(5, 3)
		self.grid.SetCellValue(0, 0, "Nombre")
		self.grid.SetCellValue(0, 1, "Apellidos")
		self.grid.SetCellValue(0, 2, "Edad")
		self.grid.SetCellValue(1, 0, "Juan")
		self.grid.SetCellValue(1, 1, "Perez")
		self.grid.SetCellValue(1, 2, "30")
		self.grid.SetCellValue(2, 0, "Pedro")
		self.grid.SetCellValue(2, 1, "Garcia")
		self.grid.SetCellValue(2, 2, "28")
		self.grid.SetCellValue(3, 0, "Maria")
		self.grid.SetCellValue(3, 1, "Lopez")
		self.grid.SetCellValue(3, 2, "31")
		self.grid.SetCellValue(4, 0, "Laura")
		self.grid.SetCellValue(4, 1, "Gomez")
		self.grid.SetCellValue(4, 2, "25")

	def imprimir_celdas(self, event):
		col = self.grid.GetGridCursorCol()
		row = self.grid.GetGridCursorRow()
		print(self.grid.GetCellValue(col, row))

if __name__ == '__main__':
	app = wx.App()
	grid_window = GridWindow()

first the print statement is wrong it should be:
print(self.grid.GetCellValue(row, col))
Normally, I don’t use just select. I use double click:
self.grid.Bind(wx.grid.EVT_GRID_CELL_LEFT_DCLICK, self.imprimir_celdas,)
Making my changes worked on Lunix, python 3.10.x, wxPython 4.2.x

You did not provide any information on your platform or what version of python or wxPython.


Thanks. I need use select, because is to use with keyboard, I changed print instruction, but I get the same result. I am using Windows10, Python3.9.9 and WxPython 4.2.0


    def imprimir_celdas(self, event):
        col = event.GetCol()
        row = event.GetRow()
        print(self.grid.GetCellValue(row, col))

Edit: I think what is happening is that EVT_GRID_SELECT_CELL is being triggered before the new cell has been selected, so that calling the Grid’s methods to get the row and column is returning the values for the previously selected cell, whereas the event itself contains the row and column values of the new cell.

Perhaps it is done this way so that the event could be vetoed in certain circumstances. For example, if you wanted to prevent the user from selecting a cell in the top row:

    def imprimir_celdas(self, event):
        row = event.GetRow()
        if row == 0:
            col = event.GetCol()
            print(self.grid.GetCellValue(row, col))

Tested using Python 3.10.6 + wxPython 4.2.0 gtk3 (phoenix) wxWidgets 3.2.0 on Linux Mint 21.1

not confusing row & column is sufficient :roll_eyes:

Hi Georg,
Perhaps there is a difference between Windows and Linux?

@carlos.rojas uses EVT_GRID_SELECT_CELL because he wants to use the keyboard.

If I bind EVT_GRID_SELECT_CELL to the following handler:

    def imprimir_celdas(self, event):
        print("(%d,%d) - (%d,%d)" % (self.grid.GetGridCursorRow(),

When I run the program and then press the following keys:

I get the following output:

(0,0) - (1,0)
(1,0) - (1,1)
(1,1) - (2,1)
(2,1) - (2,0)

This shows that the Grid’s methods are returning the old cell’s row and column, but the event’s methods are returning the new cell’s row and column.

What output does Windows produce for this?

that’s done on purpose :face_with_monocle:

  • the cursor can be set programmatically (see SetGridCursor)

  • in an event it’s attributes are relevant

(I have to admit the grid is better than I thought :hot_face:)