同时运行多个可相互通信的Kivy应用程序 [英] Running multiple Kivy apps at same time that communicate with each other

查看:68
本文介绍了同时运行多个可相互通信的Kivy应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Kivy应用程序能够在可以相互通信的Windows机器上产生多个应用程序(即新Windows).

I would like my Kivy application to be able to spawn multiple apps (i.e. new windows) on a Windows machine that can communicate with each other.

ScreenManager

ScreenManager and Popup options will not cut it because they live in the same window..I need to be able to drag new screens across multiple monitors and therefore need multiple windows.

Kivy文档明确声明"Kivy仅支持一个窗口 每个应用程序:请不要尝试创建多个应用程序."

Kivy docs explicitly state that "Kivy supports only one window per application: please don't try to create more than one."

谷歌搜索会生成此简单生成的简单方法另一个应用中的一个新应用,例如:

A google search produces this simple approach of simple spawning a new app from within another app, like so:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label


class ChildApp(App):
    def build(self):
        return Label(text='Child')


class MainApp(App):

    def build(self):
        b = Button(text='Launch Child App')
        b.bind(on_press=self.launchChild)
        return b

    def launchChild(self, button):
        ChildApp().run()

if __name__ == '__main__':
    MainApp().run()

但是,当我这样做时,它会在同一窗口中启动该应用程序并崩溃,并且我的终端会疯狂地吐出来:

However, when I do this, it launches the app within the same window and crashes, and my terminal spits out like crazy:

Original exception was:
Error in sys.exceptionhook:

如果我使用multiprocessing.Process(target=ChildApp().run()).start()

使用subprocess库使我更接近想要的内容:

Using the subprocess library gets me closer to what I want:

# filename: test2.py

from kivy.app import App
from kivy.uix.label import Label


class ChildApp(App):
    def build(self):
        return Label(text='Child')

if __name__ == '__main__':
    ChildApp().run()


# filename: test.py

from kivy.app import App
from kivy.uix.button import Button

import subprocess


class MainApp(App):

    def build(self):
        b = Button(text='Launch Child App')
        b.bind(on_press=self.launchChild)
        return b

    def launchChild(self, button):
        subprocess.call('ipython test2.py', shell=True)

if __name__ == '__main__':
    MainApp().run()

这会无误地生成子窗口,但是现在主窗口已锁定(白色画布),如果我关闭子窗口,它将重新打开.

This spawns the child window without error, however now the main window is locked (white canvas) and if I close the child window, it just gets reopened.

他们需要能够彼此之间传递数据.关于如何在Windows中正确执行此操作的任何想法?这篇帖子似乎暗示这是可能的,但我不确定从哪里开始.

They need to be able pass data between one another. Any ideas on how to do this correctly in Windows? This post seems to suggest that this is possible but I'm not sure where to start.

推荐答案

bj0关于子流程的答案是正确的.

bj0's answer regarding subprocess was correct.

更好的是,我想出了如何通过多处理来做到这一点,从而可以更好地在应用之间进行交流和信息传递.以前没有用,因为我在应该是multiprocessing.Process(target=ChildApp().run).start()的时候做了multiprocessing.Process(target=ChildApp().run()).start().以下作品

Even better, I figured out how to do this via multiprocessing, which allows better communication and passing of information between apps. It wasn't working before because I did multiprocessing.Process(target=ChildApp().run()).start() when it should be multiprocessing.Process(target=ChildApp().run).start(). The following works

# filename: test.py

from kivy.app import App
from kivy.uix.button import Button

from test2 import ChildApp

import multiprocessing


class MainApp(App):

    def build(self):
        b = Button(text='Launch Child App')
        b.bind(on_press=self.launchChild)
        return b

    def launchChild(self, button):
        app = ChildApp()
        p = multiprocessing.Process(target=app.run)
        p.start()

if __name__ == '__main__':
    MainApp().run()


# filename: test2.py

from kivy.app import App
from kivy.uix.label import Label


class ChildApp(App):
    def build(self):
        return Label(text='Child')

if __name__ == '__main__':
    ChildApp().run()

这篇关于同时运行多个可相互通信的Kivy应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆