[wxPython-users] best way to keep tab transversal in notebook pane?

Frank Wilder frank.wilder at gmail.com
Sat Sep 15 04:58:01 PDT 2007


On 9/14/07, Robin Dunn <robin at alldunn.com> wrote:
>
> ...
>
You could try turning off the tab traversal style and
> catching the EVT_KEY_DOWN events from all the widgets and then calling
> Navigate yourself, or you can try doing it at the panel or notebook
> level by intercepting the EVT_NAVIGATION_KEY events.
> ...
>
Yes, I added the EVT_KEY_DOWN event/Navigate  code to the sample, and it
works fine, (Here it is if anyone else needs an idea on how to do it)

import wx

# Set to 0 to see normal tab transversal behavior
# Set to 1 to see tab transversal stay in the notebook panel
TESTING_FIX =3D 1

ID_tc1 =3D wx.NewId()
ID_tc3 =3D wx.NewId()

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] =3D wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.panel_1 =3D wx.Panel(self, -1)
        self.notebook_1 =3D wx.Notebook(self.panel_1, -1, style=3D0)
        self.notebook_1_pane_1 =3D wx.Panel(self.notebook_1, -1)
        self.panel_2 =3D wx.Panel(self.notebook_1_pane_1, -1)
        self.button_1 =3D wx.Button(self.panel_1, -1, "button_1")
        self.button_2 =3D wx.Button(self.panel_1, -1, "button_2")
        self.button_3 =3D wx.Button(self.panel_1, -1, "button_3")

        if TESTING_FIX :
            self.text_ctrl_1 =3D wx.TextCtrl(self.panel_2, ID_tc1, "textctrl
1", style =3D wx.TE_PROCESS_TAB)
            self.text_ctrl_2 =3D wx.TextCtrl(self.panel_2, -1, "textctrl 2")
            self.text_ctrl_3 =3D wx.TextCtrl(self.panel_2, ID_tc3, "textctrl
3", style =3D wx.TE_PROCESS_TAB)

            self.text_ctrl_1.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
            self.text_ctrl_3.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)

        else :
            self.text_ctrl_1 =3D wx.TextCtrl(self.panel_2, -1, "textctrl 1")
            self.text_ctrl_2 =3D wx.TextCtrl(self.panel_2, -1, "textctrl 2")
            self.text_ctrl_3 =3D wx.TextCtrl(self.panel_2, -1, "textctrl 3")

        self.__do_layout()

    def OnKeyDown(self, evt):

        BACKWARD=3D0
        FORWARD=3D1

        # was the tab key hit?
        if evt.GetKeyCode() =3D=3D wx.WXK_TAB :

            # find the window where the tab key was hit
            win =3D self.FindFocus()
            if win :
                id =3D  win.GetId()
            else :
                evt.Skip()
                return

            # shift tab goes backwards...was the shift key pressed too?
            if evt.ShiftDown() :
                if id =3D=3D ID_tc1 :
                    # jump to last textcontrol on panel
                    self.text_ctrl_3.SetFocus()
                    self.text_ctrl_3.SetSelection(-1,-1)
                else :
                    # move to default next textcontrol
                    win.Navigate(BACKWARD)
            else :
                # normal tab key...move forward
                if id =3D=3D ID_tc3 :
                    # jump to first textcontrol on panel
                    self.text_ctrl_1.SetFocus()
                    self.text_ctrl_1.SetSelection(-1,-1)
                else :
                    # move to default next textcontrol
                    win.Navigate(FORWARD)

    def __do_layout(self):
        sizer_1 =3D wx.BoxSizer(wx.HORIZONTAL)
        sizer_2 =3D wx.BoxSizer(wx.VERTICAL)
        sizer_4 =3D wx.BoxSizer(wx.HORIZONTAL)
        sizer_5 =3D wx.BoxSizer(wx.HORIZONTAL)
        sizer_3 =3D wx.BoxSizer(wx.HORIZONTAL)
        sizer_3.Add(self.button_1, 0, wx.ALL, 10)
        sizer_3.Add(self.button_2, 0, wx.ALL, 10)
        sizer_3.Add(self.button_3, 0, wx.ALL, 10)
        sizer_2.Add(sizer_3, 0, wx.EXPAND, 0)
        sizer_5.Add(self.text_ctrl_1, 0, wx.ALL, 10)
        sizer_5.Add(self.text_ctrl_2, 0, wx.ALL, 10)
        sizer_5.Add(self.text_ctrl_3, 0, wx.ALL, 10)
        self.panel_2.SetSizer(sizer_5)
        sizer_4.Add(self.panel_2, 1, wx.EXPAND, 0)
        self.notebook_1_pane_1.SetSizer(sizer_4)
        self.notebook_1.AddPage(self.notebook_1_pane_1, "tab 1")
        sizer_2.Add(self.notebook_1, 1, wx.ALL|wx.EXPAND, 10)
        self.panel_1.SetSizer(sizer_2)
        sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()

class MyApp(wx.App):
    def OnInit(self):
        wx.InitAllImageHandlers()
        frame_1 =3D MyFrame(None, -1, "Test Frame")
        self.SetTopWindow(frame_1)
        frame_1.Show()
        return 1

if __name__ =3D=3D "__main__":
    app =3D MyApp(0)
    app.MainLoop()


My problem is this can get tedious with a notebook that can display up to 9
different panels out of a total set of 17.

I will see if I can do it at the panel or notebook level by intercepting the
EVT_NAVIGATION_KEY events.

Thanks
  F
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200709=
15/f7b84c86/attachment.htm


More information about the wxpython-users mailing list