How to save a value to be used after app.exec() exits in python?
<p>I would like to save/store mouse event values that were created in app.exec() as it was running. I would like to use the following code that I got from a post that I am having trouble finding now.(Will update with link to post where this code came from, once I find it.)</p>

<pre><code>import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
from PyQt4.QtCore import * 

import numpy as np

class DrawImage(QMainWindow): 

  def __init__(self,fName, parent=None):

    ## Default values
    self.x = 0
    self.y = 0

    super(QMainWindow, self).__init__(parent)

    self.setWindowTitle('Select Window')
    self.local_image = QImage(fName)

    self.local_grview = QGraphicsView()
    self.setCentralWidget( self.local_grview )

    self.local_scene = QGraphicsScene()

    self.image_format = self.local_image.format()
    #self.pixMapItem = self.local_scene.addPixmap( QPixmap(self.local_image) )
    self.pixMapItem = QGraphicsPixmapItem(QPixmap(self.local_image), None, self.local_scene)

    self.local_grview.setScene( self.local_scene )

    self.pixMapItem.mousePressEvent = self.pixelSelect


  def pixelSelect( self, event ):
    # print(event.pos().x(), event.pos().y())
    self.x = event.pos().x()
    self.y = event.pos().y()
    print(self.x, self.y)

def main():
  # Initialize  
  fName = "N2-600-PSI-V1-40-30ms-1.tiff"


  app = QtGui.QApplication(sys.argv)
  form = DrawImage(fName)
  form.show()
  app.exec_()

  x,y = app.exec_()

  print(x,y)

  return




if __name__ == '__main__':
  main()
</code></pre>

<p>My first attempt was to create two global variables, which then I used in the pixelSelect function to hold the values from event.pos().x() and .()y.</p>

<p>This worked however... ultimately I would like to pass more than just one set of coordinates out of the app.exec() loop...(process?? its a strange beast)</p>

<p>So from this point I have tried several different methods to pass an array into the app.exec() to save more values. The best result I have gotten thus far has been by using a global array and trying to make a for loop happen in the the DrawImage class.</p>

<p>Any pointers would be great :)</p>

<p>Have a good one!</p>

<p><code>app.exec_()</code> is not doing anything magical, it simply starts the event loop that handles all the GUI events. Your main function will block here until the GUI is shut down and the event loop exits. At this point the objects you have created are still in scope, that is, they have not been garbage collected, they just can't do anything that relies on the Qt event loop.</p>

<p>You can simply access the members of <code>DrawImage</code> to retrieve what you need.</p>

<pre><code>from PyQt4 import QtCore, QtGui

class DrawImage(QtGui.QMainWindow):
    def __init__(self, fname, **kwargs):
        super(DrawImage, self).__init__(**kwargs)
        self.setWindowTitle('Select Window')

        scene = QtGui.QGraphicsScene()
        gview = QtGui.QGraphicsView()
        gview.setScene(scene)
        self.setCentralWidget(gview)
        image = QtGui.QImage(fname)
        pixmapitem = QtGui.QGraphicsPixmapItem(QtGui.QPixmap(image), None, scene)
        pixmapitem.mousePressEvent = self.pixelSelect

        self.points = []

    def pixelSelect(self, event):
        x, y = event.pos().x(), event.pos().y()
        self.points.append((x, y))
        print x, y


if __name__ == '__main__':
    fname = 'N2-600-PSI-V1-40-30ms-1.tiff'

    app = QtGui.QApplication([])
    form = DrawImage(fname)
    form.show()
    app.exec_()

    for point in form.points:
        print point
</code></pre>

<p>Presumably you want to actually <em>do something</em> with the values your application generates though, rather than just print them. There's no reason why you can't (and you should!) handle any further processing from within the Qt application.</p>

python qt user-interface pyqt