绘制直方图会出现错误“QCoreApplication :: exec:事件循环已在运行”

时间:2015-08-13 11:44:08

标签: python matplotlib pyqt4 histogram

我正在使用matplotlib绘制直方图并在控制台中收到此警告。我已经看到了关于这个主题的几个问题,但我仍然无法理解代码中的问题。

import numpy as np

from matplotlib import pyplot as plt

class ishani(QtGui.QWidget):

    def __init__(self):
        super(ishani, self).__init__()

        hbox = QtGui.QHBoxLayout(self)
        top = QtGui.QFrame(self)
        top.setFrameShape(QtGui.QFrame.StyledPanel)
        bottomleft = QtGui.QFrame(self)
        bottomleft.setFrameShape(QtGui.QFrame.StyledPanel)
        bottomright = QtGui.QFrame(self)
        bottomright.setFrameShape(QtGui.QFrame.StyledPanel)
        splitter1 = QtGui.QSplitter(QtCore.Qt.Vertical)
        splitter1.addWidget(top)
        splitter2 = QtGui.QSplitter(QtCore.Qt.Horizontal)
        splitter2.addWidget(bottomleft)
        splitter2.addWidget(bottomright)
        splitter1.addWidget(splitter2)
        hbox.addWidget(splitter1)
        self.setLayout(hbox)
        QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
        self.setGeometry(200, 200, 500, 400)
        self.setWindowTitle('QtGui.QSplitter')

        self.show()
        browseButton = QtGui.QPushButton("Browse")
        browseButton.clicked.connect(self.loadFromFile)
        histButton = QtGui.QPushButton("Histogram")
        histButton.clicked.connect(self.loadFromHist)
        self.lbl= QtGui.QLabel()
        bottomleftLayout = QtGui.QHBoxLayout()
        self.im = "\Users\Public\Pictures\ishani\lord.jpg"
        self.pixmap = QtGui.QPixmap(self.im)
        self.lbl.setPixmap(self.pixmap)

        bottomleftLayout.addWidget(self.lbl)
        bottomleft.setLayout(bottomleftLayout)

        browseButton.setParent(top)
        histButton.setParent(top)
        browseButton.move(10,125)
        histButton.move(80,125)
        browseButton.resize(60,40)
        histButton.resize(60,40)
        browseButton.show()
        histButton.show()


    def loadFromFile(self):
        self.fileName = QtGui.QFileDialog.getOpenFileName(
        self,
        "Select an image",
        "\Users\Public\Pictures\ishani",
        "Image Files(*.png *.gif *.jpg *jpeg *.bmp)"
        )

        if self.fileName != "":
            self.image = QtGui.QImage(QtGui.QImageReader(self.fileName).read())
            self.lbl.setPixmap(QtGui.QPixmap.fromImage(self.image))

    def loadFromHist(self):

        filename = self.im  
        img=cv2.imread("\Users\Public\Pictures\ishani\lord.jpg")

        if filename != "":
            img = cv2.imread(filename)

            color = ('b','g','r')
            for i,col in enumerate(color):
                histr = cv2.calcHist([img],[i],None,[256],[0,256])
                plt.plot(histr,color = col)
                plt.xlim([0,256])
                plt.show()

if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)

    app.setApplicationName('ishani')
    main = ishani()
    main.show()    

    app.exec_() 

0 个答案:

没有答案