'Passing a label to a class in PyQt5

I am creating a hand tracking game where it takes an input from a camera, applies ML to show points on the fingers, and then actions can be performed based on the location of the fingers in the frame and the gesture.

I have a screen where the play will choose between two games. I want a modular approach as I will be wanting to using this webcam capture code multiple times for different game. So I want it to be its own file where I can just pass it a label from the current screen and It will show the video feed.

I can't get the video feed to show on a label I pass to the class.

Game choice screen -

class gameChoice(QWidget):

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

        self.resize(1200, 850)
        self.setLayout(QHBoxLayout())

        self.label = QLabel('VIDEO GOES HERE')
        self.layout().addWidget(self.label)
        self.label.setStyleSheet('border : 3px solid green;') # 

        # This is the class I want to call
        videoFeed(self.label)
        self.show()

Which produces: enter image description here

This is the code for the video feed -


class videoFeed(QWidget):
    
    def __init__(self, parent):
        super().__init__()

        self.disply_width = 1200
        self.display_height = 850

        # create the label that holds the webcam video
        self.image_label = parent
        # The line above it important
        self.image_label.resize(self.disply_width, self.display_height)
        self.image_label.setAlignment(Qt.AlignCenter)
        self.image_label.setStyleSheet('border : 3px solid blue;')

        self.closeButton = QPushButton( "Close the video", clicked = lambda : self.closeVideo())
        self.closeButton.setEnabled(False)
        self.closeButton.setFont(QFont('Helvetica', 15))

        # create a vertical box layout and add the two labels
        hbox = QHBoxLayout()
        hbox.addWidget(self.closeButton)

        vbox = QVBoxLayout()
        vbox.addWidget(self.image_label)
        vbox.addLayout(hbox)
        # set the vbox layout as the widgets layout
        self.setLayout(vbox)

        # this is a method to actually show the webcam feed
        self.videoFeed = VideoThread()

I've passed the label I want to use and called it parent in the videoFeed class and reassigned it in the class.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source