网络编程
位置:首页>> 网络编程>> Python编程>> python中Pyqt5使用Qlabel标签进行视频播放

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

作者:??牧子川????  发布时间:2021-12-19 13:32:20 

标签:python,Pyqt5,Qlabel,视频,播放

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模块:

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标签进行视频播放

注:视频播放没有声音

来源:https://juejin.cn/post/7083511739827879943

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com