我正在研究可以將輸入從一個客戶端寫入多客戶端的多客戶端/服務器聊天應用程序。對於客戶端來說,它運行良好,但是對於服務器端,我想添加一個可以在客戶端自己的屏幕上輸出輸入的部分。而當我做這個工作,我遇到的初始化()的問題恰恰3個arguements(2給出)符合「self.app =程序」找不到TypeError:__init __()需要3個參數(給出2個)
這裏是我的代碼
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.support import install_twisted_reactor
install_twisted_reactor()
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, Factory
class MultiClientEcho(Protocol):
def __init__(self, factory, app):
self.factory = factory
self.app = app
def connectionMade(self):
self.factory.clients.append(self)
def dataReceived(self, data):
for client in self.factory.clients:
addtolog = self.factory.app.handle_message(data)
if addtolog:
client.transport.write(data)
def connectionLost(self,reason):
self.factory.clients.remove(self)
class MultiClientEchoFactory(Factory):
protocol = MultiClientEcho
def __init__(self):
self.clients = []
def buildProtocol(self, addr):
return MultiClientEcho(self)
class ServerApp(App):
def build(self):
self.label = Label(text="server started\n")
reactor.listenTCP(8000, MultiClientEchoFactory())
return self.label
def handle_message(self, msg):
self.label.text = "received: %s\n" % msg
return msg
if __name__ == '__main__':
ServerApp().run()
有趣的是,我只是從這個網站的源代碼中進行調整:http://kivy.org/docs/guide/other-frameworks.html,它自己的工作也很好,但是一旦我將協議更改爲MultiClientEcho,它立即導致這種類型的錯誤。我怎樣才能解決這個問題?
的偉大工程,太感謝你了! –