Table Of Contents
Integrating with other Frameworks(翻訳済み)¶
バージョン 1.0.8 で追加.
Using Twisted inside Kivy(Kivy内部でTwistedを使用する)¶
注釈
twistedをインストールすることで、関数`kivy.support.install_twisted_reactor` をkivyのイベントループ内で実行できます。
threadedselect リアクター関数はインタリーブ上の任意の引数や、この関数に渡されたキーワード引数が渡されます。これらは1つが通常 twisted’s reactor.startRunningに渡す引数です。
警告
明示的に第1引数に’installSignalHandlers’ キーワード引数を設定しない限り、デフォルトのtwisted reactorとは異なり、インストールされたreactorは、任意のシグナルを処理しません。
kivyの例は、twisted サーバーとクライアントの小さな例になります。サーバアプリが実行されているシンプルなtwistedサーバーを持ち、すべてのメッセージをログに記録します。 クライアントアプリケーションは、サーバーにメッセージを送信できし、メッセージとレスポンスを出力します。例は、twistedドキュメントからのシンプルなEchoサンプルに基づいており、ほとんどここで見つけられます:
- http://twistedmatrix.com/documents/current/_downloads/simpleserv.py
 - http://twistedmatrix.com/documents/current/_downloads/simpleclient.py
 
例に挑戦してみてください。最初にecho_server_app.pyを実行してから、echo_client_app.pyを起動してください。” “テキストボックスに何かを入力した後Enterキーを押すと、サーバーは、応答し簡単なエコーメッセージをクライアントアプリケーションに送信します。
Server App(サーバーアプリ)¶
# install_twisted_rector must be called before importing  and using the reactor
from kivy.support import install_twisted_reactor
install_twisted_reactor()
from twisted.internet import reactor
from twisted.internet import protocol
class EchoProtocol(protocol.Protocol):
    def dataReceived(self, data):
        response = self.factory.app.handle_message(data)
        if response:
            self.transport.write(response)
class EchoFactory(protocol.Factory):
    protocol = EchoProtocol
    def __init__(self, app):
        self.app = app
from kivy.app import App
from kivy.uix.label import Label
class TwistedServerApp(App):
    def build(self):
        self.label = Label(text="server started\n")
        reactor.listenTCP(8000, EchoFactory(self))
        return self.label
    def handle_message(self, msg):
        self.label.text = "received:  %s\n" % msg
        if msg == "ping":
            msg = "pong"
        if msg == "plop":
            msg = "kivy rocks"
        self.label.text += "responded: %s\n" % msg
        return msg
if __name__ == '__main__':
    TwistedServerApp().run()
Client App(クライアントアプリ)¶
# install_twisted_rector must be called before importing the reactor
from kivy.support import install_twisted_reactor
install_twisted_reactor()
# A simple Client that send messages to the echo server
from twisted.internet import reactor, protocol
class EchoClient(protocol.Protocol):
    def connectionMade(self):
        self.factory.app.on_connection(self.transport)
    def dataReceived(self, data):
        self.factory.app.print_message(data)
class EchoFactory(protocol.ClientFactory):
    protocol = EchoClient
    def __init__(self, app):
        self.app = app
    def clientConnectionLost(self, conn, reason):
        self.app.print_message("connection lost")
    def clientConnectionFailed(self, conn, reason):
        self.app.print_message("connection failed")
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
# A simple kivy App, with a textbox to enter messages, and
# a large label to display all the messages received from
# the server
class TwistedClientApp(App):
    connection = None
    def build(self):
        root = self.setup_gui()
        self.connect_to_server()
        return root
    def setup_gui(self):
        self.textbox = TextInput(size_hint_y=.1, multiline=False)
        self.textbox.bind(on_text_validate=self.send_message)
        self.label = Label(text='connecting...\n')
        self.layout = BoxLayout(orientation='vertical')
        self.layout.add_widget(self.label)
        self.layout.add_widget(self.textbox)
        return self.layout
    def connect_to_server(self):
        reactor.connectTCP('localhost', 8000, EchoFactory(self))
    def on_connection(self, connection):
        self.print_message("connected successfully!")
        self.connection = connection
    def send_message(self, *args):
        msg = self.textbox.text
        if msg and self.connection:
            self.connection.write(str(self.textbox.text))
            self.textbox.text = ""
    def print_message(self, msg):
        self.label.text += msg + "\n"
if __name__ == '__main__':
    TwistedClientApp().run()