Hi,
I’m a university student creating a python Chat server
and client using twisted. The server works great. However I’m trying to
create the client with a gui using wxpython and twisted. The code for the
client is attached bellow. I’m working with a partner and he already
tried posting our problem on comp.lang.python but we didn’t receive a
successful reply so I thought I would try the mailing list. Here’s the problem:
I can get my client to connect to my server, but can’t get it to
disconnect or send messages to the server.
I am getting the following error when I click on the 'Disconnect' button - AttributeError:
'NoneType' object has no attribute 'loseConnection'
I have attached the code for the client below this.
We are both fairly new to Python so would appreciate any help anyone can offer.
Thanks,
Chris & Peter
client.py
----------
from wxPython.wx import *
import wx
from twisted.internet import wxreactor
wxreactor.install()
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, ClientCreator
class imApp(wxApp, Protocol):
def buildMe(self):
frame = wx.Frame(None,
title="IM Client", size=(800, 550))
bkg = wx.Panel(frame)
global ipAdd
global portNo
global messages
global newMsg
ipAddLab = wx.StaticText(bkg, -1,
'IP Address: ')
ipAdd = wx.TextCtrl(bkg)
ipAdd.SetToolTipString('Please enter
the server IP address
here.')
spacer1 = wx.StaticText(bkg, -1,
'
')
portNoLab = wx.StaticText(bkg, -1,
'Port No: ')
portNo = wx.TextCtrl(bkg)
portNo.SetToolTipString('Please
enter the port number the
server is using here.')
spacer2 = wx.StaticText(bkg, -1,
'
')
connectButton = wx.Button(bkg,
label='Connect')
connectButton.SetToolTipString('Click
this button to connect to
the server.')
connectButton.Bind(wx.EVT_BUTTON,
self.connectMe)
disconnectButton = wx.Button(bkg,
label='Disconnect')
disconnectButton.SetToolTipString('Click this button to
disconnect from the server.')
disconnectButton.Bind(wx.EVT_BUTTON,
self.disconnectMe)
messages = wx.TextCtrl(bkg,
style=(wx.TE_MULTILINE |
wx.HSCROLL))
newMsg = wx.TextCtrl(bkg)
sendButton = wx.Button(bkg,
label='Send')
sendButton.SetToolTipString('Click
this button to send a
message to the server.')
sendButton.Bind(wx.EVT_BUTTON,
self.sendMe)
hbox1 = wx.BoxSizer()
hbox1.Add(ipAddLab, proportion=0,
flag=wx.EXPAND)
hbox1.Add(ipAdd, proportion=0,
flag=wx.EXPAND)
hbox1.Add(spacer1, proportion=0,
flag=wx.EXPAND)
hbox1.Add(portNoLab, proportion=0,
flag=wx.EXPAND)
hbox1.Add(portNo, proportion=0,
flag=wx.EXPAND)
hbox1.Add(spacer2, proportion=0,
flag=wx.EXPAND)
hbox1.Add(connectButton,
proportion=0, flag=wx.LEFT, border=5)
hbox1.Add(disconnectButton,
proportion=0, flag=wx.LEFT,
border=5)
hbox2 = wx.BoxSizer()
hbox2.Add(newMsg, proportion=1,
flag=wx.EXPAND | wx.LEFT |
wx.LEFT | wx.LEFT, border=5)
hbox2.Add(sendButton, proportion=0,
flag=wx.LEFT, border=5)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox1, proportion=0,
flag=wx.EXPAND | wx.ALL,
border=5)
vbox.Add(messages, proportion=1,
flag=wx.EXPAND | wx.LEFT |
wx.LEFT | wx.LEFT, border=5)
vbox.Add(hbox2, proportion=1,
flag=wx.EXPAND | wx.ALL,
border=5)
bkg.SetSizer(vbox)
ipAdd.WriteText('localhost')
portNo.WriteText('1234')
frame.Show(true)
return true
def sendMe(self, e):
msg = newMsg.GetValue() + '\n'
messages.WriteText(msg)
newMsg.SetValue('')
def disconnectMe(self, e):
messages.WriteText('Disconnecting
from server...\n')
self.transport.loseConnection()
def connectMe(self, e):
messages.WriteText('Connecting to
server...\n')
c = ClientCreator(reactor, imApp)
ip = str(ipAdd.GetValue())
port = int(portNo.GetValue())
c.connectTCP(ip, port)
def mainProg():
app = imApp(0)
app.buildMe()
reactor.registerWxApp(app)
reactor.run()
if __name__ == '__main__':
mainProg()