使用焦点将“ Enter”键绑定到WXPython按钮

时间:2018-08-01 19:53:06

标签: python user-interface wxpython

我正在尝试使用箭头键导航一系列按钮,然后按Enter键。

我已经很容易地实现了箭头键,但是我似乎找不到在线的答案如何将焦点按钮绑定到Enter键。具体来说,我希望能够使用向上/向下键将焦点设置为按钮,然后按Enter键以激活该按钮。

Highlighted button 1

我可以用箭头突出显示想要的按钮,但是Enter键什么也没做。有什么想法吗?

import wx

class Example(wx.Frame):
    def __init__(self, parent, title):
        frame = wx.Frame.__init__(self, parent, title=title, )

        self.btn = []
        self.btn.append(wx.Button(self, label='Button 1', id=0))
        self.btn.append(wx.Button(self, label='Button 2', id=1))

        self.Bind(wx.EVT_BUTTON, self.button1_press, id=0)
        self.Bind(wx.EVT_TEXT_ENTER, self.button1_press)

        self.Bind(wx.EVT_BUTTON, self.button2_press, id=1)
        self.Bind(wx.EVT_TEXT_ENTER, self.button2_press)

        self.Bind(wx.EVT_CHAR_HOOK, self.on_key)

        self.sizer = wx.GridBagSizer(0, 0)

        self.sizer.Add(self.btn[0], pos=(0, 0), flag=wx.ALIGN_CENTER)
        self.sizer.Add(self.btn[1], pos=(1, 0), flag=wx.ALIGN_CENTER)

        self.SetSizer(self.sizer)
        self.Fit()

    def button1_press(self, event):
        print 'button 1'

    def button2_press(self, event):
        print 'button 2'

    def on_key(self, event):
        i = self.get_focus()
        if event.GetKeyCode() == wx.WXK_DOWN:
            i = min(i+1, 1)
            self.btn[i].SetFocus()
        elif event.GetKeyCode() == wx.WXK_UP:
            i = max(i-1, 0) 
            self.btn[i].SetFocus()
        elif event.GetKeyCode() == wx.WXK_RETURN:  # <-doesn't work
            print 'ENTER!'
        else:
            event.Skip()

    def get_focus(self):
        focused = wx.Window.FindFocus()
        if focused == self.btn[0]:
            return 0
        elif focused == self.btn[1]:
            return 1


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(0)
app.MainLoop()

1 个答案:

答案 0 :(得分:1)

您的代码很亲密,说实话,在我的机器(ubuntu 18.04)上,输入确实有效,但箭头键无效。 这样做的关键似乎是为窗口内的焦点设置Default()
如果将它们全部放在Panel中,则可以得到Tab遍历的额外好处,因此,制表符也可以在按钮之间移动,而无需您费力。
这是使用python2.7 wx(3.0.2.0)和python 3.6 wx(4.0.3)在我的盒子上工作的代码的版本

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.btn2 = wx.Button(self.panel, label='Button 2', id=2)
        self.btn1.SetDefault()
        self.btn1.SetFocus()

        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.Bind(wx.EVT_BUTTON, self.button_press)
        self.Bind(wx.EVT_CHAR_HOOK, self.on_key)

        self.panel.SetSizer(self.sizer)
        self.Fit()

    def button_press(self, event):
        Id = event.GetId()
        print ('Click Button',str(Id))

    def on_key(self, event):
        key = event.GetKeyCode()
        if key == wx.WXK_DOWN or key == wx.WXK_UP:
            i = self.get_focus()
            if i == 1:
                self.btn1.SetDefault()
                self.btn1.SetFocus()
            else:
                self.btn2.SetDefault()
                self.btn2.SetFocus()
            print ('Focus on',str(i))
        elif key == wx.WXK_RETURN:
            print ('ENTER on Button',str(event.GetId()))
        else:
            event.Skip()

    def get_focus(self):
        focused = wx.Window.FindFocus()
        if focused == self.btn1:
            return 2
        elif focused == self.btn2:
            return 1


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