简体   繁体   中英

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

I am new to this site and fairly new to Python. I am working on a program using wxPython for the GUI. This is being developed and will be used on Windows OS machines. The GUI uses a number of buttons that trigger different processes in my script. I decided to try using the AGW AquaButtons for visual interest. I am finding that an "AquaButton" with focus will not respond to pressing the "Enter" key while the standard "wx.Button" does. Below are examples (mostly borrowed from similar questions, thanks "Rolf of Saxony" and "Mike Driscoll") of the working and non-working code.

Is there a way to get an "AquaButton" (with Focus) have it's event triggered by the "Enter" key?

This works:

    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()

This doesn't:

    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()

Bizarrely, the AquaButton is pressed using the Spacebar and not the Enter key.
Navigating appears to be via Arrow keys and/or Tab and Shift Tab.
If in doubt the source code is in:
your_python_location/dist-packages/wx/lib/agw/aquabutton.py

I hope that clears that up for you, if not for your users. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM