wxPython Key Bindings don't work on Linux

拈花ヽ惹草 提交于 2019-12-13 07:57:08

问题


The following program works for me in Windows but not in Linux. There are no print statements displayed when I press the keys that I binded. However, the tab key can switch between the two buttons and the enter and space key toggle them. No other keys work.

import wx

class MyForm(wx.Frame):
    def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, "Pressing dem keyz")

            # Add a panel so it looks the correct on all platforms
            panel = wx.Panel(self, wx.ID_ANY)
            self.btn = wx.ToggleButton(panel, label="TOGGLE")
            self.btn2 = wx.ToggleButton(panel, label="TOGGLE 2", pos = (85,0))

            self.btn.Bind(wx.EVT_CHAR_HOOK, self.onKeyPress)
            self.btn2.Bind(wx.EVT_CHAR_HOOK, self.onKeyPress)

    def onKeyPress(self, event):
            space = False
            keycode = event.GetKeyCode()
            print keycode
            if keycode == wx.WXK_SPACE: 
                    print "SPACEBAR!"
                    space = True
            self.btn.SetValue(space)
            if space == True:
                    print "Do something"
            elif keycode == wx.WXK_RETURN:
                    self.Hello()
            elif keycode == wx.WXK_LEFT:
                    self.btn2
                    print 'YOU MOVED LEFT'
            elif keycode == wx.WXK_RIGHT:
                    self.btn
                    print 'YOU MOVED RIGHT'
            elif keycode == wx.WXK_UP:
                    print 'YOU MOVED UP'
            elif keycode == wx.WXK_DOWN:
                    print 'YOU MOVED DOWN'
            elif keycode == wx.WXK_ESCAPE:
                    self.Destroy()
    def Hello(self):
            print "Hello"
            return
 # Run the program
if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm()
        frame.Show()
        app.MainLoop()

回答1:


Or indeed, EVT_KEY_UP
which gets rid of the problem of a key held down giving multiple key down events.




回答2:


Instead of using EVT_CHAR_HOOK use EVT_KEY_DOWN.



来源:https://stackoverflow.com/questions/31680039/wxpython-key-bindings-dont-work-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!