Search code examples
pythontkintertwisted

Twisted and Tkinter Issue with Tk Text widget


I am building a simple tkinter gui for the twisted application (server side). The code below works well except when I want to write the data send from twisted(dataReceived method), it deletes all the previous messages inside Text widget and writes at the first line. I want to write the client message in that Text widget without deleting previous text contained inside it.

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 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 __init__(self,parent=None):
        Frame.__init__(self,parent)
        self.parent=parent
        self.initUI()


    def kill(self):        
        reactor.stop()
        self.quit()

    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 main():  
    root = Tk()
    ex = App(root)
    tksupport.install(root)
    mef = MultiEchoFactory()
    reactor.listenTCP(8000, mef)
    reactor.run()


if __name__ == '__main__':
    main()

Please help me out with this problem. Thanks for taking time to read this.


Solution

  • By calling App repeatedly, you are generating a new App instance on every call.

    Instead, you should keep a reference to the ex you created in main and pass it to MultiEchoFactory.__init__, save it on self, and then from MultiEcho do self.app.write(...) rather than App().write(...).