Fix the flickering problem

Thanks to @ito725.

The patch was taken from: https://github.com/sympy/sympy/issues/3971#issuecomment-61500458