Python Kivy和外部事件循环

Python Kivy和外部事件循环,python,kivy,Python,Kivy,我想开发一个基于Kivy运行应用程序的远程控制框架。其想法是使用(或用于客户端-服务器通信的类似网络层)远程启动/控制/停止Kivy应用程序。这是基于运行一个外部事件循环(用于网络事件),但是我如何从Kivy的责任中获取我想要运行的任何Kivy应用程序的事件循环 from kivy.app import App from kivy.uix.button import Button from PodSixNet.Connection import connection, ConnectionLis

我想开发一个基于Kivy运行应用程序的远程控制框架。其想法是使用(或用于客户端-服务器通信的类似网络层)远程启动/控制/停止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()

应该这样做吗?

要手动运行Kivy的事件循环,请查看
Kivy.base.runTouchApp
,其中包含了一些有关执行此操作的信息。文档是否比?我找到的唯一其他参考资料是,但也不详细。查看
kivy.App.run()
的源代码表明,我必须在
runTouchApp()之前复制整个创建序列?最后,是否有理由不在编辑中嵌套应用程序?runTouchApp不需要所有应用程序结构。我不完全理解你想做什么,但如果你的编辑做了你想要的,那就好了。