Title: Kivy Integration Date: 2017-02-23 Category: Kivy Tags: Python,Kivy
Kivy中文编程指南:整合其他框架
这是在 Kivy 1.0.8 版本以后添加的新功能。
在 Kivy 内使用 Twisted
特别注意
可以使用 kivy.support.install_twisted_reactor 这个函数来安装一个 twisted 反应器(reactor),这个反应器会在 Kivy 的事件循环内运行。
传递给此函数(kivy.support.install_twisted_reactor )的任何参数、关键字参数将在线程选择反应器交叉函数上传递(threadedselect reactors interleave function 这个我不懂是什么,强行翻译了,抱歉)。 这些参数通常是传递给twisted 的反应器启动函数(reactor.startRunning)的。
警告
Kivy 这里面的这个 reactore 和默认的 twisted reactor 反应器不一样,只有当你把 ‘installSignalHandlers’ 关键词参数设置为 1 来才去处理信号。这样做是为了保证 Kivy 能够按照常规情况来处理信号,而当你指定让 twisted reactor 来处理信号的时候再换用 twisted。(比如 SIGINT 智能信号等情境。)
Kivy 的样例代码中有一个例子,是一个简单的 twisted 服务端和客户端。服务端应用开启了一个 twisted 服务器,并对任何信息都进行日志记录。客户端的应用可以向服务端发送信息,然后接收服务端返回的信息并且输出显示。这些样例代码是基于 twisted 官方文档里面的简单的回声样例进行修改而实现的,原版代码可以在下面的地址中找到:
请按照如下步骤来尝试这个样例:先运行 echo_server_app.py, 然后运行 echo_client_app.py。在客户端的文本框中输入随便一些什么内容都可以,然后回车发送给服务端,服务端会把接收到的信息都原样返回,就像是回声一样。
服务端应用
# install_twisted_rector must be called before importing and using the reactor# 一定要先要导入反应器,然后才能调用install_twisted_rectorfrom kivy.support import install_twisted_reactorinstall_twisted_reactor()from twisted.internet import reactorfrom twisted.internet import protocolclass 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 = EchoProtocoldef __init__(self, app):self.app = appfrom kivy.app import Appfrom kivy.uix.label import Labelclass TwistedServerApp(App):def build(self):self.label = Label(text="server started\n")reactor.listenTCP(8000, EchoFactory(self))return self.labeldef handle_message(self, msg):self.label.text = "received: %s\n" % msgif msg == "ping":msg = "pong"if msg == "plop":msg = "kivy rocks"self.label.text += "responded: %s\n" % msgreturn msgif __name__ == '__main__':TwistedServerApp().run()
客户端应用
# install_twisted_rector must be called before importing the reactor# 一定要先要导入反应器,然后才能调用install_twisted_rectorfrom kivy.support import install_twisted_reactorinstall_twisted_reactor()# A simple Client that send messages to the echo server# 这个简单的客户端是用来给回声服务器发送信息的from twisted.internet import reactor, protocolclass 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 = EchoClientdef __init__(self, app):self.app = appdef 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 Appfrom kivy.uix.label import Labelfrom kivy.uix.textinput import TextInputfrom 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# 这里的样例是一个很简单的 Kivy 应用,有一个字符输入框 textbox 来输入消息# 还有一个大的文本标签 label 来显示从服务器接收到的信息。class TwistedClientApp(App):connection = Nonedef build(self):root = self.setup_gui()self.connect_to_server()return rootdef 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.layoutdef connect_to_server(self):reactor.connectTCP('localhost', 8000, EchoFactory(self))def on_connection(self, connection):self.print_message("connected successfully!")self.connection = connectiondef send_message(self, *args):msg = self.textbox.textif 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()
