绑定“Enter"带焦点的 WXPython 按钮的关键 [英] Bind "Enter" key to WXPython Button with Focus

查看:32
本文介绍了绑定“Enter"带焦点的 WXPython 按钮的关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用箭头键和回车键使一系列按钮可导航.

I'm trying to make a series of buttons navigable with the arrow keys and enter key.

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

I have the arrow keys implemented easily enough, but I can't seem to find the answer online how to bind the focused button to the enter key. Specifically I want to be able to use the up/down keys to set the focus to a button, and then hit the enter key to activate the button.

我可以用箭头突出显示我想要的按钮,但回车键什么也不做.有什么想法吗?

I can highlight the button i want with the arrows, but the enter key does nothing. Any ideas?

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

推荐答案

老实说,你的代码很接近,在我的机器 (ubuntu 18.04) 上输入确实有效,但箭头键无效.
关键似乎是为窗口内的焦点设置 Default().
如果您将其全部放在 Panel 中,您将获得 Tab traversval 的额外好处,因此 Tab 也可以在您的按钮之间移动,而无需您做任何努力.
这是使用 python2.7 wx (3.0.2.0) 和 python 3.6 wx (4.0.3) 在我的盒子上运行的代码版本

Your code is close and to be honest, on my machine (ubuntu 18.04) enter does work but the arrows keys don't.
The key to this appears to be setting Default() for the focus within the window.
If you put it all in a Panel you get the added benefit of Tab traversval, so tabbing moves between your buttons as well, without any effort on your part.
Here is a version of your code working on my box using python2.7 wx (3.0.2.0) and 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()

这篇关于绑定“Enter"带焦点的 WXPython 按钮的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆