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_())
matplotlib
window, or is that frozen as well?matplotlib
window.matplotlib
are you using? Maybe thepyplot
event loop is interfering withPyQt
. I would try embedding your plot in aPyQt
window instead of usingpyplot
PyQ
t helped, thank you. However, still can't understand why the behavior ofpyplot
is so strange.