PyQt - is it possible to run two applications?

Two files. Each of them launches a new window and works on its own. I need to run them both.
When I start first.pyw, only one (second) window is displayed.

Is it possible that they both start them both?

first.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_())

second.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_())

How can I run two applications that are in different modules?

+3
source share
3 answers

You can only run one application at a time, although your application may have multiple top-level windows. QCoreApplication docs say that:

... there should be only one QCoreApplication object.

QApplication, QCoreApplication. QCoreApplication.instance() qApp ++.

? , .

+1

, , 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())
+2

You import the second. Therefore, this is interpreted before you even get to the definition of a class in the first place. Since the last line of second.pyw is sys.exit, nothing behind it can be done.

0
source

All Articles