在尝试在 PyQt4 窗口中显示网络摄像头提要(使用 imageio/ffmpeg 获得)的不同方式时,我偶然发现了这个答案。在 Python 2.7 中将其作为一个ImageDisplayWidget
类实现后(如下所述),一切似乎都运行良好:打开一个窗口,显示我的网络摄像头提要没有故障。如果我关上窗户,一切都会停止并整齐地关闭。
但是...每当我单击此 PyQt 窗口外的任何位置(当它显示网络摄像头馈送时),导致它失去焦点时,Python.exe 会因未处理的 win32 异常而崩溃。当我尝试调整窗口大小时也会发生同样的情况。
我可能犯了某种非常愚蠢的初学者错误,但我只是不明白。谁能指出我正确的方向?我是否违反了(Py)Qt 甚至 Python 的一些基本规则?
这是一个最小的例子:
import sys
import numpy
from PIL import Image, ImageQt # pillow
from PyQt4 import QtGui, QtCore
class DummyVideoGrabber(QtCore.QTimer):
signal_image_available = QtCore.pyqtSignal(QtGui.QImage)
def __init__(self):
super(DummyVideoGrabber, self).__init__()
self.timeout.connect(self.update_image)
self.counter = 0
def update_image(self):
# Dummy rgb image (in reality we get a numpy array from imageio's Reader)
self.counter += 1
numpy_image = numpy.zeros(shape=(480, 640, 3), dtype=numpy.int8)
numpy_image[:, :, self.counter%3] = 255
qt_image = ImageQt.ImageQt(Image.fromarray(numpy_image, mode='RGB'))
# Emit image
self.signal_image_available.emit(qt_image)
class ImageDisplayWidget(QtGui.QWidget):
"""
Custom widget that displays an image using QPainter.
Mostly copied from: https://stackoverflow.com/a/22355028/4720018
"""
def __init__(self, size_wxh=None, parent=None):
super(ImageDisplayWidget, self).__init__(parent)
self.image = QtGui.QImage()
def set_image(self, qimage, resize_window=False):
self.image = qimage
self.repaint()
def paintEvent(self, QPaintEvent):
if not self.image:
return
painter = QtGui.QPainter(self)
painter.drawImage(self.rect(), self.image, self.image.rect())
app = QtGui.QApplication(sys.argv)
# instantiate a display object
display = ImageDisplayWidget()
display.resize(640, 480)
display.show()
# instantiate a grabber object
grabber = DummyVideoGrabber()
grabber.signal_image_available.connect(display.set_image)
grabber.start(100) # timer interval in ms
# start the event loop
app.exec_()
我发现可以通过添加一个wasActiveWindow
标志(在构造函数中初始化为True
)并将drawImage()
调用封装在一些逻辑中来防止崩溃,如下所示:
if self.isActiveWindow():
if self.wasActiveWindow:
painter.drawImage(self.rect(), self.image, self.image.rect())
self.wasActiveWindow = True
else:
self.wasActiveWindow = False
但是,调整窗口大小仍然会使 python 崩溃。