Setting Focus to Grid when Clicking on Grid Label

Hi wxPython users:

I’m not sure if this is a feature, or a bug. I have a Grid control and TextCtrl on the same panel. I expect the focus to shift to the Grid control when the user clicks on a row or column label, but it does not. Keyboard operations are still directed to the last active control, which is a TextCtrl in my case. As a result, a Grid row is highlighted, but keystrokes that might be necessary to operate on the Grid (e.g. CTRL-C to copy a row) are directed to the TextCtrl. The user does not expect this and is shocked to see the text control changed when they thought they were working on the Grid control.

This issue has been reported before on this group: https://groups.google.com/forum/?fromgroups#!searchin/wxpython-users/grid$20set$20focus/wxpython-users/QL0lKmY2LoM/mM8zkG-LOQMJ

I have attached the code from the aforementioned post, modified just slightly, to reproduce the problem. Run App1.py and note the TextCtrl has the focus. Click a grid row label and note the TextCtrl still has focus. Now press an alpha-numeric key and observe the character is inserted in the TextCtrl.

To work around the “problem”, I call Grid.SetFocus() in mouse click event handlers. To see this, uncomment the code beneath the UNCOMMENT TO FIX comment.

Is this a feature or a bug?

Environment: Windows XP, wxPython 2.8.12.1, Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32

Thanks.

App1.py (3.47 KB)

Well, you disabled editing of the Grid, so I’m not sure what you expected would happen. You certainly can’t edit something that you’ve disabled. If you pass True to self.grid1.EnableEditing, then it works the way it ought to.

  • Mike
···

On Thursday, May 24, 2012 8:28:58 AM UTC-5, Jay wrote:

Hi wxPython users:

I’m not sure if this is a feature, or a bug. I have a Grid control and TextCtrl on the same panel. I expect the focus to shift to the Grid control when the user clicks on a row or column label, but it does not. Keyboard operations are still directed to the last active control, which is a TextCtrl in my case. As a result, a Grid row is highlighted, but keystrokes that might be necessary to operate on the Grid (e.g. CTRL-C to copy a row) are directed to the TextCtrl. The user does not expect this and is shocked to see the text control changed when they thought they were working on the Grid control.

This issue has been reported before on this group: https://groups.google.com/forum/?fromgroups#!searchin/wxpython-users/grid$20set$20focus/wxpython-users/QL0lKmY2LoM/mM8zkG-LOQMJ

I have attached the code from the aforementioned post, modified just slightly, to reproduce the problem. Run App1.py and note the TextCtrl has the focus. Click a grid row label and note the TextCtrl still has focus. Now press an alpha-numeric key and observe the character is inserted in the TextCtrl.

To work around the “problem”, I call Grid.SetFocus() in mouse click event handlers. To see this, uncomment the code beneath the UNCOMMENT TO FIX comment.

Is this a feature or a bug?

Environment: Windows XP, wxPython 2.8.12.1, Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32

Thanks.

I modified the code, but still get the same result: click on a grid label, press any character key, and the text control handles that character event, not the Grid.

···

On Thursday, May 24, 2012 11:10:18 AM UTC-2:30, Mike Driscoll wrote:

On Thursday, May 24, 2012 8:28:58 AM UTC-5, Jay wrote:

Hi wxPython users:

I’m not sure if this is a feature, or a bug. I have a Grid control and TextCtrl on the same panel. I expect the focus to shift to the Grid control when the user clicks on a row or column label, but it does not. Keyboard operations are still directed to the last active control, which is a TextCtrl in my case. As a result, a Grid row is highlighted, but keystrokes that might be necessary to operate on the Grid (e.g. CTRL-C to copy a row) are directed to the TextCtrl. The user does not expect this and is shocked to see the text control changed when they thought they were working on the Grid control.

This issue has been reported before on this group: https://groups.google.com/forum/?fromgroups#!searchin/wxpython-users/grid$20set$20focus/wxpython-users/QL0lKmY2LoM/mM8zkG-LOQMJ

I have attached the code from the aforementioned post, modified just slightly, to reproduce the problem. Run App1.py and note the TextCtrl has the focus. Click a grid row label and note the TextCtrl still has focus. Now press an alpha-numeric key and observe the character is inserted in the TextCtrl.

To work around the “problem”, I call Grid.SetFocus() in mouse click event handlers. To see this, uncomment the code beneath the UNCOMMENT TO FIX comment.

Is this a feature or a bug?

Environment: Windows XP, wxPython 2.8.12.1, Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32

Thanks.

Well, you disabled editing of the Grid, so I’m not sure what you expected would happen. You certainly can’t edit something that you’ve disabled. If you pass True to self.grid1.EnableEditing, then it works the way it ought to.

  • Mike

Um, why are you clicking on a label? I clicked on a cell and it worked just fine. Clicking on a label won’t work. I suppose you could catch the focus event and transfer the focus to the first cell if that’s what you want.

  • Mike
···

On Thursday, May 24, 2012 10:43:00 AM UTC-5, Jay wrote:

I modified the code, but still get the same result: click on a grid label, press any character key, and the text control handles that character event, not the Grid.

You already have the solution commented out in the code. Explicitly set the focus to the grid when you get EVT_GRID_LABEL_LEFT_CLICK events. That handler should also call event.Skip() however so the grid can still process that event itself.

···

On 5/24/12 8:43 AM, Jay wrote:

I modified the code, but still get the same result: click on a grid
label, press any character key, and the text control handles that
character event, not the Grid.

--
Robin Dunn
Software Craftsman

I click on row labels to select rows, then handle keystrokes to operate on these rows, e.g. ctrl-c to copy a row and ctrl-v to paste it. Users might never click in a cell, but because the row is highlighted/selected by clicking on the label, they will try to issue keystroke commands such as ctrl-c, and these would fail, or worse muck up another control’s value.

I know how to work around it, as commented out in the code. (thanks for the suggestion anyway). I just got caught me off guard when the Grid didn’t set the focus automatically, so I was wondering if this was a bug or not.

···

On Thursday, May 24, 2012 3:19:34 PM UTC-2:30, Mike Driscoll wrote:

On Thursday, May 24, 2012 10:43:00 AM UTC-5, Jay wrote:

I modified the code, but still get the same result: click on a grid label, press any character key, and the text control handles that character event, not the Grid.

Um, why are you clicking on a label? I clicked on a cell and it worked just fine. Clicking on a label won’t work. I suppose you could catch the focus event and transfer the focus to the first cell if that’s what you want.

  • Mike

I know. I was just wondering if this is a bug, or desired behavior. See my previous post https://groups.google.com/d/msg/wxpython-users/h87HDZVivtw/YuvuRt4fedsJ, for why I thought it might be a bug.

···

On Thursday, May 24, 2012 3:44:55 PM UTC-2:30, Robin Dunn wrote:

On 5/24/12 8:43 AM, Jay wrote:

I modified the code, but still get the same result: click on a grid

label, press any character key, and the text control handles that

character event, not the Grid.

You already have the solution commented out in the code. Explicitly set
the focus to the grid when you get EVT_GRID_LABEL_LEFT_CLICK events.
That handler should also call event.Skip() however so the grid can still
process that event itself.


Robin Dunn

Software Craftsman

http://wxPython.org

Sorry, I guess I didn't read that message closely enough and just jumped to the sample code. My guess is that it is somewhere between a bug and a desired behavior. An "it is what it is" type of thing. :wink:

···

On 5/24/12 1:27 PM, Jay wrote:

I know. I was just wondering if this is a bug, or desired behavior. See
my previous post
https://groups.google.com/d/msg/wxpython-users/h87HDZVivtw/YuvuRt4fedsJ,
for why I thought it might be a bug.

--
Robin Dunn
Software Craftsman

Fair enough. I won’t create a bug report (ticket).

···

On Monday, May 28, 2012 5:49:46 PM UTC-2:30, Robin Dunn wrote:

On 5/24/12 1:27 PM, Jay wrote:

I know. I was just wondering if this is a bug, or desired behavior. See

my previous post

https://groups.google.com/d/msg/wxpython-users/h87HDZVivtw/YuvuRt4fedsJ,

for why I thought it might be a bug.

Sorry, I guess I didn’t read that message closely enough and just jumped
to the sample code. My guess is that it is somewhere between a bug and
a desired behavior. An “it is what it is” type of thing. :wink:


Robin Dunn

Software Craftsman

http://wxPython.org