+-
python – 同时运行多个Kivy应用程序,相互通信
我希望我的Kivy应用程序能够在可以相互通信的 Windows机器上生成多个应用程序(即新窗口).

ScreenManager和Popup选项不会削减它,因为它们位于同一窗口中.我需要能够跨多个监视器拖动新屏幕,因此需要多个窗口.

Kivy docs明确声明“Kivy supports only one window
per application: please don’t try to create more than one.”

谷歌搜索生成this simple approach简单的从另一个应用程序中生成一个新的应用程序,如下所示:

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

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

Original exception was:
Error in sys.exceptionhook:

我得到了相同的结果,而不是ChildApp().run()我做multiprocessing.Process(target = ChildApp().run()).start()

使用子流程库让我更接近我想要的东西:

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

这会生成子窗口而不会出现错误,但是现在主窗口被锁定(白色画布),如果我关闭子窗口,它就会重新打开.

他们需要能够在彼此之间传递数据.有关如何在Windows中正确执行此操作的任何想法?这post似乎表明这是可能的,但我不知道从哪里开始.

最佳答案
我尝试了baconwichsand的代码,可以用Python 3.6和Windows 10确认它不起作用.显然只有顶级对象类可以被pickle,并且因为两个应用程序都继承自App类python会引发错误.但是,只需执行ChildApp().run()命令的顶级定义就可以进行pickle和工作.这是我的工作代码.

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

这是我正在使用的代码,包括Builder为两个窗口使用共享的.kv文件.

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()
点击查看更多相关文章

转载注明原文:python – 同时运行多个Kivy应用程序,相互通信 - 乐贴网