Setting Focus to Grid when Clicking on Grid Label

86 views
Skip to first unread message

Jay

unread,
May 24, 2012, 9:28:58 AM5/24/12
to wxpytho...@googlegroups.com
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

Mike Driscoll

unread,
May 24, 2012, 9:40:18 AM5/24/12
to wxpytho...@googlegroups.com

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

Jay

unread,
May 24, 2012, 11:43:00 AM5/24/12
to wxpytho...@googlegroups.com
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.

Mike Driscoll

unread,
May 24, 2012, 1:49:34 PM5/24/12
to wxpytho...@googlegroups.com


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

Robin Dunn

unread,
May 24, 2012, 2:14:55 PM5/24/12
to wxpytho...@googlegroups.com
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

Jay

unread,
May 24, 2012, 4:22:39 PM5/24/12
to wxpytho...@googlegroups.com
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.

Jay

unread,
May 24, 2012, 4:27:26 PM5/24/12
to wxpytho...@googlegroups.com
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

unread,
May 28, 2012, 4:19:46 PM5/28/12
to wxpytho...@googlegroups.com
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. ;-)

Jay

unread,
May 29, 2012, 11:44:00 AM5/29/12
to wxpytho...@googlegroups.com
Fair enough. I won't create a bug report (ticket).
Reply all
Reply to author
Forward
0 new messages