標籤:

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 reactorn# 一定要先要導入反應器,然後才能調用install_twisted_rectornnnnfrom kivy.support import install_twisted_reactorninstall_twisted_reactor()nnfrom twisted.internet import reactornfrom twisted.internet import protocolnnclass EchoProtocol(protocol.Protocol):n def dataReceived(self, data):n response = self.factory.app.handle_message(data)n if response:n self.transport.write(response)nnclass EchoFactory(protocol.Factory):n protocol = EchoProtocolnn def __init__(self, app):n self.app = appnnfrom kivy.app import Appnfrom kivy.uix.label import Labelnnclass TwistedServerApp(App):n def build(self):n self.label = Label(text="server startedn")n reactor.listenTCP(8000, EchoFactory(self))n return self.labelnn def handle_message(self, msg):n self.label.text = "received: %sn" % msgnn if msg == "ping":n msg = "pong"n if msg == "plop":n msg = "kivy rocks"n self.label.text += "responded: %sn" % msgn return msgnnif __name__ == __main__:n TwistedServerApp().run()n

客戶端應用

# install_twisted_rector must be called before importing the reactorn# 一定要先要導入反應器,然後才能調用install_twisted_rectornnfrom kivy.support import install_twisted_reactorninstall_twisted_reactor()nn# A simple Client that send messages to the echo servern# 這個簡單的客戶端是用來給回聲伺服器發送信息的nnfrom twisted.internet import reactor, protocolnnclass EchoClient(protocol.Protocol):n def connectionMade(self):n self.factory.app.on_connection(self.transport)nn def dataReceived(self, data):n self.factory.app.print_message(data)nnclass EchoFactory(protocol.ClientFactory):n protocol = EchoClientnn def __init__(self, app):n self.app = appnn def clientConnectionLost(self, conn, reason):n self.app.print_message("connection lost")nn def clientConnectionFailed(self, conn, reason):n self.app.print_message("connection failed")nnfrom kivy.app import Appnfrom kivy.uix.label import Labelnfrom kivy.uix.textinput import TextInputnfrom kivy.uix.boxlayout import BoxLayoutnn# A simple kivy App, with a textbox to enter messages, andn# a large label to display all the messages received fromn# the servern# 這裡的樣例是一個很簡單的 Kivy 應用,有一個字元輸入框 textbox 來輸入消息n# 還有一個大的文本標籤 label 來顯示從伺服器接收到的信息。nnnnclass TwistedClientApp(App):n connection = Nonenn def build(self):n root = self.setup_gui()n self.connect_to_server()n return rootnn def setup_gui(self):n self.textbox = TextInput(size_hint_y=.1, multiline=False)n self.textbox.bind(on_text_validate=self.send_message)n self.label = Label(text=connecting...n)n self.layout = BoxLayout(orientation=vertical)n self.layout.add_widget(self.label)n self.layout.add_widget(self.textbox)n return self.layoutnn def connect_to_server(self):n reactor.connectTCP(localhost, 8000, EchoFactory(self))nn def on_connection(self, connection):n self.print_message("connected successfully!")n self.connection = connectionnn def send_message(self, *args):n msg = self.textbox.textn if msg and self.connection:n self.connection.write(str(self.textbox.text))n self.textbox.text = ""nn def print_message(self, msg):n self.label.text += msg + "n"nnif __name__ == __main__:n TwistedClientApp().run()n

推薦閱讀:

關於python Django與Flask學習的一些疑惑?
Python從零開始系列連載(25)——Python特色數據類型(集合)(下)
某測試模擬器性能優化-使用PyPy提升Python程序性能
PyQt5系列教程(16):小車快跑(滑塊的使用)

TAG:Python | Kivy |