简体   繁体   English

Tk Text小部件的扭曲和Tkinter问题

[英]Twisted and Tkinter Issue with Tk Text widget

I am building a simple tkinter gui for the twisted application (server side). 我正在为扭曲的应用程序(服务器端)构建一个简单的tkinter gui。 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. 下面的代码运行良好,除了当我要写入从twisted(dataReceived方法)发送的数据时,它删除了Text小部件内的所有先前消息,并在第一行写入。 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. 感谢您抽出宝贵时间阅读本文。

By calling App repeatedly, you are generating a new App instance on every call. 通过重复调用App ,您将在每次调用时生成一个新的App实例。

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(...) . 相反,您应该保留对在main创建的ex的引用,并将其传递给MultiEchoFactory.__init__ ,将其保存在self ,然后从MultiEcho执行self.app.write(...)而不是App().write(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM