0

最近我使用 python 2.7 和 wx2.8 作为我的应用程序的基础。从那时起,我在处理我的应用程序中的 Return 键时遇到了问题。我只在 Windows 上有这个错误 - 在 linux 中它工作正常。处理除 Return/Numpad Return 之外的所有键。我把 wx.TE_PROCESS_TAB|wx.TE_PROCESS_ENTER| 我的框架和网格上的 wx.WANTS_CHARS 样式。我在框架、网格和网格渲染器上尝试了 ON KEY DOWN 和 ON CHAR HOOK 事件。我的 Return 键在某处丢失。尝试过的解决方案来自: wxWidgets 的 EVT_CHAR_HOOK 是做什么的?

https://groups.google.com/forum/#!topic/wxpython-users/1RaaGxu62q4(这就像我的问题的完美描述,但失败了)

http://osdir.com/ml/python.wxpython/2004-03/msg00261.html

有什么想法吗?

问候迈克

4

1 回答 1

0

wxPython 版本之间似乎存在一些不一致之处。试试下面的例子:

import wx
import wx.grid

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)
        self.grid = wx.grid.Grid(self.panel)
        self.grid.CreateGrid(3, 3)
        self.grid.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        self.grid.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
        self.grid.Bind(wx.EVT_CHAR_HOOK, self.OnChar)

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.grid, 1)

        self.panel.SetSizerAndFit(self.sizer)  
        self.Show()

    # You may or may not want to add e.Skip() to your RETURN key handling.
    # If you do not call e.Skip(), the event is not propagated to the widget.
    # Depends on your app logic if that is something you want or not.

    def OnKeyDown(self, e):      
        # Python 2.7, wxPython 2.8 - Works, but more like EVT_CHAR_HOOK
        # Python 3.3, wxPython Phoenix - Never shows up
        code = e.GetKeyCode()
        if code in [wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER]:
            print("Return key down")
        else:
            e.Skip() 

    def OnKeyUp(self, e):
        code = e.GetKeyCode()
        if code in [wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER]:
            print("Return key up")
        else:
            e.Skip() 

    def OnChar(self, e):
        # Python 2.7, wxPython 2.8 - Never shows up
        # Python 3.3, wxPython Phoenix - Works
        code = e.GetKeyCode()
        if code in [wx.WXK_RETURN, wx.WXK_NUMPAD_ENTER]:
            print("Return key char")
        else:
            e.Skip() 


app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
于 2013-12-19T11:18:18.523 回答