【发布时间】:2015-10-06 03:24:56
【问题描述】:
我希望我的 Kivy 应用程序能够在 Windows 机器上生成多个可以相互通信的应用程序(即新窗口)。
ScreenManager 和 Popup 选项不会削减它,因为它们位于同一个窗口中。我需要能够在多个显示器上拖动新屏幕,因此需要多个窗口。
Kivy 文档明确声明 "Kivy supports only one window per application: please don't try to create more than one."
Google 搜索会产生 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:
如果我用multiprocessing.Process(target=ChildApp().run()).start()代替ChildApp().run(),我会得到相同的结果
使用subprocess 库让我更接近我想要的:
# 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 似乎表明这是可能的,但我不知道从哪里开始。
【问题讨论】:
标签: python python-2.7 user-interface kivy