How to Give a ListCtrl initial focus?

I have a dialog which contains a ListCtrl. It does not get initial focus, however, if you tab, you’ll enter the list control and then up/down arrows work as expected.

I’m trying to make it so that it is initially selected, but have been unsuccessful – a call to lst.SetFocus() seems to have no effect. It’s the only thing in the dialog, so I’m not even clear what else could be in focus.

Sample code attached. I’m working on OSX, with wx2.9.

Thanks in advance for any pointers – or, if there’s a good doc that explains focus, any pointers to that.

focus.py (345 Bytes)

Hi,

I have a dialog which contains a ListCtrl. It does not get initial focus,
however, if you tab, you'll enter the list control and then up/down arrows
work as expected.

I'm trying to make it so that it is initially selected, but have been
unsuccessful -- a call to lst.SetFocus() seems to have no effect. It's the
only thing in the dialog, so I'm not even clear what else *could* be in
focus.

Sample code attached. I'm working on OSX, with wx2.9.

Thanks in advance for any pointers -- or, if there's a good doc that
explains focus, any pointers to that.

I am not in front of my Mac right now but IIRC the generic listctrl on
OSX is a composite window/ctrl. Meaning that the focus may be getting
set to the outer container window but not to the part of the window
that handles the keyboard focus. Try seeing if lst.GetChildren()
returns anything to see if there is access to any of the subwindow(s)
of the control.

Cody

···

On Thu, Jun 7, 2012 at 2:37 PM, Joel Burton <joel@joelburton.com> wrote:

Cody –

Thanks for the suggestion (and the excellent book, btw, which I read a few weeks ago). There are four children as part of the OSX list control – however, trying children.SetFocus() for each of the children still doesn’t move the initial focus onto the list in such a way that up/down arrow works.

I have discovered, though, that if the example is changed to use a wx.Frame instead of a wx.Dialog, it does work–and without having to do anything interesting to set the focus. I’d much rather use a dialog, though, unless there’s a way to get a Frame to have the same steal-control-from-the-app-until-closed UI.

Thanks!

···

On Thursday, June 7, 2012 12:45:33 PM UTC-7, Cody Precord wrote:

Hi,

On Thu, Jun 7, 2012 at 2:37 PM, Joel Burton joel@joelburton.com wrote:

I have a dialog which contains a ListCtrl. It does not get initial focus,

however, if you tab, you’ll enter the list control and then up/down arrows

work as expected.

I’m trying to make it so that it is initially selected, but have been

unsuccessful – a call to lst.SetFocus() seems to have no effect. It’s the

only thing in the dialog, so I’m not even clear what else could be in

focus.

Sample code attached. I’m working on OSX, with wx2.9.

Thanks in advance for any pointers – or, if there’s a good doc that

explains focus, any pointers to that.

I am not in front of my Mac right now but IIRC the generic listctrl on

OSX is a composite window/ctrl. Meaning that the focus may be getting

set to the outer container window but not to the part of the window

that handles the keyboard focus. Try seeing if lst.GetChildren()

returns anything to see if there is access to any of the subwindow(s)

of the control.

Cody

Do you have the listctrl in a panel in the frame? If so, put a panel in the dialog and the listctrl in the panel. Otherwise, you CAN make a Frame be Modal like a Dialog using its MakeModal() method.

  • Mike
···

On Thursday, June 7, 2012 3:11:24 PM UTC-5, Joel Burton wrote:

Cody –

Thanks for the suggestion (and the excellent book, btw, which I read a few weeks ago). There are four children as part of the OSX list control – however, trying children.SetFocus() for each of the children still doesn’t move the initial focus onto the list in such a way that up/down arrow works.

I have discovered, though, that if the example is changed to use a wx.Frame instead of a wx.Dialog, it does work–and without having to do anything interesting to set the focus. I’d much rather use a dialog, though, unless there’s a way to get a Frame to have the same steal-control-from-the-app-until-closed UI.

Thanks!

Mike –

Thanks for jumping in! The ListCtrl was already in a panel in the dialog (just like it was with the wx.Frame, where navigation works).

I’m intrigued by the MakeModal() command, but I can’t make it seem to act modal. I made a test frame with a single textctrl and then made my “modal” frame after it. However, I can still use the textctrl on the test frame. Perhaps I’m misunderstanding something about how MakeModal is supposed to work?

I’m attaching test code of the MakeModal attempt.

Thanks!

test.py (556 Bytes)

···

On Thursday, June 7, 2012 1:47:13 PM UTC-7, Mike Driscoll wrote:

Do you have the listctrl in a panel in the frame? If so, put a panel in the dialog and the listctrl in the panel. Otherwise, you CAN make a Frame be Modal like a Dialog using its MakeModal() method.

Weird. Your code works for me. I can’t interact with the first frame (titled “no”), but I can interact with the “dialog” frame. Maybe Macs don’t behave the same way as Windows in this case.

···

On Thu, Jun 7, 2012 at 3:59 PM, Joel Burton joel@joelburton.com wrote:

On Thursday, June 7, 2012 1:47:13 PM UTC-7, Mike Driscoll wrote:

Do you have the listctrl in a panel in the frame? If so, put a panel in the dialog and the listctrl in the panel. Otherwise, you CAN make a Frame be Modal like a Dialog using its MakeModal() method.

Mike –

Thanks for jumping in! The ListCtrl was already in a panel in the dialog (just like it was with the wx.Frame, where navigation works).

I’m intrigued by the MakeModal() command, but I can’t make it seem to act modal. I made a test frame with a single textctrl and then made my “modal” frame after it. However, I can still use the textctrl on the test frame. Perhaps I’m misunderstanding something about how MakeModal is supposed to work?

I’m attaching test code of the MakeModal attempt.

Thanks!

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en


Mike Driscoll

Blog: http://blog.pythonlibrary.org

Try the latest 2.9.4 preview build. (https://groups.google.com/d/topic/wxpython-dev/E_JbCS2a1gU/discussion) Your sample, with some extra code added to manage the layout with some sizers, works fine for me with or without the lst.SetFocus. (OSX 10.7, wxPython cocoa build, Python 2.7)

focus.py (555 Bytes)

···

On 6/7/12 12:37 PM, Joel Burton wrote:

I have a dialog which contains a ListCtrl. It does not get initial
focus, however, if you tab, you'll enter the list control and then
up/down arrows work as expected.

I'm trying to make it so that it is initially selected, but have been
unsuccessful -- a call to lst.SetFocus() seems to have no effect. It's
the only thing in the dialog, so I'm not even clear what else *could* be
in focus.

Sample code attached. I'm working on OSX, with wx2.9.

Thanks in advance for any pointers -- or, if there's a good doc that
explains focus, any pointers to that.

--
Robin Dunn
Software Craftsman