将“Enter”键绑定到具有焦点的 WXPython "AquaButton" 按钮

Bind “Enter” key to WXPython "AquaButton" Button with Focus

我是这个网站的新手,对 Python 也相当陌生。我正在开发一个使用 wxPython 作为 GUI 的程序。这正在开发中,将在 Windows OS 机器上使用。 GUI 使用许多按钮来触发脚本中的不同进程。我决定尝试使用 AGW AquaButtons 来获得视觉效果。我发现带焦点的 "AquaButton" 不会响应按下 "Enter" 键,而标准 "wx.Button" 会响应。以下是工作代码和非工作代码的示例(主要是从类似问题中借用的,感谢 "Rolf of Saxony" 和 "Mike Driscoll")。

有没有办法让 "AquaButton"(带焦点)的事件由 "Enter" 键触发?

这个有效:

    import wx

    class Example(wx.Frame):
        def __init__(self, parent, title):
            frame = wx.Frame.__init__(self, parent, title=title, )
            self.panel = wx.Panel(self, -1, size=(200,100))
            self.btn1 = wx.Button(self.panel, label='Button 1', id=1)
            self.btn1.SetForegroundColour("black")
            self.btn2 = wx.Button(self.panel, label='Button 2', id=2)
            self.btn2.SetForegroundColour("black")

            self.sizer = wx.GridBagSizer(0, 0)
            self.sizer.Add(self.btn1, pos=(0, 0), flag=wx.ALIGN_CENTER)
            self.sizer.Add(self.btn2, pos=(1, 0), flag=wx.ALIGN_CENTER)

            self.text_box = wx.StaticText(self.panel, style = wx.NO_BORDER)
            self.text_box.SetFont(wx.Font(14, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
            self.text_box.SetForegroundColour((40,115,180))
            self.sizer.Add(self.text_box, pos=(2,0), flag=wx.ALIGN_CENTER)

            self.Bind(wx.EVT_BUTTON, self.button_press)

            self.panel.SetSizer(self.sizer)
            self.Show()

        def button_press(self, event):
            Id = event.GetId()
            print ('Click Button',str(Id))
            retVal = F"Click Button {Id}"
            self.text_box.SetLabel(str(retVal))


    class AppMenu(wx.App):
        def OnInit(self):
            'Create the main window and insert the custom frame'
            frame = Example(None, 'Example')
            frame.Show(True)

            return True

    app = AppMenu()
    app.MainLoop()

这不是:

    import wx
    import wx.lib.agw.aquabutton as AB

    class Example(wx.Frame):
        def __init__(self, parent, title):
            frame = wx.Frame.__init__(self, parent, title=title, )
            self.panel = wx.Panel(self, -1, size=(200,100))
            self.btn1 = AB.AquaButton(self.panel, label='Button 1', id=1)
            self.btn1.SetForegroundColour("black")
            self.btn2 = AB.AquaButton(self.panel, label='Button 2', id=2)
            self.btn2.SetForegroundColour("black")

            self.sizer = wx.GridBagSizer(0, 0)
            self.sizer.Add(self.btn1, pos=(0, 0), flag=wx.ALIGN_CENTER)
            self.sizer.Add(self.btn2, pos=(1, 0), flag=wx.ALIGN_CENTER)

            self.text_box = wx.StaticText(self.panel, style = wx.NO_BORDER)
            self.text_box.SetFont(wx.Font(14, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, 0, ""))
            self.text_box.SetForegroundColour((40,115,180))
            self.sizer.Add(self.text_box, pos=(2,0), flag=wx.ALIGN_CENTER)

            self.Bind(wx.EVT_BUTTON, self.button_press)

            self.panel.SetSizer(self.sizer)
            self.Show()

        def button_press(self, event):
            Id = event.GetId()
            print ('Click Button',str(Id))
            retVal = F"Click Button {Id}"
            self.text_box.SetLabel(str(retVal))


    class AppMenu(wx.App):
        def OnInit(self):
            'Create the main window and insert the custom frame'
            frame = Example(None, 'Example')
            frame.Show(True)

            return True

    app = AppMenu()
    app.MainLoop()

奇怪的是,AquaButton 是使用空格键而不是 Enter 键按下的。
导航似乎是通过箭头键 and/or Tab 和 Shift Tab。
如有疑问,源代码位于:
your_python_location/dist-packages/wx/lib/agw/aquabutton.py

我希望这能为您解决这个问题,如果不是您的用户。 :)