1

I have found a strange behavior in my PyQt application which freezes whole Graphical interface of my OS (tested on Linux Mint 17.1 and 18.0, Red Hat Enterprise Linux Server release 7.3). For Windows, it seems to behave normally (i.e., no freezing).

After clicking Click me button, some calculations start in method calculateSomething of our Worker, which works in different thread (in this case, just sleep for 3 seconds). Once it's done, signal is emitted to the MainWindow to display a matplotlib plot.

Everything works normal until you press File in the main menu and keep it opened while computation is in progress. Then, when the plot window pops up, whole Graphical interface is frozen.

Any ideas what's going on? Seems like strange bug for me. Or is there anything I am doing wrong? Thank you.

Here is an example code which demonstrates the behavior:

import sys
from time import sleep
from PyQt4 import QtGui, QtCore
import matplotlib.pyplot as plt

# -------------------------------------------------

# Simulates a Worker which does a longer time calculation
class Worker(QtCore.QObject):
    calculationFinished = QtCore.pyqtSignal()   # emited when calculation is finished   
    def __init__(self, parent=None):
        QtCore.QObject.__init__(self, parent)

        self.TheWorker = QtCore.QThread()
        self.moveToThread(self.TheWorker)
        self.TheWorker.start()

    def calculateSomething(self):
        sleep(3) # wait for 3 seconds
        self.calculationFinished.emit() # emit the signal

# -------------------------------------------------

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.one = QtGui.QAction("One", self)
        self.two = QtGui.QAction("Two", self)

        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('File')

        fileMenu.addAction(self.one)
        fileMenu.addAction(self.two)

        self.myWorker = Worker()
        self.myWorker.calculationFinished.connect(self.showPopUp)

        self.button = QtGui.QPushButton("Click me", self)
        self.button.clicked.connect(self.myWorker.calculateSomething)
        self.button.move(60, 30)

    def showPopUp(self):
        plt.plot([1,2,3,4])
        plt.ylabel('some numbers')
        plt.show()

app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
4
  • Can you close the matplotlib window, or is that frozen as well?
    – bnaecker
    Commented May 16, 2017 at 15:56
  • You can't use graphical interface of whole OS at all, so no, you can't close matplotlib window.
    – Matho
    Commented May 16, 2017 at 16:07
  • Works fine on Ubuntu 16.10. Which version of matplotlib are you using? Maybe the pyplot event loop is interfering with PyQt. I would try embedding your plot in a PyQt window instead of using pyplot Commented May 16, 2017 at 16:33
  • Yes, you are right, embedding the plot into PyQt helped, thank you. However, still can't understand why the behavior of pyplot is so strange.
    – Matho
    Commented May 17, 2017 at 14:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.