問題描述
我希望我的 Kivy 應用程序能夠在 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 和 Popup 選項不會削減它,因為它們位于同一個窗口中..我需要能夠拖動新屏幕多個顯示器,因此需要多個窗口.
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 僅支持一個窗口每個應用程序:請不要嘗試創(chuàng)建多個."
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()
而不是 ChildApp().run()
我會得到相同的結果代碼>
I get the same result if instead of ChildApp().run()
I do 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()
這會毫無錯誤地生成子窗口,但是現(xiàn)在主窗口被鎖定(白色畫布),如果我關閉子窗口,它就會重新打開.
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.
他們需要能夠在彼此之間傳遞數(shù)據(jù).關于如何在 Windows 中正確執(zhí)行此操作的任何想法?這個 post 似乎表明這是可能的,但我不知道從哪里開始.
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 應用程序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!