【问题标题】:how to make key event wxpython in textctrl?如何在 textctrl 中制作关键事件 wxpython?
【发布时间】:2012-07-13 00:57:24
【问题描述】:

这是我的代码

python 2.7 & wxpython 2.8

http://pastie.org/4248326

这3个textctrl,在chat_c(textctrl)

我想让 chat_c 和 text_c 像一个聊天室

输入是chat_c 输出是text_c

这就是我使用的原因

def OnReturnDown(self,e):

    key = e.GetKeyCode()
    self.text_c.SetValue(key) #for check out but doesn't work
    if key == wx.WXK_RETURN:
        self.text_c.SetValue(self.chat_c.GetValue()) 

 #key bind
    self.chat_c.Bind(wx.EVT_KEY_DOWN, self.OnReturnDown)

这是错误信息

Traceback (most recent call last):
  File "C:\workspace\wx_python_test\main_chat_client.py", line 239, in OnReturnDown
    self.text_c.SetValue(key) #for check out but doesn't work
  File "C:\Python27\Lib\site-packages\wx-2.8-msw-unicode\wx\_controls.py", line 1754, in SetValue
    return _controls_.TextCtrl_SetValue(*args, **kwargs)
TypeError: String or Unicode type required

那是什么?需要Unicode类型???

也许改变 textctrl 的样式?

如何解决这个问题?

【问题讨论】:

    标签: event-handling wxpython keycode textctrl


    【解决方案1】:

    e.GetKeyCode() 返回一个 int。您不会将 int 传递给文本控件。文本控件只接受一个字符串或一个 unicode 字符串。因此,您需要将 int 转换为字符串或执行其他操作。投射方法如下:

    key = str( e.GetKeyCode() )
    

    【讨论】:

    • 会发生什么?你能提供一个可运行的小例子吗?
    猜你喜欢
    • 2014-11-12
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多