from Tkinter import *
from twisted.internet import reactor, tksupport
from twisted.internet.protocol import Protocol, Factory
class MultiEcho(Protocol):
def __init__(self, factory):
self.factory = factory
def connectionMade(self,*args):
self.factory.echoers.append(self)
def dataReceived(self, data):
gettest = str(data)
get = App()
get.write(gettest)
def connectionLost(self, reason):
# self.factory.echoers.remove(self)
pass
class MultiEchoFactory(Factory):
def __init__(self):
self.echoers = []
def buildProtocol(self, addr):
return MultiEcho(self)
class App(Frame):
def __init__(self,parent=None):
Frame.__init__(self,parent)
self.parent=parent
self.initUI()
def initUI(self):
self.grid(row=0,sticky=N+E+S+W)
self.columnconfigure(0,weight=1)
self.rowconfigure(0,weight=1)
self.display = Text(self)
self.display.grid(row=0,sticky=N+E+S+W)
self.yscroll = Scrollbar(self,command=self.display.yview)
self.yscroll.grid(row=0,column=1,sticky=N+S)
self.display.config(yscrollcommand=self.yscroll.set)
self.entry = Entry(self)
self.entry.grid(row=1,sticky=E+W)
self.entry.bind('<Return>',self.handle)
self.master.protocol('WM_DELETE_WINDOW',self.kill)
def write(self,text):
message = str(text)
self.display.configure(state='normal')
self.display.insert(END,message + '\n')
self.display.configure(state='disabled')
def handle(self,event=None):
msg = self.entry.get()
self.entry.delete(0,END)
self.write(msg + '\n')
self.display.yview(END)
def kill(self):
reactor.stop()
self.quit()
def main():
root = Tk()
ex = App(root)
tksupport.install(root)
reactor.listenTCP(8000, MultiEchoFactory())
reactor.run()
if __name__ == '__main__':
main()