启动程序时需要保存来自网络摄像头的两张图片,但仅保存第一张图片。
import sys
from PyQt5.Qt import *
class MyWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
self.viewfinder = QCameraViewfinder()
self.cameraInfo = QCameraInfo.defaultCamera()
self.camera = QCamera(self.cameraInfo)
self.camera.setViewfinder(self.viewfinder)
self.camera.setCaptureMode(QCamera.CaptureStillImage)
self.layout.insertWidget(1, self.viewfinder)
if self.camera.status() != QCamera.UnavailableStatus:
self.camera.start()
self.capture = QCameraImageCapture(self.camera)
# Сохранение снимков
for i in range(2):
self.capture.capture('C:/pyqt_' + str(i) + '.jpg')
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.widget1 = MyWidget(self)
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
main_layout = QVBoxLayout(self.central_widget)
main_layout.addWidget(self.widget1)
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setApplicationName("pyqt5")
window = MainWindow()
window.show()
sys.exit(app.exec())
将打印插入循环中,您将看到一切:
您需要更改逻辑,例如这样: