From: Kevin A. <ka...@us...> - 2004-04-27 22:03:16
|
Update of /cvsroot/pythoncard/PythonCard/samples/twistedEchoClient In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17350/samples/twistedEchoClient Added Files: .cvsignore readme.txt twistedEchoClient.py twistedEchoClient.rsrc.py Log Message: added TwistedApplication subclass for writing Twisted GUI apps added twistedEchoClient sample --- NEW FILE: twistedEchoClient.rsrc.py --- {'stack':{'type':'Stack', 'name':'Dialogs', 'backgrounds': [ {'type':'Background', 'name':'bg1', 'title':'Twisted PythonCard PB Echo Client', 'size':(375, 270), 'statusBar':1, 'menubar': {'type':'MenuBar', 'menus': [ {'type':'Menu', 'name':'File', 'label':'&File', 'items': [ {'type':'MenuItem', 'name':'menuFileExit', 'label':'E&xit\tAlt+X', 'command':'exit', }, ] }, ] }, 'components': [ {'type':'StaticText', 'name':'StatusStaticText', 'position':(284, 215), 'backgroundColor':(0, 255, 127), 'font':{'family': 'sansSerif', 'size': 10}, 'text':'CONNECTED', 'visible':0, }, {'type':'Button', 'name':'buttonLogin', 'position':(22, 140), 'size':(117, 26), 'label':'Login', }, {'type':'TextField', 'name':'SendTextField', 'position':(20, 180), 'size':(330, -1), 'alignment':'left', }, {'type':'TextArea', 'name':'ReceivedTextArea', 'position':(20, 0), 'size':(330, 125), 'alignment':'left', }, {'type':'Button', 'name':'buttonSend', 'position':(151, 140), 'size':(125, 25), 'label':'Send', }, ] # end components } # end background ] # end backgrounds } } --- NEW FILE: .cvsignore --- .cvsignore *.pyc *.log .DS_Store --- NEW FILE: twistedEchoClient.py --- """ Twisted PythonCard PbEchoClient """ from PythonCard import model, twistedModel from twisted.cred.credentials import UsernamePassword from twisted.spread import pb from twisted.internet import reactor class DefinedError(pb.Error): pass class EchoClient(model.Background): """ TPC PB Echo GUI Panel """ def on_initialize(self, event): self.pbfactory = pb.PBClientFactory() # KEA the Send button and SendTextField should be disabled # until a successful login self.components.SendTextField.enabled = False self.components.buttonSend.enabled = False def on_SendTextField_keyPress(self, event): # if user presses return, send text if event.keyCode == 13: self.sendAndClearText() else: event.skip() # KEA 2004-04-27 # this should popup a custom dialog # to prompt the user for the host, port number, # username, and password # with defaults of "localhost", pb.portno # "guest", and "guest" # this dialog is going to be pretty common so we'll stick # in PythonCard/templates/dialogs to simplify usage from # other twisted apps def on_buttonLogin_mouseClick(self, event): reactor.connectTCP("localhost", pb.portno, self.pbfactory) self.pbfactory.login( UsernamePassword("guest", "guest") ).addCallbacks(self.loginsuccess, self.loginfailure) def loginsuccess(self, perspective): self.statusBar.text = 'Connected' self.components.SendTextField.enabled = True self.components.buttonSend.enabled = True self.components.SendTextField.setFocus() self.perspective = perspective def loginfailure(self, error): self.displaycontent("Error on login: %s" % error) def sendAndClearText(self): fld = self.components.SendTextField self.perspective.callRemote('echo', fld.text ).addCallbacks(self.echosuccess, self.echofailure) fld.text = "" def on_buttonSend_mouseClick(self, event): self.sendAndClearText() def echosuccess(self, message): self.displaycontent(message) def echofailure(self, error): t = error.trap(DefinedError) self.displaycontent("error received"+t) def displaycontent(self, text): self.components.ReceivedTextArea.appendText(text + "\n") if __name__ == '__main__': app = twistedModel.TwistedApplication(EchoClient) app.MainLoop() --- NEW FILE: readme.txt --- Twisted Echo client which uses the TwistedApplication class. class TwistedApplication(model.Application): def OnInit(self): model.Application.OnInit(self) reactor.startRunning() wx.EVT_TIMER(self, 999999, self.OnTimer) self.twistedTimer = wx.Timer(self, 999999) self.twistedTimer.Start(250, False) return True def OnTimer(self, event): reactor.runUntilCurrent() reactor.doIteration(0) def OnExit(self): # need to stop the timer for cleanup purposes self.twistedTimer.Stop() self.twistedTimer = None reactor.stop() You need to start pbecho.py first so you have a server to connect to. pbecho.py is included in the Twisted distribution within your Python site-packages directory: site-packages/TwistedDocs/examples/pbecho.py Contributed by Stephen Waterbury. Additional code by Kevin Altis Adapted from Uwe C. Schroeder's cookbook entry "Using wxPython with Twisted Python" http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/181780 |