Python使用pyqt,setPixmap和QLabel崩溃

时间:2015-04-23 23:04:30

标签: python python-2.7 pyqt

我在Windows 7 64位计算机上使用此python代码,它只显示随机生成的黑白图像。如果我使图像大于511x511像素,我的python 2.7控制台崩溃。它在我的Mac上工作正常。有什么想法吗?

import sys
from PyQt4 import QtGui, QtCore
import numpy as np


class PixmapTest(QtGui.QWidget):

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

        imglayout = QtGui.QHBoxLayout(self)

        size = 512
        img_8bit = (256*np.random.random((size,size))).astype(np.uint8)     

        img = QtGui.QImage(img_8bit.repeat(4), size, size, QtGui.QImage.Format_RGB32)

        pixmap = QtGui.QPixmap(img)

        imglabel = QtGui.QLabel(self)
        imglabel.setPixmap(pixmap)

        imglayout.addWidget(imglabel)
        self.setLayout(imglayout)

        self.show()        

def main():

    app = QtGui.QApplication(sys.argv)
    form = PixmapTest()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:3)

奇怪,对我来说(Win7,64位)截止大小接近300.我不知道为什么它的行为像这样但在我的情况下我可以通过调用processEvents来解决它应用程序,像这样:

class PixmapTest(QtGui.QWidget):
    def __init__(self, app):
        super(PixmapTest, self).__init__()
        self.app = app
        imglayout = QtGui.QHBoxLayout(self)
        size = 333
        img_8bit = (256*np.random.random((size,size))).astype(np.uint8)     
        img = QtGui.QImage(img_8bit.repeat(4), size, size, QtGui.QImage.Format_RGB32)
        pixmap = QtGui.QPixmap(img)
        imglabel = QtGui.QLabel(self)
        imglabel.setPixmap(pixmap)
        imglayout.addWidget(imglabel)
        self.setLayout(imglayout)
        self.show()        
        self.app.processEvents()

def main():
    app = QtGui.QApplication(sys.argv)
    form = PixmapTest(app)
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()