SoFunction
Updated on 2024-11-10

Pyqt5 in python using Qlabel tags for video playback

Pyqt5 installation and configuration to pycharm method:Teach you how to install pyqt5 and its related configurations with pycharm

I. Introduction

QLabel is the label class in the interface, inherited from the QFrame class, provides text and image display, and is a display control.

QLabel objects can display non-editable text or images, can hold an animated GIF, and can be used as hint tags for other controls.

Plain text, links or rich text can also be displayed on the label.

II. Basic usage

2.1 QLabel control

setAlignment(): aligns text in a fixed-value manner.There are the following alignments:

(horizontally aligned left), (horizontally aligned right), (horizontally aligned center), (horizontally spaced ends aligned), (vertically aligned top), (vertically aligned bottom), (vertically aligned center)

  • setIndent():Setting text indentation
  • setPixmap():Setting QLabel as a Pixmap Image
  • text():Getting the text content of a QLabel
  • setText():Setting the text content of a QLabel
  • selectedText():Returns the selected character
  • setBuddy():Setting up partnerships
  • setWordWrap():Set whether to allow line breaks

2.2 Signals (Events) Commonly Used by QLabel

1.linkHovered:When the mouse pointer slides over a hyperlink embedded in a label, a slot function is needed to bind to this signal

2.linkActivated:The setOpenExternalLinks feature must be set to true when clicking on a hyperlink embedded in a tab and wishing to open the hyperlink in a new window.

III. QLabel Playback Video

The focus of using QLabel to play video files lies in the **** timer QTimer

Timers are used when the time needs to be displayed in a program or when an operation needs to be performed periodically in a program

 3.1 QTimer

Import the QTimer module.

from  import QTimer

Initialization.

self.timer_camera = QTimer()

Timed and activated.

self.timer_camera.start(1000)   # 1000ms == 1s
self.timer_camera.()  # Connecting Slot FunctionsopenFrame

Note: When the parent object of a QTimer is destroyed, it is also destroyed automatically.

3.2 Code

UI interface:

python program:

from  import *
from  import *
from  import *
from  import loadUiType
import cv2
import sys
vedio_ui, _ = loadUiType('./UI/')

class VedioGui(QMainWindow, vedio_ui):
    # Define constructor methods
    def __init__(self):
        QMainWindow.__init__(self)
        (self)
        self.timer_camera = QTimer()
        self.handle_buttons()
        self.open_vedio()
    # All Button messages communicate with the slot
    def handle_buttons(self):
        self.btn_Start.(self.Btn_Start)
        self.btn_Stop.(self.Btn_Stop)
    def Btn_Start(self):
        # Timer on, every so often, read a frame #
        self.timer_camera.start(100)
        self.timer_camera.()
    def Btn_Stop(self):
        # ()
        self.timer_camera.stop()
    def open_vedio(self):
        """Select video file"""
        # Here's an example of mp4 and avi video playback
        openfile_name = (self, 'chose files', '', 'Image files(*.mp4 *.avi)')  # Open the file selection box to select the file
        self.file_name = openfile_name[0]  # Get the name of the image

        # Getting the file extension needs to be modified as appropriate #
        suffix = self.file_name.split("/")[-1][self.file_name.split("/")[-1].index(".") + 1:]
        # print(self.file_name, suffix)
        if self.file_name == '':
            pass
        elif suffix == "mp4" or suffix == "avi":
             = (self.file_name)
    def OpenFrame(self):
        ret, image = ()
        if ret:
            if len() == 3:
                image = (image, cv2.COLOR_BGR2RGB)
                vedio_img = QImage(, [1], [0], QImage.Format_RGB888)
            elif len() == 1:
                vedio_img = QImage(, [1], [0], QImage.Format_Indexed8)
            else:
                vedio_img = QImage(, [1], [0], QImage.Format_RGB888)
            self.vedio_label.setPixmap(QPixmap(vedio_img))
            self.vedio_label.setScaledContents(True)  # Adaptive windows
        else:
            ()
            self.timer_camera.stop()

    # Interface close event that asks the user whether to close the
    def closeEvent(self, event):
        reply = (self, 'Exit', "Do you want to exit this interface?",
                                      | , )
        if reply == :
            ()
            ()
        else:
            ()

if __name__ == "__main__":
    app = QApplication()
    window = VedioGui()
    ()
    (app.exec_())

Video playback is successfully displayed:

Note: Video playback without sound

To this point this article on python in Pyqt5 using Qlabel to achieve the label for video playback of the article is introduced to this, more related to Qlabel to achieve the video playback content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!