我想开发一个基于Kivy运行应用程序的远程控制框架。我们的想法是使用PodSixNet(或类似的网络层进行客户端 - 服务器通信)来远程启动/控制/停止Kivy应用程序。这是基于运行外部事件循环(用于网络事件),但是如何从Kivy的职责中获取我想要运行的任何Kivy应用程序的事件循环?
from kivy.app import App
from kivy.uix.button import Button
from PodSixNet.Connection import connection, ConnectionListener
class App1(App):
def build(self):
return Button(text='hello world 1')
class App2(App):
def build(self):
return Button(text='hello world 2')
class Client(ConnectionListener):
def __init__(self, *kargs, **kwargs):
ConnectionListener.__init__(self, *kargs, **kwargs)
self.Connect((kwargs['host'], kwargs['port']))
self.current_app = App1()
self.current_app.run()
def Network_switchGame(self, data):
"""Gets triggered if appropriate message is sent from server"""
if isinstance(self.current_game, App1):
self.current_app.stop()
self.current_app = App2()
else:
self.current_app.stop()
self.current_app = App1()
self.current_app.run()
def Loop(self):
"""This function takes care of network events, and app events
(if there is a valid app)"""
connection.Pump()
self.Pump()
if self.current_game:
# this should run the Kivy app's event loop
self.current_game.events()
host, port = sys.argv[1].split(":")
c = Client(host=host, port=int(port))
while 1:
c.Loop()
我想运行单独的应用程序,因为它们会有不同的逻辑,后来应该添加新应用程序而不会有太多麻烦。 如果重要:这最终将在Raspberry Pi上运行(并在Mac上进行开发)。
编辑:潜在解决方案?
好的,似乎我可以嵌套这样的应用程序:
from kivy.app import App
from kivy.properties import ListProperty, ObjectProperty, NumericProperty
from kivy.uix.button import Button
from kivy.clock import Clock
class OutsideApp(App):
current_app = ObjectProperty(None)
elapsed = NumericProperty(0)
def build(self):
Clock.schedule_interval(self.update, 1.0 / 60)
self.current_app = App1()
self.current_app.run()
return Button(text='fff 1')
def update(self, dt):
self.elapsed += dt
if self.elapsed > 3:
self.elapsed = 0
if self.current_app:
self.current_app.stop()
if isinstance(self.current_app, App1):
self.current_app = App2()
else:
self.current_app = App1()
self.current_app.run()
class App1(App):
def build(self):
return Button(text='hello world 1')
class App2(App):
def build(self):
return Button(text='hello world 2')
oa = OutsideApp()
oa.run()
这是怎么做的?