python同时运行两个程序_python – 同时运行多个Kivy应用程序,相互通信

我希望我的Kivy应用程序能够在可以相互通信的Windows机器上生成多个应用程序(即新窗口).

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

谷歌搜索生成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,user-interface,python-2-7,kivy

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值