2015-04-27 102 views
2

我正在研究可以將輸入從一個客戶端寫入多客戶端的多客戶端/服務器聊天應用程序。對於客戶端來說,它運行良好,但是對於服務器端,我想添加一個可以在客戶端自己的屏幕上輸出輸入的部分。而當我做這個工作,我遇到的初始化()的問題恰恰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,它立即導致這種類型的錯誤。我怎樣才能解決這個問題?

回答

2

看那__init__定義MultiClientEchoFactory

def __init__(self, factory, app): 

這需要三個參數的功能(否則它會拋出一個錯誤)。

你叫這條線的位置:

return MultiClientEcho(self) 

現在,__init__self將獲得自動爲您的MultiClientEcho這個實例定義。 factory將被定義爲您的實例MultiClientEchoFactory。但是,您尚未傳入app的參數,因此python無法繼續。

你可能想要做的是在build功能通過您的ServerApp實例進入MultiClientEchoFactory構造:

reactor.listenTCP(8000, MultiClientEchoFactory(self)) 

改變出廠爲:

def __init__(self, app): 
    self.app = app 
    self.clients = [] 

def buildProtocol(self, addr): 
     return MultiClientEcho(self, self.app) 

這將擺脫現在您將提供第三個參數app

+0

的偉大工程,太感謝你了! –

0

錯誤信息似乎很清楚:MultiClientEcho__init__方法需要三個參數(工廠和應用程序以及自動自我),但你只有通過自我和工廠當你在buildProtocol方法實例化。它應該從哪裏得到app

2

你調用MultiClientEchoFactoryMultiClientEcho(self)只有一個參數,factory

def buildProtocol(self, addr): 
      return MultiClientEcho(self) 

你應該嘗試像

class MultiClientEchoFactory(Factory): 
    protocol = MultiClientEcho 
    def __init__(self,app): 
     self.clients = [] 
     self.app=app 

    def buildProtocol(self, addr): 
      return MultiClientEcho(self,app) 


class ServerApp(App): 
    def build(self): 
     self.label = Label(text="server started\n") 
     reactor.listenTCP(8000, MultiClientEchoFactory(self)) 
     return self.label 

    def handle_message(self, msg): 
     self.label.text = "received: %s\n" % msg 
     return msg 


if __name__ == '__main__': 
    ServerApp().run() 
相關問題