适合新手看看。
我只用了光电那边的播放界面。凑合理解一下。
利用Qtdesigner创建一个UI界面。
上面的QWidget需要提升为:
提升的类名称为QVideoWidget,头文件为PyQt5.QtMultimediaWidgets
然后 .ui 转为 .py 文件 ,这一步百度都有。
我保存的文件名是 rador.py。
代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'rador.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1066, 765)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.splitter = QtWidgets.QSplitter(self.centralwidget)
self.splitter.setGeometry(QtCore.QRect(200, 40, 891, 20))
self.splitter.setOrientation(QtCore.Qt.Horizontal)
self.splitter.setObjectName("splitter")
self.lab_rador = QtWidgets.QLabel(self.splitter)
self.lab_rador.setObjectName("lab_rador")
self.lab_camera = QtWidgets.QLabel(self.splitter)
self.lab_camera.setObjectName("lab_camera")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(10, 60, 1031, 501))
self.layoutWidget.setObjectName("layoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.wdt_rador = QVideoWidget(self.layoutWidget)
self.wdt_rador.setAutoFillBackground(True)
self.wdt_rador.setObjectName("wdt_rador")
self.gridLayout.addWidget(self.wdt_rador, 0, 0, 1, 1)
self.wdt_camera = QVideoWidget(self.layoutWidget)
self.wdt_camera.setAutoFillBackground(True)
self.wdt_camera.setStyleSheet("color:rgb(0, 0, 0)")
self.wdt_camera.setObjectName("wdt_camera")
self.gridLayout.addWidget(self.wdt_camera, 0, 1, 1, 1)
self.splitter_3 = QtWidgets.QSplitter(self.centralwidget)
self.splitter_3.setGeometry(QtCore.QRect(30, 670, 631, 23))
self.splitter_3.setOrientation(QtCore.Qt.Horizontal)
self.splitter_3.setObjectName("splitter_3")
self.splitter_2 = QtWidgets.QSplitter(self.splitter_3)
self.splitter_2.setOrientation(QtCore.Qt.Horizontal)
self.splitter_2.setObjectName("splitter_2")
self.btn_open = QtWidgets.QPushButton(self.splitter_2)
self.btn_open.setObjectName("btn_open")
self.btn_stop = QtWidgets.QPushButton(self.splitter_2)
self.btn_stop.setObjectName("btn_stop")
self.btn_play = QtWidgets.QPushButton(self.splitter_2)
self.btn_play.setObjectName("btn_play")
self.btn_quit = QtWidgets.QPushButton(self.splitter_3)
self.btn_quit.setObjectName("btn_quit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1066, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.btn_quit.clicked.connect(MainWindow.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.lab_rador.setText(_translate("MainWindow", "雷达"))
self.lab_camera.setText(_translate("MainWindow", "光电"))
self.btn_open.setText(_translate("MainWindow", "打开"))
self.btn_stop.setText(_translate("MainWindow", "暂停"))
self.btn_play.setText(_translate("MainWindow", "播放"))
self.btn_quit.setText(_translate("MainWindow", "退出"))
from PyQt5.QtMultimediaWidgets import QVideoWidget
然后再新建一个 .py 文件。名字随便。
代码如下:
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtMultimediaWidgets import QVideoWidget
from rador import Ui_MainWindow
import sys
class myMainWindow(Ui_MainWindow,QMainWindow):
def __init__(self):
super(Ui_MainWindow, self).__init__()
self.setupUi(self)
# 播放器
self.player = QMediaPlayer()
self.player.setVideoOutput(self.wdt_camera)
# 按钮打开文件
self.btn_open.clicked.connect(self.openVideoFile)
# 播放
self.btn_play.clicked.connect(self.playVideo) # play
# 暂停
self.btn_stop.clicked.connect(self.pauseVideo) # pause
def openVideoFile(self):
self.player.setMedia(QMediaContent(QFileDialog.getOpenFileUrl()[0]))
self.player.play()
print(self.player.availableMetaData())
def playVideo(self):
self.player.play()
def pauseVideo(self):
self.player.pause()
# if __name__ == '__main__':
# app = QApplication(sys.argv)
# mainWindow = QMainWindow()
# ui = Ui_MainWindow()
# ui.setupUi(mainWindow)
# mainWindow.show()
# sys.exit(app.exec_())
if __name__ == '__main__':
app = QApplication(sys.argv)
vieo_gui = myMainWindow()
vieo_gui.show()
sys.exit(app.exec_())
运行:这个文件。结果如下
有些格式的视频,比如MP4格式的视频不能打开,需要下载转码器。百度有。
avi格式的是可以打开的