Pylab.show () "QCoreApplication :: exec: the event loop is already running"

I have a basic image viewer in Python using matplotlib and QT4Agg Backend. One of the functions is to generate a 2D-Power Spectrum from a 2D-FFT and place it on a second plot within a single shape. What I would like to add is a button that takes an FFT image and pops it into another window so that it can be analyzed in more detail.

At the moment, my code does whatever I want, but it also raises a QT exception or warning of some kind to the terminal (this is done under OS X):

"QCoreApplication :: exec: the event loop is already running"

def popout(event):
    nfig, nfftax = pylab.subplots(1, 1)
    imgtoconv = getArr(files[imgindex])
    fftimg = np.abs(fftpack.fftshift(fftpack.fft2(imgtoconv)))**2
    nfftax.imshow(np.log10(fftimg), cmap=cm.Greys_r)
    pylab.show()

      

I have a button defined and associated with a popout () function using Button.on_clicked (popout) In the main loop of the program, two plots are initiated and displayed. So pylab.show () is already being called

The problem is when I call pylab.show () again, which throws an exception or warning that the event loop has already started.

I tried to use pylab.draw () instead of pylab.show (), but when I do this, the button does not show the new digit

Is there an easy way to suppress this warning? Or is there another way to create a new shape and make it visible without recalling pylab.show ()?

EDIT: I also tried adding nfig.canvas.draw () after creating a new shape and new axes, and then calling pylab.draw () at the end of the popout () function. It doesn't show the new figure onclick yet

EDIT2: Switching to using the TKAgg Backend and calling pylab.show () makes the problem go away. Any ideas as to why this is only a problem in the QT4Agg Backend?

+3


source to share


1 answer


Have you tried using ion()

? I had the same problem when using multiple digits from a button callback. For me, this code inside a function popout()

solved the problem:

import matplotlib.pyplot as p
import numpy as np
p.ion()
p.close(1)
p.close(2)

p.figure(1)
p.plot(np.random.normal(size=100))

p.figure(2)
p.plot(np.random.normal(size=100))

      



As I understand it, this forces matplotlib to use its own threads to process all shapes without conflicting with the main loop of your GUI. I close the digits completely reset and free the memory, but p.clf()

works as well.

+3


source







All Articles