我正在使用python中的一些客户端/服务器示例来设置一个简单的基于聊天的客户端。
当我尝试发送消息时,我遇到了GUI问题:
class ChatFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent=None, title="Game Client")
self.protocol = EchoProtocol # twisted Protocol
sizer = wx.BoxSizer(wx.VERTICAL)
self.text = wx.TextCtrl(self, style=wx.TE_MULTILINE | wx.TE_READONLY)
self.ctrl = wx.TextCtrl(self, style=wx.TE_PROCESS_ENTER, size=(300, 25))
sizer.Add(self.text, 5, wx.EXPAND)
sizer.Add(self.ctrl, 0, wx.EXPAND)
self.SetSizer(sizer)
self.ctrl.Bind(wx.EVT_TEXT_ENTER, self.send)
def send(self, evt):
self.protocol.sendLine(str(self.ctrl.GetValue()))
self.ctrl.SetValue("")
问题在于,当我发送消息时,我收到以下错误:
TypeError: unbound method sendLine() must be called with EchoProtocol instance as first argument (got str instance instead)
我也尝试过更改行
self.protocol = EchoProtocol#twisted Protocol
要:
protocol = EchoProtocol() self.protocol = protocol
我得到的错误是:
Traceback (most recent call last):
File "client.py", line 32, in send
self.protocol.sendLine(str(self.ctrl.GetValue()))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/protocols/basic.py", line 625, in sendLine
return self.transport.write(line + self.delimiter)
AttributeError: 'NoneType' object has no attribute 'write'
我的问题是如何调用发送事件并正确发送消息?