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:當鼠標指針滑過標簽中嵌入的超鏈接時,需要用槽函數(shù)與這個信號進行綁定
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) # 連接槽函數(shù)openFrame
注意:當QTimer的父對象被銷毀時,它也會被自動銷毀。
3.2 代碼
UI界面:
?
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] # 獲取圖片名稱
# 得到文件后綴名 需要根據(jù)情況進行修改
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實現(xiàn)標簽進行視頻播放的文章就介紹到這了,更多相關Qlabel實現(xiàn)視頻播放內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python字符串中出現(xiàn)的次數(shù)統(tǒng)計多種方法
這篇文章主要介紹了Python字符串中出現(xiàn)的次數(shù)統(tǒng)計多種方法,使用內置的count()方法、正則表達式、列表推導式、循環(huán)和條件判斷以及字符串分割,每種方法都有其適用的場景和優(yōu)缺點,選擇合適的方法取決于具體的需求和場景,需要的朋友可以參考下2024-12-12
pyqt QGraphicsView 以鼠標為中心進行縮放功能實現(xiàn)
在PyQt開發(fā)中,實現(xiàn)QGraphicsView的鼠標中心縮放功能需要注意初始化以及關鍵函數(shù)的重定義,遇到不達預期的效果時,可能需要重寫所有鼠標事件,本文記錄了解決QGraphicsView鼠標縮放問題的過程,供開發(fā)者參考2024-10-10
python3 實現(xiàn)的對象與json相互轉換操作示例
這篇文章主要介紹了python3 實現(xiàn)的對象與json相互轉換操作,結合實例形式分析了Python3使用json模塊針對json格式數(shù)據(jù)轉換操作的相關實現(xiàn)技巧,需要的朋友可以參考下2019-08-08

