两个文件。每个都运行新窗口并自行工作。我需要同时运行它们。
当我运行 first.pyw
时,只显示一个(第二个)窗口。
是否可以同时运行它们?
第一个.pyw:
import sys
from PyQt4.QtGui import *
import second
class first(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setWindowTitle('first')
app = QApplication(sys.argv)
firstApp = first()
firstApp.show()
sys.exit(app.exec_())
第二个.pyw:
import sys
from PyQt4.QtGui import *
class second(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setWindowTitle('second')
app2 = QApplication(sys.argv)
secondApp = second()
secondApp.show()
sys.exit(app2.exec_())
如何运行位于不同模块中的两个应用程序?
最佳答案
接受的答案基本上是正确的,但在某些情况下您希望一个接一个地运行多个 QApplications,例如:
- 单元测试
- 一个命令行工具,不需要运行 X 服务器(因此启动时没有 QApplication),但如果用户系统支持,可以选择显示一个窗口
我最终使用 multiprocessing
模块在单独的进程中启动每个 QApplication
,这样每个进程都相互独立。
from multiprocessing import Queue, Process
class MyApp(Process):
def __init__(self):
self.queue = Queue(1)
super(MyApp, self).__init__()
def run(self):
app = QApplication([])
...
self.queue.put(return_value)
app1 = MyApp()
app1.start()
app1.join()
print("App 1 returned: " + app1.queue.get())
app2 = MyApp()
app2.start()
app2.join()
print("App 2 returned: " + app1.queue.get())
关于python - PyQt - 是否可以运行两个应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6142098/