python中Pyqt5怎么使用Qlabel标签进行视频播放

发布时间:2022-04-22 10:03:13 作者:zzz
来源:亿速云 阅读:839

本文小编为大家详细介绍“python中Pyqt5怎么使用Qlabel标签进行视频播放”,内容详细,步骤清晰,细节处理妥当,希望这篇“python中Pyqt5怎么使用Qlabel标签进行视频播放”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

一、简介

QLabel是界面中的标签类,继承自QFrame类,提供文本和图像的显示,是一种展示控件。

QLabel对象可以显示不可编辑的文本或图片,可以放置一个GIF动画,还可以被用作提示标记为其他控件。

纯文本、链接或富文本也可以显示在标签上。

二、基本用法

2.1 QLabel控件   

setAlignment():按固定值方式对齐文本,有以下对齐方式:

Qt.AlignLeft(水平方向靠左对齐) 、Qt.AlignRight(水平方向靠右对齐)、Qt.AlignCenter(水平方向居中对齐)、Qt.AlignJustify(水平方向调整间距两端对齐)、Qt.AlignTop(垂直方向靠上对齐)、Qt.AlignBottom(垂直方向靠下对齐)、Qt.AlignVCenter(垂直方向居中对齐)

2.2 QLabel常用的信号(事件)

1.linkHovered:当鼠标指针滑过标签中嵌入的超链接时,需要用槽函数与这个信号进行绑定

2.linkActivated:当单击标签中嵌入的超链接,希望在新窗口中打开这个超链接时,setOpenExternalLinks特性必须设置为true

三、QLabel播放视频

使用QLabel播放视频文件的重点就在****定时器QTimer

当程序中需要显示时间时或者需要在程序中周期性地进行某项操作,就会用到定时器

 3.1 QTimer

导入QTimer模块:

from PyQt5.QtCore import QTimer

初始化:

self.timer_camera = QTimer()

计时并启动:

self.timer_camera.start(1000)   # 1000ms == 1s
self.timer_camera.timeout.connect(self.openFrame)  # 连接槽函数openFrame

注意:当QTimer的父对象被销毁时,它也会被自动销毁。

3.2 代码 

UI界面:

python中Pyqt5怎么使用Qlabel标签进行视频播放

 python程序:

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.uic import loadUiType
import cv2
import sys
vedio_ui, _ = loadUiType('./UI/vedio.ui')

class VedioGui(QMainWindow, vedio_ui):
    # 定义构造方法
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.timer_camera = QTimer()
        self.handle_buttons()
        self.open_vedio()
    # 所有Button的消息与槽的通信
    def handle_buttons(self):
        self.btn_Start.clicked.connect(self.Btn_Start)
        self.btn_Stop.clicked.connect(self.Btn_Stop)
    def Btn_Start(self):
        # 定时器开启,每隔一段时间,读取一帧
        self.timer_camera.start(100)
        self.timer_camera.timeout.connect(self.OpenFrame)
    def Btn_Stop(self):
        # self.cap.release()
        self.timer_camera.stop()
    def open_vedio(self):
        """选取视频文件"""
        # 这里以mp4和avi视频播放为例
        openfile_name = QFileDialog.getOpenFileName(self, 'chose files', '', 'Image files(*.mp4 *.avi)')  # 打开文件选择框选择文件
        self.file_name = openfile_name[0]  # 获取图片名称

        # 得到文件后缀名  需要根据情况进行修改
        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.cap = cv2.VideoCapture(self.file_name)
    def OpenFrame(self):
        ret, image = self.cap.read()
        if ret:
            if len(image.shape) == 3:
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                vedio_img = QImage(image.data, image.shape[1], image.shape[0], QImage.Format_RGB888)
            elif len(image.shape) == 1:
                vedio_img = QImage(image.data, image.shape[1], image.shape[0], QImage.Format_Indexed8)
            else:
                vedio_img = QImage(image.data, image.shape[1], image.shape[0], QImage.Format_RGB888)
            self.vedio_label.setPixmap(QPixmap(vedio_img))
            self.vedio_label.setScaledContents(True)  # 自适应窗口
        else:
            self.cap.release()
            self.timer_camera.stop()

    # 界面关闭事件,询问用户是否关闭
    def closeEvent(self, event):
        reply = QMessageBox.question(self, '退出', "是否要退出该界面?",
                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            self.close()
            event.accept()
        else:
            event.ignore()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = VedioGui()
    window.show()
    sys.exit(app.exec_())

视频播放成功显示: 

python中Pyqt5怎么使用Qlabel标签进行视频播放

注:视频播放没有声音

读到这里,这篇“python中Pyqt5怎么使用Qlabel标签进行视频播放”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Qt中QLabel怎么用
  2. python中pyqt5怎么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python pyqt5 qlabel

上一篇:react-hot-loader怎么用

下一篇:Python基于DFA算法怎么实现内容敏感词过滤

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》