如何在PyQT4中为显示和控制按钮添加QPixmap

时间:2018-11-11 08:08:42

标签: python pyqt pyqt4

我刚开始pyqt4。 我的UI有两个选项卡Face和Human。 当我按下“脸部”选项卡或“人”选项卡时,我应该看到显示区域在左侧显示来自网络摄像头的图像,在右侧显示“控制”组框(包括按钮,单选按钮等)。

我不知道如何制作整个UI,但要准备各个部分。 我现有的代码如下。

我应该如何继续?

import sys
import cv2
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Thread(QThread):
    changePixmap = pyqtSignal(QImage)

    def run(self):
        cap = cv2.VideoCapture(0)
        while True:
            ret, frame = cap.read()
            if ret:
                rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                convertToQtFormat = QImage(rgbImage.data, rgbImage.shape[1], rgbImage.shape[0], QImage.Format_RGB888)
                p = convertToQtFormat.scaled(640, 480, Qt.KeepAspectRatio)
                self.changePixmap.emit(p)


class Display(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    @pyqtSlot(QImage)
    def setImage(self, image):
        self.label.setPixmap(   .fromImage(image))

    def initUI(self):
        self.setWindowTitle(self.title)        
        # create a label
        self.label = QLabel(self)
        self.label.move(280, 120)
        self.label.resize(640, 480)
        th = Thread(self)
        th.changePixmap.connect(self.setImage)
        th.start()

class Human(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setLayout(QGridLayout())

        #Add display

        #Add control components
        layout = QVBoxLayout()
        self.b1 = QPushButton("Button1")
        self.b1.setCheckable(True)
        self.b1.toggle()
        self.b1.clicked.connect(lambda:self.whichbtn(self.b1))
        self.b1.clicked.connect(self.btnstate)
        layout.addWidget(self.b1)

    def btnstate(self):
      if self.b1.isChecked():
         print "button pressed"
      else:
         print "button released"

    def whichbtn(self,b):
      print "clicked button is "+b.text()

class Face(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setLayout(QGridLayout())

        #Add display

        #Add control components
        layout = QVBoxLayout()
        self.b1 = QPushButton("Button1")
        self.b1.setCheckable(True)
        self.b1.toggle()
        self.b1.clicked.connect(lambda:self.whichbtn(self.b1))
        self.b1.clicked.connect(self.btnstate)
        layout.addWidget(self.b1)
    def btnstate(self):
      if self.b1.isChecked():
         print "button pressed"
      else:
         print "button released"

    def whichbtn(self,b):
      print "clicked button is "+b.text()

class Page1(QTabWidget):
    def __init__(self, parent=None):
        super(Page1, self).__init__(parent)
        self.face = Face()
        self.human = Human()
        self.addTab(self.face, "Face")
        self.addTab(self.human, "Human")

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setGeometry(200, 200, 800, 600)
        self.startPage1()

    def startPage1(self):
        x = Page1(self)
        self.setWindowTitle("Face & Human")
        self.setCentralWidget(x)
        self.show()

def main():
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

我喜欢看到的用户界面如下。

enter image description here

0 个答案:

没有答案