脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - python中Pyqt5使用Qlabel标签进行视频播放

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

2022-12-05 10:37牧子川 Python

这篇文章主要介绍了python中Pyqt5使用Qlabel实现标签进行视频播放,QLabel是界面中的标签类,继承自QFrame类,提供文本和图像的显示,是一种展示控件,下文相关内容介绍需要的小伙伴可以参考一下

Pyqt5安装并配置到pycharm方法:教你如何用pycharm安装pyqt5及其相关配置

一、简介

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

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

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

二、基本用法

2.1 QLabel控件   

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

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

  • setIndent():设置文本缩进
  • setPixmap():设置QLabel为一个Pixmap图片
  • text():获得QLabel的文本内容
  • setText():设置QLabel的文本内容   
  • selectedText():返回所选择的字符
  • setBuddy():设置伙伴关系
  • setWordWrap():设置是否允许换行

2.2 QLabel常用的信号(事件)

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

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

三、QLabel播放视频

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

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

 3.1 QTimer

导入QTimer模块:

?
1
from PyQt5.QtCore import QTimer

初始化:

?
1
self.timer_camera = QTimer()

计时并启动:

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

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

3.2 代码 

UI界面:

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

 python程序:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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实现标签进行视频播放的文章就介绍到这了,更多相关Qlabel实现视频播放内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/7083511739827879943

延伸 · 阅读

精彩推荐