What is needed to accept a newline in a wx.grid

Support,

I am having difficulties accepting a newline in a wx.grid.
Specifically, I can not get Ctrl-Enter to work in a cell.
Ctrl-Enter exits the grid and Enter exits the cell.
event.KeyCode never returns wx.WXK_RETURN (13).
In addition, event.KeyCode never returns lowercase letters.

Using Python 2.6.4, wxPython 2.8.10.1, Windows 7

Attached is my test program.

Thanks,
Bruce

cellWrap.py (2.44 KB)

The Grid pushes a custom wx.EvtHandler on to the editor controls so it can get first dibs on the key events. The key events that it handles itself will never be seen at the editor widget. However the EvtHandler sends the event to the Grid using normal event processing, so you should be able to catch the key events directly in the grid instead of needing to intercept them from the editor. Take a look at the GridEnterHandler sample in the demo.

The other part of the problem is that on Windows the Enter key is usually treated as a navigation key like Tab, so it may be not be getting to your handlers because of that as well. If you pass style=0 to the wx.Panel then that will turn off the wx.TAB_TRAVERSAL style and turn off the processing of navigation keys.

···

On 11/20/12 3:13 PM, bruce g wrote:

Support,

I am having difficulties accepting a newline in a wx.grid.
Specifically, I can not get Ctrl-Enter to work in a cell.
Ctrl-Enter exits the grid and Enter exits the cell.
event.KeyCode never returns wx.WXK_RETURN (13).
In addition, event.KeyCode never returns lowercase letters.

Using Python 2.6.4, wxPython 2.8.10.1, Windows 7

Attached is my test program.

--
Robin Dunn
Software Craftsman

Robin, thank you for replying.

I almost have newlines working in a wx.grid cell.

I have two remaining issues.

  1. Have the grid cell expand on the fly as text is entered.
    ( I can expand the cell on the fly but the previous row is deleted when AutoRowSize is called.)
    (Logic is not in attached file.)

  2. Entering text after toggling between cells is awkward, need to left click 3 times to insert data.
    (Can be seen in attached file.)

Any suggestions to solve the above.

Thanks again,
Bruce

cellWrap1.py (4.47 KB)

···

On Wednesday, November 21, 2012 12:26:57 PM UTC-5, Robin Dunn wrote:

On 11/20/12 3:13 PM, bruce g wrote:

Support,

I am having difficulties accepting a newline in a wx.grid.

Specifically, I can not get Ctrl-Enter to work in a cell.

Ctrl-Enter exits the grid and Enter exits the cell.

event.KeyCode never returns wx.WXK_RETURN (13).

In addition, event.KeyCode never returns lowercase letters.

Using Python 2.6.4, wxPython 2.8.10.1, Windows 7

Attached is my test program.

The Grid pushes a custom wx.EvtHandler on to the editor controls so it
can get first dibs on the key events. The key events that it handles
itself will never be seen at the editor widget. However the EvtHandler
sends the event to the Grid using normal event processing, so you should
be able to catch the key events directly in the grid instead of needing
to intercept them from the editor. Take a look at the GridEnterHandler
sample in the demo.

The other part of the problem is that on Windows the Enter key is
usually treated as a navigation key like Tab, so it may be not be
getting to your handlers because of that as well. If you pass style=0
to the wx.Panel then that will turn off the wx.TAB_TRAVERSAL style and
turn off the processing of navigation keys.


Robin Dunn

Software Craftsman

http://wxPython.org

1 Like

Robin, thank you for replying.

I almost have newlines working in a wx.grid cell.

I have two remaining issues.
1) Have the grid cell expand on the fly as text is entered.
       ( I can expand the cell on the fly but the previous row is
deleted when AutoRowSize is called.)
       (Logic is not in attached file.)

You probably need to add some pixels to your GetBestSize to account for the margins and border sizes of the textctrl. Unfortunately there isn't a portable way to determine that programatically so you may need to just figure out (aka guess) a good approximation. There is some code in wx.lib.expando that may help.

2) Entering text after toggling between cells is awkward, need to
left click 3 times to insert data.
        (Can be seen in attached file.)

Have you tried using the wx.lib.mixins.grid.GridAutoEditMixin class with your grid?

···

On 11/21/12 11:39 AM, bruce g wrote:

--
Robin Dunn
Software Craftsman

Robin,

Issue #1, will solve with AutoRowSizes for now.
Issue #2, solved using wx.lib.mixins.grid.GridAutoEditMixin per your suggestion, satisfied with the grid behavior.
Also, I attached my test program with GridAutoEditMixin logic.

Thank you very much for your help,
Bruce

cellWrap4.py (5.04 KB)

···

On Monday, November 26, 2012 3:42:30 PM UTC-5, Robin Dunn wrote:

On 11/21/12 11:39 AM, bruce g wrote:

Robin, thank you for replying.

I almost have newlines working in a wx.grid cell.

I have two remaining issues.

  1. Have the grid cell expand on the fly as text is entered.
   ( I can expand the cell on the fly but the previous row is

deleted when AutoRowSize is called.)

   (Logic is not in attached file.)

You probably need to add some pixels to your GetBestSize to account for
the margins and border sizes of the textctrl. Unfortunately there isn’t
a portable way to determine that programatically so you may need to just
figure out (aka guess) a good approximation. There is some code in
wx.lib.expando that may help.

  1. Entering text after toggling between cells is awkward, need to

left click 3 times to insert data.

    (Can be seen in attached file.)

Have you tried using the wx.lib.mixins.grid.GridAutoEditMixin class with
your grid?


Robin Dunn

Software Craftsman

http://wxPython.org