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

Frank Wilder frank.wilder at gmail.com
Mon Sep 17 19:44:51 PDT 2007


On 9/15/07, Frank Wilder <frank.wilder at gmail.com> wrote:
>
> ...
>

> I will see if I can do it at the panel or notebook level by intercepting
> the EVT_NAVIGATION_KEY events.
>
> Thanks
>   F
>
>
I got the EVT_NAVIGATION_KEY working. Here is the code.

import wx

# Set to 0 to see normal tab transversal behavior
# Set to 1 to see tab transversal stay in the notebook panel capturing the
tab at the textctrl level via EVT_KEY_DOWN
# Set to 2 to see tab transversal stay in the notebook panel capturing the
navigation at the panel level via EVT_NAVIGATION_KEY
TESTING_FIX =3D 2

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 =3D=3D 0 :
            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")

        elif TESTING_FIX =3D=3D 1 :
            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)

        elif TESTING_FIX =3D=3D 2 :
            self.text_ctrl_1 =3D wx.TextCtrl(self.panel_2, ID_tc1, "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, ID_tc3, "textctrl
3")

            self.panel_2.Bind(wx.EVT_NAVIGATION_KEY, self.OnNavigate)

        self.__do_layout()

    def OnNavigate(self, event ) :
        BACKWARD=3D0
        FORWARD=3D1

        win =3D self.FindFocus()
        id =3D win.GetId()
        tabdir =3D event.GetDirection()

        if id =3D=3D ID_tc1 and tabdir =3D=3D BACKWARD :
            # we are on the first textctrl and a shft-tab key was pressed
            self.text_ctrl_3.SetFocus()
            self.text_ctrl_3.SetSelection (-1,-1)

        elif id =3D=3D ID_tc3 and tabdir =3D=3D FORWARD :
            # we are on the last textctrl and a tab key was pressed
            self.text_ctrl_1.SetFocus()
            self.text_ctrl_1.SetSelection(-1,-1)

        else :
            # for all other textctrls, perform the panel normal navigation
            event.Skip()

    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 , 'TESTING_FIX=3D%s'=
 %
TESTING_FIX)
        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()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.wxwidgets.org/pipermail/wxpython-users/attachments/200709=
17/1c1383df/attachment.htm


More information about the wxpython-users mailing list