0

I'm sorry if this is a newbie question, but I am writing a PythonCard program and I want it to do something when I press the enter key from inside a TextBox. My code is below:

def on_SampleTextField_keyDown(self, event):
    if event.keyCode == wx.WXK_RETURN:
        doSomething()
    else:
        event.Skip()

This works fine on Linux, but when I run it on Windows it just acts as the Tab key would. After searching the web I believe Windows is treating the enter key as a navigational key, and if I were using just wxPython I would need to set the window style to wx.WANTS_CHARS . I think this should be possible in PythonCard since it just sits on top of wxPython, but I'm not sure. And if it is I wouldn't know how to do it! So if anyone has any ideas, please let me know!

4

1 回答 1

0

wx.TE_PROCESS_ENTER在 wxPython 中,我可以在添加到样式TextCtrl并绑定wx.EVT_TEXT_ENTER事件时使用返回键,如此所述。
PythonCard 既不定义此事件,也不能手动更改 TextField 的样式,因为此属性设置在__init__(). 在 wxPython 中对我有用的另一个选项是绑定到wx.EVT_CHAR_HOOK不需要在初始化期间设置特定样式属性的事件。这就是为什么我使用这种方法来为这个问题提出一个有点老套的解决方案。

PythonCard 在类结构中定义它的事件,如下所示:

class KeyPressEvent(KeyEvent):    
    name = 'keyPress'
    binding = wx.EVT_CHAR
    id = wx.wxEVT_CHAR

我将此结构用作新自定义事件的模板,因此我的代码现在看起来像这样:

from PythonCard import model
from PythonCard.event import KeyEvent
import PythonCard.components.textfield

import wx

class KeyPressHookEvent(KeyEvent):    
    name = 'keyPressHook'
    binding = wx.EVT_CHAR_HOOK
    id = wx.wxEVT_CHAR_HOOK

# TextFieldEvents is a tuple so type juggling is needed to modify it
PythonCard.components.textfield.TextFieldEvents = tuple(
        list(PythonCard.components.textfield.TextFieldEvents) + 
        [KeyPressHookEvent])

然后我像往常一样创建我的应用程序并定义处理程序:

def on_keyPressHook(self, event):
    if event.keyCode == wx.WXK_RETURN:
        self.handle_return()
    else:
        event.Skip()

这样我就可以处理按回车键handle_return()。这适用于 Windows,但不适用于 Cygwin,因此可能不适用于 linux。至少在 Cygwin 中,应用程序仍然会收到按回车键,on_keyDown()所以您所要做的就是将内容复制on_keyPressHook()到该函数。

于 2013-10-14T12:17:48.610 回答