Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

python - wxpython toolbarmenu keyboard navigation

I've been working on an application for blind people, so I decided to python and wxPython for the GUI. The problem is that I cannot navigate the wx.toolbar using tabs. This is my actual code.

    menuToolBar = self.CreateToolBar(style=wx.TB_FLAT)
    menuToolBar.AddSeparator()
    menuToolBar.AddTool(ID_NUEVO, "Nuevo Archivo", wx.Bitmap(
        os.path.join(self.__rutaPrincipal, "img/nueva pagina.png")), "Nuevo Archivo")
    menuToolBar.AddSeparator()
    menuToolBar.AddTool(ID_GUARDAR_ARCHIVO, "Guardar Archivo", wx.Bitmap(
        os.path.join(self.__rutaPrincipal, "img/guardar pagina.png")),  "Guardar Archivo")
    menuToolBar.AddTool(ID_ABRIR_ARCHIVO, "Abrir Página", wx.Bitmap(
        os.path.join(self.__rutaPrincipal, "img/abrir pagina.png")), "Abrir Página")
    menuToolBar.AddSeparator()
    menuToolBar.AddTool(ID_FOCUS_PANEL_ESCRITURA, "Foco Escritura", wx.Bitmap(
        os.path.join(self.__rutaPrincipal, "img/panel escritura.png")), "Foco Escritura")
    menuToolBar.AddTool(ID_FOCUS_PANEL_RESULTADO, "Foco Resultado", wx.Bitmap(
        os.path.join(self.__rutaPrincipal, "img/panel resultado.png")), "Foco Resultado")
    menuToolBar.AddSeparator()
    menuToolBar.AddTool(ID_CERRAR_PAGINA, "Cerrar Página", wx.Bitmap(
        os.path.join(self.__rutaPrincipal, "img/cerrar pagina.png")), "Cerrar Página")
    menuToolBar.AddSeparator()

    menuToolBar.Realize()

is there any configuration to achieve it? or maybe is there another component that help me?. P.D. I am new to python development and Stackoverflow

question from:https://stackoverflow.com/questions/66055835/wxpython-toolbarmenu-keyboard-navigation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Rather than delete the existing anwser, which in fact answers a different question, I'm leaving it in place because I personally believe it's useful, in a related way.

Here is an example of what I was referring to in my comment, about making your own toolbar, which you can navigate using Tab, Left and Right Arrow keys.
It should be enough to get you started.

It uses a normal button and some Generic bitmap buttons, both will work, as we need to illustrate which button currently has focus, which is achieved by using at least 2 different bitmaps for each button. One Normal and one for Focus.

import wx
import wx.lib.buttons as buttons

Valid_keys = [wx.WXK_F1,wx.WXK_RETURN,wx.WXK_TAB,wx.WXK_LEFT,wx.WXK_RIGHT]

class MainFrame(wx.Frame):

#----------------------------------------------------------------------
    def __init__(self):

        self.funcs={"Button1":self.OnButton1,
                    "Button2":self.OnButton2,
                    "Button3":self.OnButton3,
                    "Button4":self.OnButton4}
        
        wx.Frame.__init__(self, None, title="Self defined Toolbar", size=(400,400))

        tool_panel = wx.Panel(self,-1,name="tool_panel")
        tool_panel.SetBackgroundColour("lightgreen")
        tool_panel.SetToolTip("Press F1 or Tab to Access Toolbar")

        panel = wx.Panel(self,-1,size=(400,300), name="Main_panel")
        panel.SetBackgroundColour("lightblue")

        bmp = wx.Bitmap("Discord.png", wx.BITMAP_TYPE_ANY)
        bmp1 = wx.Bitmap("Discord1.png", wx.BITMAP_TYPE_ANY)
        bmp2 = wx.Bitmap("Discord2.png", wx.BITMAP_TYPE_ANY)

        self.Button1 = buttons.GenBitmapButton(tool_panel,bitmap=bmp,size=(40,40),name="Button1")
        self.Button2 = buttons.GenBitmapButton(tool_panel,bitmap=bmp,size=(40,40),name="Button2")
        self.Button3 = buttons.GenBitmapButton(tool_panel,bitmap=bmp,size=(40,40),name="Button3")
        self.Button4 = wx.Button(tool_panel,-1, label="",size=(40,40),name="Button4")
        self.Button4.SetBitmap(wx.Bitmap('test.png'))
        self.Button4.SetBitmapFocus(wx.Bitmap('testfocus.png'))

        self.Text = wx.TextCtrl(panel, -1, size=(400,300), style=wx.TE_MULTILINE)

        self.Button1.SetToolTip("Function1")
        self.Button2.SetToolTip("Function2")
        self.Button3.SetToolTip("Function3")
        self.Button4.SetToolTip("Function4")

        self.BitmapButtons = [self.Button1,self.Button2,self.Button3]
        for i in range(0,len(self.BitmapButtons)):
            self.BitmapButtons[i].SetBitmapLabel(bmp)
            self.BitmapButtons[i].SetBitmapFocus(bmp2)
            self.BitmapButtons[i].SetBitmapSelected(bmp1)

        self.Bind(wx.EVT_BUTTON, self.OnButton)
        self.Bind(wx.EVT_CHAR_HOOK, self.OnKey)

        tool_sizer=wx.BoxSizer(wx.HORIZONTAL)
        main_sizer=wx.BoxSizer(wx.VERTICAL)
        tool_sizer.Add(self.Button1)
        tool_sizer.Add(self.Button2)
        tool_sizer.Add(self.Button3)
        tool_sizer.Add(self.Button4)
        tool_panel.SetSizer(tool_sizer)
        
        main_sizer.Add(tool_panel,0,wx.EXPAND)                
        main_sizer.Add(panel,0,wx.EXPAND,0)
        self.SetSizer(main_sizer)                
        self.Text.SetFocus() 
        self.Show()

    def OnButton(self, event):
        Id = event.GetId()
        btn = event.GetEventObject()
        x = self.funcs[btn.GetName()]
        x()
        self.Text.SetFocus()

    def OnButton1(self):
        print("Button 1")
    def OnButton2(self):
        print("Button 2")  
    def OnButton3(self):
        print("Button 3")
    def OnButton4(self):
        print("Button 4")        

    def OnKey(self, event):
        key = event.GetKeyCode()
        if key not in Valid_keys: #Ignore all keys except F1, Enter and Navigation keys
            event.Skip()
            return
        if key not in [wx.WXK_F1,wx.WXK_RETURN]:
            event.Skip()
            return
        if key == wx.WXK_F1: #F1 focuses on the First Toolbar button 
            self.Button1.SetFocus()
            return
        i = self.get_focus()
        if i == 1:
            id="Button1"
        elif i == 2:
            id="Button2"
        elif i == 3: 
            id="Button3"
        elif i == 4: 
            id="Button4"            
        if i > 0: #Focus was a toolbar button, execute button function
            x = self.funcs[id]
            x()
            self.Text.SetFocus()
        event.Skip()

    def get_focus(self):
        focused = wx.Window.FindFocus()
        if focused == self.Button1:
            return 1
        elif focused == self.Button2:
            return 2
        elif focused == self.Button3:
            return 3
        elif focused == self.Button4:
            return 4            
        else:#Something other than the toolbar buttons has focus return Fail
            return -1
        
#----------------------------------------------------------------------
if __name__ == "__main__":
      app = wx.App()
      frame = MainFrame()
      app.MainLoop()

enter image description here

enter image description hereenter image description here enter image description here test.pngtestfocus.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...