在Python中使用MatPlotLib时,Linux GUI会被冻结

时间:2017-05-16 15:06:44

标签: linux python-2.7 matplotlib pyqt pyqt4

我在PyQt应用程序中发现了一个奇怪的行为,它冻结了我操作系统的整个图形界面(在Linux Mint 17.1和18.0,Red Hat Enterprise Linux Server 7.3版上测试过)。对于Windows,它似乎表现正常(即没有冻结)。

点击Click me按钮后,一些计算从我们calculateSomething的方法Worker开始,它在不同的线程中工作(在这种情况下,只需sleep 3秒) 。完成后,信号将发送到MainWindow以显示matplotlib plot

一切正常,直到您在主菜单中按File并在计算过程中保持打开状态。然后,当弹出绘图窗口时,整个图形界面被冻结。

任何想法是怎么回事?对我来说似乎是一个奇怪的错误。或者我做错了什么?谢谢。

以下是演示行为的示例代码:

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_())

0 个答案:

没有答案