I am working on a multi clients/ server chat app that can write input from one client to multi clients. For the client side it's working well, yet for the server side I want to add a part where it can print out the input from the clients on its own screen as well. And when I am working on it I encounter the issue of init()takes exactly 3 arguements (2 given) with the line "self.app = app"
我正在开发一个多客户机/服务器聊天应用程序,可以从一个客户端到多个客户端编写输入。对于客户端来说,它运行得很好,但是对于服务器端,我想添加一个部分,它可以在自己的屏幕上打印出来自客户机的输入。当我在处理它的时候,我遇到了init()的问题,它正好带了3个论点(2给出)和“self”。应用=应用”
Here is my code
这是我的代码
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()
The interesting thing is that I was just adapting from the source code from this site: http://kivy.org/docs/guide/other-frameworks.html , it was working well on its own as well, but once i changed the protocol to MultiClientEcho it immediately resulted in such type error. How can I fix this?
有趣的是,我刚刚从这个站点上修改了源代码:http://kivy.org/docs/guide/other-frameworks.html,它本身也运行良好,但是一旦我将协议更改为多列技术,它就会立即导致这种类型错误。我怎么解决这个问题?
3 个解决方案
#1
2
Look at the __init__
definition for MultiClientEchoFactory
:
看看多中心工厂的__init__定义:
def __init__(self, factory, app):
That requires three parameters to function (or else it'll throw an error).
这需要三个参数来运行(否则会抛出一个错误)。
You call this line here:
你把这条线叫做
return MultiClientEcho(self)
Now, the self
in __init__
will get defined automatically for you by this instance of MultiClientEcho
. factory
will get defined as your instance of MultiClientEchoFactory
. However, you haven't passed in a parameter for app
, so python cannot continue.
现在,在__init__中的self将通过这个多列技术的实例自动为您定义。工厂将被定义为你的多工厂的实例。但是,您还没有在应用程序中传递参数,因此python不能继续。
What you probably want to do is pass your instance of ServerApp
in the build
function into the constructor of MultiClientEchoFactory
:
您可能想要做的是将构建函数中的ServerApp实例传递给multilientechofactory的构造函数:
reactor.listenTCP(8000, MultiClientEchoFactory(self))
change the factory to be:
更改工厂为:
def __init__(self, app):
self.app = app
self.clients = []
def buildProtocol(self, addr):
return MultiClientEcho(self, self.app)
which will get rid of this error as now you'll be supplying that third app
parameter.
这将会消除这个错误现在你将提供第三个应用程序参数。
#2
2
you are calling MultiClientEcho(self)
in class MultiClientEchoFactory
with only one arguments,factory
:
您在类多列的工厂中调用多列技术(self),只有一个参数,工厂:
def buildProtocol(self, addr):
return MultiClientEcho(self)
you should try something like
你应该试试。
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()
#3
0
The error message seems clear: MultiClientEcho
's __init__
method expects three parameters (factory and app as well as the automatic self) but you're only passing self and factory when you instantiate it in the buildProtocol
method . Where is it supposed to get app
from?
错误信息似乎很清楚:多列技术的__init__方法需要三个参数(工厂和应用程序以及自动的self),但是当你在buildProtocol方法中实例化它时,你只是在传递self和factory。它应该从哪里得到应用?
#1
2
Look at the __init__
definition for MultiClientEchoFactory
:
看看多中心工厂的__init__定义:
def __init__(self, factory, app):
That requires three parameters to function (or else it'll throw an error).
这需要三个参数来运行(否则会抛出一个错误)。
You call this line here:
你把这条线叫做
return MultiClientEcho(self)
Now, the self
in __init__
will get defined automatically for you by this instance of MultiClientEcho
. factory
will get defined as your instance of MultiClientEchoFactory
. However, you haven't passed in a parameter for app
, so python cannot continue.
现在,在__init__中的self将通过这个多列技术的实例自动为您定义。工厂将被定义为你的多工厂的实例。但是,您还没有在应用程序中传递参数,因此python不能继续。
What you probably want to do is pass your instance of ServerApp
in the build
function into the constructor of MultiClientEchoFactory
:
您可能想要做的是将构建函数中的ServerApp实例传递给multilientechofactory的构造函数:
reactor.listenTCP(8000, MultiClientEchoFactory(self))
change the factory to be:
更改工厂为:
def __init__(self, app):
self.app = app
self.clients = []
def buildProtocol(self, addr):
return MultiClientEcho(self, self.app)
which will get rid of this error as now you'll be supplying that third app
parameter.
这将会消除这个错误现在你将提供第三个应用程序参数。
#2
2
you are calling MultiClientEcho(self)
in class MultiClientEchoFactory
with only one arguments,factory
:
您在类多列的工厂中调用多列技术(self),只有一个参数,工厂:
def buildProtocol(self, addr):
return MultiClientEcho(self)
you should try something like
你应该试试。
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()
#3
0
The error message seems clear: MultiClientEcho
's __init__
method expects three parameters (factory and app as well as the automatic self) but you're only passing self and factory when you instantiate it in the buildProtocol
method . Where is it supposed to get app
from?
错误信息似乎很清楚:多列技术的__init__方法需要三个参数(工厂和应用程序以及自动的self),但是当你在buildProtocol方法中实例化它时,你只是在传递self和factory。它应该从哪里得到应用?