Well, for this minimal example we have one main window and one qdialog window. Every of these has one button.
When the user clicks in the button of main window the qdialog window is open.
When the user clicks in the button of qdialog, I want to run in multiprocessing (asynchronous) a function from mainwindow. But instead of these it opens again a new mainwindow.
Code:
untitled.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(318, 41)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.open_dialog = QtWidgets.QPushButton(self.centralwidget)
self.open_dialog.setObjectName("open_dialog")
self.gridLayout.addWidget(self.open_dialog, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.open_dialog.setText(_translate("MainWindow", "Open Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
dialog.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'dialog.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 41)
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setObjectName("gridLayout")
self.call_method_button = QtWidgets.QPushButton(Dialog)
self.call_method_button.setObjectName("call_method_button")
self.gridLayout.addWidget(self.call_method_button, 0, 0, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.call_method_button.setText(_translate("Dialog", "Call method from main Window"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
main.py
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from untitled import *
from dialog import Ui_Dialog
from dialog_code import Dialog_Code
class MainCode:
def __init__(self):
self.app = QtWidgets.QApplication(sys.argv)
self.mainWindow = QtWidgets.QMainWindow()
self.main_ui = Ui_MainWindow()
self.main_ui.setupUi(self.mainWindow)
self.mainWindow.show()
self.main_ui.open_dialog.clicked.connect(self.open_dialog_window)
sys.exit(self.app.exec_())
def open_dialog_window(self):
self.dialog_window = QtWidgets.QDialog(self.mainWindow)
self.ui_dialog_window = Ui_Dialog()
self.ui_dialog_window.setupUi(self.dialog_window)
self.dialog_window.show()
dialog_window_run_code = Dialog_Code(self)
def print_ok_123(self):
print("print_ok_123 method")
for i in range(0,10):
print("Ok")
return 1
program = MainCode()
dialog_code.py
from multiprocessing import Pool
class Dialog_Code:
def __init__(self,main_self):
print("Dialog init")
self.main_self = main_self
self.main_self.ui_dialog_window.call_method_button.clicked.connect(lambda:self.call_main_method())
def call_main_method(self):
print("Button pressed")
self.pool = Pool(processes=1)
result = self.pool.apply_async(self.main_self.print_ok_123, tuple(), self.print_ok_end_123)
self.pool.close()
self.pool.join()
def print_ok_end_123(self):
print("End of main method")
To run the code run python main.py
What's wrong with the pool statement, and how can i fix it?