Running multiple Kivy apps at the same time that talk to each other

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

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

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

A google search produces this simple approach of simply creating a new app from another app, e.g .:

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 application in the same window and crashes and my terminal splashes out like crazy:

Original exception was:
Error in sys.exceptionhook:

      

I get the same result if instead ChildApp().run()

I domultiprocessing.Process(target=ChildApp().run()).start()

Using the library subprocess

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 opens again.

They must be able to transfer data between themselves. Any ideas on how to do this correctly on Windows? This post seems like it's possible, but I'm not sure where to start.

+3


source to share


3 answers


Answer to

bj0 relative to subprocess was correct.

Better yet, I figured out how to do this using multiprocessing, which allows for better communication and information transfer between applications. It didn't work before because I did multiprocessing.Process(target=ChildApp().run()).start()

when it should be multiprocessing.Process(target=ChildApp().run).start()

. Next work

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

      

+1


source


I tried baconwichsand code and can confirm that it doesn't work with Python 3.6 and Windows 10. Apparently only top level classroom classes can be pickled and since both applications inherit from the application class python throws an error. However, a top-level definition that just executes the ChildApp () command. Run (), you can pickle and work. Here is my working code.

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

class MainApp(App):
    def build(self):
        return Label(text='Main App Window')

class OtherApp(App):
    def build(self):
        return Label(text='Other App Window')

def open_parent():
    MainApp().run()

def open_child():
    OtherApp().run()

if __name__ == '__main__':
    a = multiprocessing.Process(target=open_parent)
    b = multiprocessing.Process(target=open_child)
    a.start()
    b.start()

      



And here is the code I am using including the Builder to use a common .kv file for both windows.

import multiprocessing
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.widget import Widget

class MainRoot(Widget):
    pass

class OtherRoot(Widget):
    pass

class MainApp(App):
    def build(self):
        Builder.load_file('B:\Python_Codes\Testing Grounds\shared.kv')
        main = MainRoot()
        return main

class OtherApp(App):
    def build(self):
        Builder.load_file('B:\Python_Codes\Testing Grounds\shared.kv')
        other = OtherRoot()
        return other

def open_parent():
    MainApp().run()

def open_child():
    OtherApp().run()

if __name__ == '__main__':
    a = multiprocessing.Process(target=open_parent)
    b = multiprocessing.Process(target=open_child)
    a.start()
    b.start()

      

+2


source


I'm not sure why it doesn't work with multiprocessing (I've never tried it), but it should at least work with subprocess

. The reason your main window is locked is because the subprocess.call

thread that calls it is blocking while it waits for the subprocess to complete and returns the result.

Instead, you want to use subprocess.Popen

one that does not block.

+1


source







All Articles