Tab behaviour

I have two questions about tab behaviour in wxPython:

  1. Is it possible to set a control as not being able to receive focus from a tab event?
  2. Is it possible to find out which is the next control in the tab order? For example, I have an application with a ListCtrl, and I want the control to be able to receive focus from a tab event only if there are rows in the ListCtrl, and pass focus to the next control in the tab order if the ListCtrl is empty. I know that I can put the logic in the EVT_SET_FOCUS event of the ListCtrl, but I don’t know how to programmatically figure out the next control in the tab order.
    I’ve done some googling prior to writing this post, but I can’t find any answers to these questions. Thanks in advance.

It might not be the best idea, but you could listen to the TAB key and set focus on the next control as you wish.

self.Bind(wx.EVT_KEY_DOWN, self.OnKey)

def OnKey (self, event=None):
key = event.GetKeyCode()
if key == wx.WXK_TAB:
next_control.SetFocus()

If you are going this way, I recommend making a list of the controls you would like to receive focus, in the correct order, and just calling the next one when the tab key is pressed.

But it’s like a “brute force” approach.

mercado wrote:

I have two questions about tab behaviour in wxPython:

   1. Is it possible to set a control as not being able to receive
      focus from a tab event?
   2. Is it possible to find out which is the next control in the tab
      order? For example, I have an application with a ListCtrl, and
      I want the control to be able to receive focus from a tab event
      only if there are rows in the ListCtrl, and pass focus to the
      next control in the tab order if the ListCtrl is empty. I know
      that I can put the logic in the EVT_SET_FOCUS event of the
      ListCtrl, but I don't know how to programmatically figure out
      the next control in the tab order.

I've done some googling prior to writing this post, but I can't find any answers to these questions. Thanks in advance.

Tab order is usually set to the order that the widgets are created. So you can create them in the order you want to tab to, and then disable the ListCtrl when it doesn't have any rows. Disabled controls don't accept focus. When you add a row, just enable the control.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Mercado,

Thanks for the reply Mike. That's the best response I've heard so far.

No problem. I think there's a way to change the order at run time too. Ah-ha! Here's some links:

http://wiki.wxpython.org/Getting%20Started
http://www.nabble.com/How-to-get-next-prev-control-in-tab-order--td17933635.html

Have fun!

- Mike

···

On Tue, Mar 10, 2009 at 10:32 AM, Mike Driscoll > <mike@pythonlibrary.org <mailto:mike@pythonlibrary.org>> wrote:

    mercado wrote:

        I have two questions about tab behaviour in wxPython:

          1. Is it possible to set a control as not being able to receive

             focus from a tab event?
          2. Is it possible to find out which is the next control in
        the tab

             order? For example, I have an application with a
        ListCtrl, and
             I want the control to be able to receive focus from a tab
        event
             only if there are rows in the ListCtrl, and pass focus to the
             next control in the tab order if the ListCtrl is empty.
         I know
             that I can put the logic in the EVT_SET_FOCUS event of the
             ListCtrl, but I don't know how to programmatically figure out
             the next control in the tab order.

        I've done some googling prior to writing this post, but I
        can't find any answers to these questions. Thanks in advance.

    Tab order is usually set to the order that the widgets are
    created. So you can create them in the order you want to tab to,
    and then disable the ListCtrl when it doesn't have any rows.
    Disabled controls don't accept focus. When you add a row, just
    enable the control.

    -------------------
    Mike Driscoll

    Blog: http://blog.pythonlibrary.org

    _______________________________________________
    wxpython-users mailing list
    wxpython-users@lists.wxwidgets.org
    <mailto:wxpython-users@lists.wxwidgets.org>
    http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

For what it's worth, Boa Constructor has an easy way to change tab order
for the generated controls it makes. (right-click in Designer, select
"Creation/Tab Order").

Che

···

On Tue, Mar 10, 2009 at 10:32 AM, Mike Driscoll <mike@pythonlibrary.org> wrote:

mercado wrote:

I have two questions about tab behaviour in wxPython:

1. Is it possible to set a control as not being able to receive
focus from a tab event?
2. Is it possible to find out which is the next control in the tab
order? For example, I have an application with a ListCtrl, and
I want the control to be able to receive focus from a tab event
only if there are rows in the ListCtrl, and pass focus to the
next control in the tab order if the ListCtrl is empty. I know
that I can put the logic in the EVT_SET_FOCUS event of the
ListCtrl, but I don't know how to programmatically figure out
the next control in the tab order.