pyqtgraph: simplest way to pause ipython script until user has placed a roi object
<p>I would like to implement the following sequence in a script and keep it as simple as possible (i.e., if possible avoid explicit multi-threading):</p>

<ol>
<li><p>Process some data. The result is a 2d numpy array, say <code>a</code></p></li>
<li><p>Show <code>a</code> using <code>pw = pg.show(a)</code> (after <code>import pyqtgraph as qt</code> and using pyqt5)</p></li>
<li><p>Define a circular roi, e.g. via</p></li>
</ol>

<p><code>circ = pg.CircleROI([1024,1024],300)
pw.addItem(circ)</code></p>

<ol start="4">
<li><p>The user moves the roi to the relevant place</p></li>
<li><p>Read out the roi coordinates, continue with the script (where the roi coordinates are used)</p></li>
</ol>

<p>My question is: How can I define a break in the script between 3. and 5. such that the user has time to do 4., in a way that the pyqtgraph is not blocked? Ideally, the user would confirm the correct roi position by pressing enter or clicking a button.</p>

<p>Edit: The script is executed in IPython with qt gui.</p>

<p>Edit2: Here is a full test script to be executed in IPython. What I want is that the user can move the circle before the roi is evaluated, i.e., that the print output is something different than <code>(slice(1024, 1174, None), slice(1024, 1174, None))</code></p>

<pre><code>import numpy as np
import pyqtgraph as pg

a = np.array(range(2048**2)).reshape((2048,2048))
pw = pg.show(a)
circ = pg.CircleROI([1024,1024],300)
pw.addItem(circ)



roi = np.s_[int(circ.pos().x()):int(circ.pos().x()+circ.size().x()/2),\
            int(circ.pos().y()):int(circ.pos().y()+circ.size().x()/2)]

print(roi)
</code></pre>

<h1>1. raw_input('') in an IPython console</h1>

<p>If the script is running in an IPython console, you could try adding a </p>

<pre><code>raw_input("Press Enter to continue...")
</code></pre>

<p>or <code>input()</code> in python3 to pause the script. The user has to go back and press enter in the ipython console but the code is simple.</p>

<pre><code>import numpy as np
import pyqtgraph as pg

a = np.array(range(2048**2)).reshape((2048,2048))
pw = pg.show(a)
circ = pg.CircleROI([1024,1024],300)
pw.addItem(circ)

raw_input("Press Enter to continue...")

roi = np.s_[int(circ.pos().x()):int(circ.pos().x()+circ.size().x()/2),\
            int(circ.pos().y()):int(circ.pos().y()+circ.size().x()/2)]

print(roi)
</code></pre>

<h1>2. Monkey patching keyPressEvent</h1>

<p>Another solution could be to monkey patch the keyPressEvent in the ImageWindow.</p>

<p>Beware that this solution uses both <strong>globals</strong> and <strong>monkey patching</strong>, please make sure you know what this means.</p>

<pre><code>import numpy as np
from PyQt4 import QtCore, QtGui
import pyqtgraph as pg


a = np.array(range(2048**2)).reshape((2048,2048))
pw = pg.show(a)
circ = pg.CircleROI([1024,1024],300)
pw.addItem(circ)

def myKeyPressEvent(e):
    if e.key() == QtCore.Qt.Key_Enter or e.key() == QtCore.Qt.Key_Return:
        global selectionFinished
        selectionFinished = True

# Monkey patch
selectionFinished = False
pw.keyPressEvent = myKeyPressEvent

while not selectionFinished:
    QtGui.QApplication.processEvents()

roi = np.s_[int(circ.pos().x()):int(circ.pos().x()+circ.size().x()/2),\
            int(circ.pos().y()):int(circ.pos().y()+circ.size().x()/2)]

print(roi)
</code></pre>

python ipython jupyter-notebook pyqtgraph