Search code examples
pythonpython-2.7user-interfacekivy

Running multiple Kivy apps at same time that communicate with each other


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 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 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:

I get the same result if instead of ChildApp().run() I do multiprocessing.Process(target=ChildApp().run()).start()

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.

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.


Solution

  • bj0's answer regarding subprocess was correct.

    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()