wxGrid selection functions all broken?

I'm using wx.grid.Grid() without a table.

In an event handler I'm trying to get info on the selection:

print 'Cells:',self.grid_ctrl.GetSelectedCells()
print 'Rows:',self.grid_ctrl.GetSelectedRows()

I always get [] back. Is this a bug or am I doing something
wrong?

Also I wanted to experiment with the selection mode but
cannot find the symbols like wxGridSelectRows for
wxGrid::SetSelectionMode.

I looked in wx and wx.grid modules.

Barry

Hi, There,
I tried to run wxPythonWIN32-2.4.2.4-Py23.exe on my Win 2000. I got the
NTVDM CPU has encountered an illegal instruction CS:0581, IP:0199
Choose 'Close' to teminate the applicaiton. Instruction will be =
apprciated. Thanks
Charles

Barry Scott wrote:

I'm using wx.grid.Grid() without a table.

In an event handler I'm trying to get info on the selection:

print 'Cells:',self.grid_ctrl.GetSelectedCells()
print 'Rows:',self.grid_ctrl.GetSelectedRows()

I always get back. Is this a bug or am I doing something
wrong?

IIRC, those only return rows/cols that are fully selected, such as with dragging the mouse across the row labels.

Also I wanted to experiment with the selection mode but
cannot find the symbols like wxGridSelectRows for
wxGrid::SetSelectionMode.

I looked in wx and wx.grid modules.

They are members of the Grid class:

  wx.grid.Grid.GridSelectCells

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

cguan@ameritech.net wrote:

Hi, There,
I tried to run wxPythonWIN32-2.4.2.4-Py23.exe on my Win 2000. I got the
NTVDM CPU has encountered an illegal instruction CS:0581, IP:0199 Choose 'Close' to teminate the applicaiton. Instruction will be =
apprciated. Thanks

Perhaps your download was corrupted? Please try downloading it again.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Are you saying that GetSelectedCells() only works if full cols
or rows are selected? Surely that's not useful?

Even when I select all cells in a row GetSelectedRows() returns
.

Are you sure these functions work?

I'll try and work up a small example to show the problem.

Barry

···

At 20-11-2003 00:43, Robin Dunn wrote:

Barry Scott wrote:

I'm using wx.grid.Grid() without a table.
In an event handler I'm trying to get info on the selection:
print 'Cells:',self.grid_ctrl.GetSelectedCells()
print 'Rows:',self.grid_ctrl.GetSelectedRows()
I always get back. Is this a bug or am I doing something
wrong?

IIRC, those only return rows/cols that are fully selected, such as with dragging the mouse across the row labels.

Here is an example that demonstrate the problem. Run in a console
and see the print out when you right click on a cell.

BArry

import wx
import wx.grid

class App(wx.App):
  def __init__( self ):
    self.frame = None

    wx.App.__init__( self, 0 )

  def OnInit(self):
    self.frame = Frame()
    self.frame.Show( wx.true )
    self.SetTopWindow( self.frame )
    return wx.true

class Frame(wx.Frame):
  def __init__( self ):
    wx.Frame.__init__(self, wx.NULL, -1, 'Grid Test',
        wx.DefaultPosition,
        wx.DefaultSize,
        wx.DEFAULT_FRAME_STYLE )

    self.grid_panel = wx.Panel(self, -1)
    self.grid_id = wx.NewId()
    g = self.grid_ctrl = wx.grid.Grid( self.grid_panel, self.grid_id,
            wx.DefaultPosition,
            wx.Size( 400, 300 ),
            wx.LC_REPORT|wx.NO_BORDER )

    g.CreateGrid( 2, 3 )
    g.SetSelectionMode( 1 )
    g.SetColLabelAlignment( wx.ALIGN_LEFT, wx.ALIGN_BOTTOM )
    g.SetColLabelSize( 20 )
    g.SetRowLabelSize( 0 )

    g.SetColLabelValue( 0, 'Name' )
    g.SetColLabelValue( 1, 'State' )
    g.SetColLabelValue( 2, 'Type' )

    g.SetCellValue( 0, 0, 'name 0' )
    g.SetCellValue( 1, 0, 'name 1' )
    g.SetCellValue( 0, 1, 'state 0' )
    g.SetCellValue( 1, 1, 'state 1' )
    g.SetCellValue( 0, 2, 'type 0' )
    g.SetCellValue( 1, 2, 'type 1' )

    g.SetReadOnly( 0, 0, wx.false )
    g.SetReadOnly( 1, 0, wx.false )
    g.SetReadOnly( 0, 1, wx.false )
    g.SetReadOnly( 1, 1, wx.false )
    g.SetReadOnly( 0, 2, wx.false )
    g.SetReadOnly( 1, 2, wx.false )

    wx.grid.EVT_GRID_CELL_RIGHT_CLICK( g, self.OnGridCellRightClick )

  def OnGridCellRightClick( self, event ):
    print 'OnGridCellRightClick --------------------'
    print 'Cells:',self.grid_ctrl.GetSelectedCells()
    print 'Rows:',self.grid_ctrl.GetSelectedRows()
    print 'Cols:',self.grid_ctrl.GetSelectedCols()

def main():
  app = App()
  app.frame.Show( 1 )
  app.MainLoop()

if __name__ == '__main__':
  main()

I have same troubles too. Finally, I've solved my problem using

topleft = self.GetSelectionBlockTopLeft()
bottomright = self.GetSelectionBlockBottomRight()

and then doing some post processing over the row data to get some business data.

Barry Scott wrote:

Barry Scott wrote:

I'm using wx.grid.Grid() without a table.
In an event handler I'm trying to get info on the selection:
print 'Cells:',self.grid_ctrl.GetSelectedCells()
print 'Rows:',self.grid_ctrl.GetSelectedRows()
I always get back. Is this a bug or am I doing something
wrong?

IIRC, those only return rows/cols that are fully selected, such as with dragging the mouse across the row labels.

Are you saying that GetSelectedCells() only works if full cols
or rows are selected? Surely that's not useful?

No, I was refering to the Rows/Cols methods. Sorry for the confusion.

Even when I select all cells in a row GetSelectedRows() returns
.

Are you sure these functions work?

Yes, they return the rows or cols that are selected by clicking/dragging > Here is an example that demonstrate the problem. Run in a console
> and see the print out when you right click on a cell.

Comment out the SetSelectionMode and SetRowLabelSize calls and you'll see that the GetSelectedCells and GetSelectedRows will return values if you click or drag on the labels.

GetSelectedCells returns a list of cells that are selected individually, usually by Ctrl-Clicking on individual cells.

For one or more blocks of selected cells selected with click-drag or Ctrl-click-drag on the cells we have GetSelectionBlockTopLeft and
GetSelectionBlockBottomRight.

It's possible to have a combination of selection types selected at the same time when using the Ctrl key and the various clicking and dragging methods, and the various Get methods will show that. For example:

OnGridCellRightClick --------------------
Cells: [(6, 0), (8, 0)]
Rows: [2]
Cols: [2]
TopLeft: [(10, 0), (14, 0)]
BotRt: [(11, 1), (16, 0)]

gridsel2.py (2.24 KB)

···

At 20-11-2003 00:43, Robin Dunn wrote:

on the row or col labels. Barry Scott wrote:

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Barry Scott <barry@barrys-emacs.org> writes:

    wx.grid.EVT_GRID_CELL_RIGHT_CLICK( g, self.OnGridCellRightClick )

Hmmm... I think you should pass on a different 'g' here. It leads you
to one conclusion that is not what I saw happening in the code...

What I use is:

class MyFrame(wxFrame):

    def __init__(self, *args, **kwds):
        (...)

        self.__associate_events()

    def __associate_events(self):
        (...)
        EVT_GRID_CELL_RIGHT_CLICK(self, self.OnMouseRightDown)
        (...)

Then I can get the correct row and col position with event.GetRow()
and event.GetCol() (this is your problem, I think: you were trying to
get information that was contained in the event from the class).

  def OnGridCellRightClick( self, event ):
    print 'OnGridCellRightClick --------------------'
    print 'Cells:',self.grid_ctrl.GetSelectedCells()
    print 'Rows:',self.grid_ctrl.GetSelectedRows()
    print 'Cols:',self.grid_ctrl.GetSelectedCols()

You should be trying to get these from the event shouldn't you?

Here's your own code rewritten so that it shows something:

···

----------------------------------------------------------------------
import wx
import wx.grid

class App(wx.App):
  def __init__( self ):
    self.frame = None

    wx.App.__init__( self, 0 )

  def OnInit(self):
    self.frame = Frame()
    self.frame.Show( wx.true )
    self.SetTopWindow( self.frame )
    return wx.true

class Frame(wx.Frame):
  def __init__( self ):
    wx.Frame.__init__(self, wx.NULL, -1, 'Grid Test',
        wx.DefaultPosition,
        wx.DefaultSize,
        wx.DEFAULT_FRAME_STYLE )

    self.grid_panel = wx.Panel(self, -1)
    self.grid_id = wx.NewId()
    g = self.grid_ctrl = wx.grid.Grid( self.grid_panel, self.grid_id,
            wx.DefaultPosition,
            wx.Size( 400, 300 ),
            wx.LC_REPORT|wx.NO_BORDER )

    g.CreateGrid( 2, 3 )
    g.SetSelectionMode( 1 )
    g.SetColLabelAlignment( wx.ALIGN_LEFT, wx.ALIGN_BOTTOM )
    g.SetColLabelSize( 20 )
    g.SetRowLabelSize( 0 )

    g.SetColLabelValue( 0, 'Name' )
    g.SetColLabelValue( 1, 'State' )
    g.SetColLabelValue( 2, 'Type' )

    g.SetCellValue( 0, 0, 'name 0' )
    g.SetCellValue( 1, 0, 'name 1' )
    g.SetCellValue( 0, 1, 'state 0' )
    g.SetCellValue( 1, 1, 'state 1' )
    g.SetCellValue( 0, 2, 'type 0' )
    g.SetCellValue( 1, 2, 'type 1' )

    g.SetReadOnly( 0, 0, wx.false )
    g.SetReadOnly( 1, 0, wx.false )
    g.SetReadOnly( 0, 1, wx.false )
    g.SetReadOnly( 1, 1, wx.false )
    g.SetReadOnly( 0, 2, wx.false )
    g.SetReadOnly( 1, 2, wx.false )

    wx.grid.EVT_GRID_CELL_RIGHT_CLICK( g, self.OnGridCellRightClick )

  def OnGridCellRightClick( self, event ):
                """ Added the print statement for the event and
                    the event.Method statements."""
    print 'Events:', dir(event)
    print 'OnGridCellRightClick --------------------'
    print 'Cells:',self.grid_ctrl.GetSelectedCells(), event.GetSelection()
    print 'Rows:',self.grid_ctrl.GetSelectedRows(), event.GetRow()
    print 'Cols:',self.grid_ctrl.GetSelectedCols(), event.GetCol()

def main():
  app = App()
  app.frame.Show( 1 )
  app.MainLoop()

if __name__ == '__main__':
  main()

----------------------------------------------------------------------

The output should look like this:

----------------------------------------------------------------------
Events: ['Allow', 'AltDown', 'Checked', 'Clone', 'ControlDown', 'Destroy', 'GetClassName', 'GetClientData', 'GetCol', 'GetEventObject', 'GetEventType', 'GetExtraLong', 'GetId', 'GetInt', 'GetPosition', 'GetRow', 'GetSelection', 'GetSkipped', 'GetString', 'GetTimestamp', 'IsAllowed', 'IsChecked', 'IsSelection', 'MetaDown', 'Selecting', 'SetEventObject', 'SetEventType', 'SetExtraLong', 'SetId', 'SetInt', 'SetString', 'SetTimestamp', 'ShiftDown', 'Skip', 'Veto', '__del__', '__doc__', '__init__', '__module__', '__repr__', 'this', 'thisown']
OnGridCellRightClick --------------------
Cells: 0
Rows: 1
Cols: 0
----------------------------------------------------------------------

I hope I understood you correctly.

--
Godoy. <godoy@metalab.unc.edu>

Thanks for your help. But I cannot use the technique you propose.

Barry Scott <barry@barrys-emacs.org> writes:

> wx.grid.EVT_GRID_CELL_RIGHT_CLICK( g, self.OnGridCellRightClick )

Hmmm... I think you should pass on a different 'g' here. It leads you
to one conclusion that is not what I saw happening in the code...

Why can't I trap the event on the ctrl that sends it?

What I use is:

class MyFrame(wxFrame):

    def __init__(self, *args, **kwds):
        (...)

        self.__associate_events()

    def __associate_events(self):
        (...)
        EVT_GRID_CELL_RIGHT_CLICK(self, self.OnMouseRightDown)
        (...)

Then I can get the correct row and col position with event.GetRow()
and event.GetCol() (this is your problem, I think: you were trying to
get information that was contained in the event from the class).

I'm do not what the data from the event. It only tells me about one
cell. If the user used Ctrl-Click to select Rows 1, 3, 7 how would
the event tell me that?

> def OnGridCellRightClick( self, event ):
> print 'OnGridCellRightClick --------------------'
> print 'Cells:',self.grid_ctrl.GetSelectedCells()
> print 'Rows:',self.grid_ctrl.GetSelectedRows()
> print 'Cols:',self.grid_ctrl.GetSelectedCols()

You should be trying to get these from the event shouldn't you?

No the event only knows the cell that the rigth click is on.

···

At 23-11-2003 12:07, you wrote:

Here's your own code rewritten so that it shows something:

----------------------------------------------------------------------
import wx
import wx.grid

class App(wx.App):
        def __init__( self ):
                self.frame = None

                wx.App.__init__( self, 0 )

        def OnInit(self):
                self.frame = Frame()
                self.frame.Show( wx.true )
                self.SetTopWindow( self.frame )
                return wx.true

class Frame(wx.Frame):
        def __init__( self ):
                wx.Frame.__init__(self, wx.NULL, -1, 'Grid Test',
                                wx.DefaultPosition,
                                wx.DefaultSize,
                                wx.DEFAULT_FRAME_STYLE )

                self.grid_panel = wx.Panel(self, -1)
                self.grid_id = wx.NewId()
                g = self.grid_ctrl = wx.grid.Grid( self.grid_panel, self.grid_id,
                                                wx.DefaultPosition,
                                                wx.Size( 400, 300 ),
                                                wx.LC_REPORT|wx.NO_BORDER )

                g.CreateGrid( 2, 3 )
                g.SetSelectionMode( 1 )
                g.SetColLabelAlignment( wx.ALIGN_LEFT, wx.ALIGN_BOTTOM )
                g.SetColLabelSize( 20 )
                g.SetRowLabelSize( 0 )

                g.SetColLabelValue( 0, 'Name' )
                g.SetColLabelValue( 1, 'State' )
                g.SetColLabelValue( 2, 'Type' )

                g.SetCellValue( 0, 0, 'name 0' )
                g.SetCellValue( 1, 0, 'name 1' )
                g.SetCellValue( 0, 1, 'state 0' )
                g.SetCellValue( 1, 1, 'state 1' )
                g.SetCellValue( 0, 2, 'type 0' )
                g.SetCellValue( 1, 2, 'type 1' )

                g.SetReadOnly( 0, 0, wx.false )
                g.SetReadOnly( 1, 0, wx.false )
                g.SetReadOnly( 0, 1, wx.false )
                g.SetReadOnly( 1, 1, wx.false )
                g.SetReadOnly( 0, 2, wx.false )
                g.SetReadOnly( 1, 2, wx.false )

                wx.grid.EVT_GRID_CELL_RIGHT_CLICK( g, self.OnGridCellRightClick )

        def OnGridCellRightClick( self, event ):
                """ Added the print statement for the event and
                    the event.Method statements."""
                print 'Events:', dir(event)
                print 'OnGridCellRightClick --------------------'
                print 'Cells:',self.grid_ctrl.GetSelectedCells(), event.GetSelection()
                print 'Rows:',self.grid_ctrl.GetSelectedRows(), event.GetRow()
                print 'Cols:',self.grid_ctrl.GetSelectedCols(), event.GetCol()

def main():
        app = App()
        app.frame.Show( 1 )
        app.MainLoop()

if __name__ == '__main__':
        main()

----------------------------------------------------------------------

The output should look like this:

----------------------------------------------------------------------
Events: ['Allow', 'AltDown', 'Checked', 'Clone', 'ControlDown', 'Destroy', 'GetClassName', 'GetClientData', 'GetCol', 'GetEventObject', 'GetEventType', 'GetExtraLong', 'GetId', 'GetInt', 'GetPosition', 'GetRow', 'GetSelection', 'GetSkipped', 'GetString', 'GetTimestamp', 'IsAllowed', 'IsChecked', 'IsSelection', 'MetaDown', 'Selecting', 'SetEventObject', 'SetEventType', 'SetExtraLong', 'SetId', 'SetInt', 'SetString', 'SetTimestamp', 'ShiftDown', 'Skip', 'Veto', '__del__', '__doc__', '__init__', '__module__', '__repr__', 'this', 'thisown']
OnGridCellRightClick --------------------
Cells: 0
Rows: 1
Cols: 0
----------------------------------------------------------------------

I hope I understood you correctly.

--
Godoy. <godoy@metalab.unc.edu>

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwindows.org
For additional commands, e-mail: wxPython-users-help@lists.wxwindows.org

Barry Scott wrote:

Thanks for your help. But I cannot use the technique you propose.

Barry Scott <barry@barrys-emacs.org> writes:

> wx.grid.EVT_GRID_CELL_RIGHT_CLICK( g, self.OnGridCellRightClick )

Hmmm... I think you should pass on a different 'g' here. It leads you
to one conclusion that is not what I saw happening in the code...

Yeah, that should be okay.

···

At 23-11-2003 12:07, you wrote:

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!