先上一张代码实现的效果图,代码实现了视频播放功能、停止、暂停、前进、后退以及声音的调大调小。视频的图像显示我使用的是QLabel控件,我对他进行了二次封装,以实现图片的自适用缩放,图片的切换等。另外初始图显示我的是我的家乡夜景图,号称“北方水城”的一座历史文化城市,欢迎大家来我的家乡参观游玩。
1、关于pyside6的桌面布局,大家可以从网上搜一下相关内容。介绍一下我的经验,如有不对的地方请大家指正。QtDesigner为我们提供了方便的页面设计功能,类似以前的VB。如下图:
我的主要面继承自QMainWindow类,所以他包含了三部分,自上而下:menubar(菜单)、centralwidget(主功能区)、statusbar(状态区)。
在设计桌面模板前,先有一个大致的规划,这样更方便之后的布局设置。PyQt5中常用的主窗体有两种QMainWindow和QDialog,他们也都继承自QWidget,所以这三个类都可以用来创建主窗口,可以直接使用,也可以继承后再使用。
我这里就是继承了QMainWindow类。下面的代码时通过Compile Form工具将设计的模板文件自动生成的代码,如下:
注意:from qframelesswindow import QMainWindow ,我这里使用了qframelesswindow 中的类
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 'video_MainWindow.ui'
##
## Created by: Qt User Interface Compiler version 6.7.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QAction, QBrush, QColor, QConicalGradient,
QCursor, QFont, QFontDatabase, QGradient,
QIcon, QImage, QKeySequence, QLinearGradient,
QPainter, QPalette, QPixmap, QRadialGradient,
QTransform)
from PySide6.QtWidgets import (QApplication, QFrame, QHBoxLayout, QLayout,
QListView, QMainWindow, QMenu, QMenuBar,
QSizePolicy, QStatusBar, QVBoxLayout, QWidget)
from qfluentwidgets import (BodyLabel, PushButton, RoundMenu, Slider)
from qframelesswindow import QMainWindow
import com.sunway.video.ui.image_rc
class Ui_VideoMainWindow(QMainWindow):
def setupUi(self, VideoMainWindow):
if not VideoMainWindow.objectName():
VideoMainWindow.setObjectName(u"VideoMainWindow")
VideoMainWindow.resize(1380, 698)
icon = QIcon()
icon.addFile(u":/icons/raspberry.jpg", QSize(), QIcon.Normal, QIcon.Off)
VideoMainWindow.setWindowIcon(icon)
self.openDir = QAction(VideoMainWindow)
self.openDir.setObjectName(u"openDir")
self.openFile = QAction(VideoMainWindow)
self.openFile.setObjectName(u"openFile")
self.actionquite = QAction(VideoMainWindow)
self.actionquite.setObjectName(u"actionquite")
self.centralwidget = QWidget(VideoMainWindow)
self.centralwidget.setObjectName(u"centralwidget")
sizePolicy = QSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.centralwidget.sizePolicy().hasHeightForWidth())
self.centralwidget.setSizePolicy(sizePolicy)
self.centralwidget.setStyleSheet(u"")
self.horizontalLayout_3 = QHBoxLayout(self.centralwidget)
self.horizontalLayout_3.setObjectName(u"horizontalLayout_3")
self.horizontalLayout_2 = QHBoxLayout()
self.horizontalLayout_2.setObjectName(u"horizontalLayout_2")
self.play_list = QListView(self.centralwidget)
self.play_list.setObjectName(u"play_list")
self.horizontalLayout_2.addWidget(self.play_list)
self.verticalLayout_2 = QVBoxLayout()
self.verticalLayout_2.setObjectName(u"verticalLayout_2")
self.playerFrame = QFrame(self.centralwidget)
self.playerFrame.setObjectName(u"playerFrame")
sizePolicy.setHeightForWidth(self.playerFrame.sizePolicy().hasHeightForWidth())
self.playerFrame.setSizePolicy(sizePolicy)
self.playerFrame.setStyleSheet(u"background-color: rgb(85, 170, 255);\n"
"background-image: url(:/backimg/backimg.jpg);")
self.horizontalLayout_4 = QHBoxLayout(self.playerFrame)
self.horizontalLayout_4.setObjectName(u"horizontalLayout_4")
self.verticalLayout_2.addWidget(self.playerFrame)
self.horizontalLayout = QHBoxLayout()
self.horizontalLayout.setObjectName(u"horizontalLayout")
self.frame_3 = QFrame(self.centralwidget)
self.frame_3.setObjectName(u"frame_3")
self.frame_3.setStyleSheet(u"background-color: rgb(255, 85, 127);")
self.frame_3.setFrameShape(QFrame.Shape.StyledPanel)
self.frame_3.setFrameShadow(QFrame.Shadow.Raised)
self.verticalLayout = QVBoxLayout(self.frame_3)
self.verticalLayout.setObjectName(u"verticalLayout")
self.verticalLayout.setSizeConstraint(QLayout.SizeConstraint.SetNoConstraint)
self.frame = QFrame(self.frame_3)
self.frame.setObjectName(u"frame")
self.frame.setFrameShape(QFrame.Shape.StyledPanel)
self.frame.setFrameShadow(QFrame.Shadow.Raised)
self.horizontalLayout_5 = QHBoxLayout(self.frame)
self.horizontalLayout_5.setObjectName(u"horizontalLayout_5")
self.viderPalyerSlider = Slider(self.frame)
self.viderPalyerSlider.setObjectName(u"viderPalyerSlider")
self.viderPalyerSlider.setOrientation(Qt.Orientation.Horizontal)
self.horizontalLayout_5.addWidget(self.viderPalyerSlider)
self.videoTimelabel = BodyLabel(self.frame)
self.videoTimelabel.setObjectName(u"videoTimelabel")
self.videoTimelabel.setStyleSheet(u"background-color: rgb(255, 255, 127);")
self.horizontalLayout_5.addWidget(self.videoTimelabel)
self.horizontalLayout_5.setStretch(0, 14)
self.horizontalLayout_5.setStretch(1, 1)
self.verticalLayout.addWidget(self.frame)
self.frame_2 = QFrame(self.frame_3)
self.frame_2.setObjectName(u"frame_2")
self.frame_2.setStyleSheet(u"background-color: rgb(85, 170, 255);")
self.frame_2.setFrameShape(QFrame.Shape.StyledPanel)
self.frame_2.setFrameShadow(QFrame.Shadow.Raised)
self.horizontalLayout_6 = QHBoxLayout(self.frame_2)
self.horizontalLayout_6.setObjectName(u"horizontalLayout_6")
self.playBtn = PushButton(self.frame_2)
self.playBtn.setObjectName(u"playBtn")
icon1 = QIcon()
icon1.addFile(u":/icons/banana.jpg", QSize(), QIcon.Normal, QIcon.Off)
self.playBtn.setIcon(icon1)
self.horizontalLayout_6.addWidget(self.playBtn)
self.stopBtn = PushButton(self.frame_2)
self.stopBtn.setObjectName(u"stopBtn")
self.horizontalLayout_6.addWidget(self.stopBtn)
self.pauseBtn = PushButton(self.frame_2)
self.pauseBtn.setObjectName(u"pauseBtn")
self.horizontalLayout_6.addWidget(self.pauseBtn)
self.muteBtn = PushButton(self.frame_2)
self.muteBtn.setObjectName(u"muteBtn")
self.horizontalLayout_6.addWidget(self.muteBtn)
self.vol_pBtn = PushButton(self.frame_2)
self.vol_pBtn.setObjectName(u"vol_pBtn")
self.horizontalLayout_6.addWidget(self.vol_pBtn)
self.vol_bBtn = PushButton(self.frame_2)
self.vol_bBtn.setObjectName(u"vol_bBtn")
self.horizontalLayout_6.addWidget(self.vol_bBtn)
self.seek_bBtn = PushButton(self.frame_2)
self.seek_bBtn.setObjectName(u"seek_bBtn")
self.horizontalLayout_6.addWidget(self.seek_bBtn)
self.seek_pBtn = PushButton(self.frame_2)
self.seek_pBtn.setObjectName(u"seek_pBtn")
self.horizontalLayout_6.addWidget(self.seek_pBtn)
self.verticalLayout.addWidget(self.frame_2)
self.verticalLayout.setStretch(0, 4)
self.verticalLayout.setStretch(1, 6)
self.horizontalLayout.addWidget(self.frame_3)
self.verticalLayout_2.addLayout(self.horizontalLayout)
self.verticalLayout_2.setStretch(0, 8)
self.verticalLayout_2.setStretch(1, 2)
self.horizontalLayout_2.addLayout(self.verticalLayout_2)
self.horizontalLayout_2.setStretch(1, 8)
self.horizontalLayout_3.addLayout(self.horizontalLayout_2)
VideoMainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(VideoMainWindow)
self.menubar.setObjectName(u"menubar")
self.menubar.setGeometry(QRect(0, 0, 1380, 21))
self.FileMenus = RoundMenu(self.menubar)
self.FileMenus.setObjectName(u"FileMenus")
self.editMenus = QMenu(self.menubar)
self.editMenus.setObjectName(u"editMenus")
VideoMainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(VideoMainWindow)
self.statusbar.setObjectName(u"statusbar")
VideoMainWindow.setStatusBar(self.statusbar)
self.menubar.addAction(self.FileMenus.menuAction())
self.menubar.addAction(self.editMenus.menuAction())
self.FileMenus.addAction(self.openDir)
self.FileMenus.addAction(self.openFile)
self.FileMenus.addAction(self.actionquite)
self.retranslateUi(VideoMainWindow)
QMetaObject.connectSlotsByName(VideoMainWindow)
# setupUi
def retranslateUi(self, VideoMainWindow):
VideoMainWindow.setWindowTitle(QCoreApplication.translate("VideoMainWindow", u"\u89c6\u9891\u64ad\u653e\u5668", None))
self.openDir.setText(QCoreApplication.translate("VideoMainWindow", u"\u6253\u5f00\u76ee\u5f55", None))
self.openFile.setText(QCoreApplication.translate("VideoMainWindow", u"\u6253\u5f00\u6587\u4ef6", None))
self.actionquite.setText(QCoreApplication.translate("VideoMainWindow", u"\u9000\u51fa", None))
self.videoTimelabel.setText("")
self.playBtn.setText(QCoreApplication.translate("VideoMainWindow", u"\u64ad\u653e", None))
self.stopBtn.setText(QCoreApplication.translate("VideoMainWindow", u"\u505c\u6b62", None))
self.pauseBtn.setText(QCoreApplication.translate("VideoMainWindow", u"\u6682\u505c", None))
self.muteBtn.setText(QCoreApplication.translate("VideoMainWindow", u"\u9759\u97f3", None))
self.vol_pBtn.setText(QCoreApplication.translate("VideoMainWindow", u"\u8c03\u5927\u58f0\u97f3", None))
self.vol_bBtn.setText(QCoreApplication.translate("VideoMainWindow", u"\u8c03\u5c0f\u58f0\u97f3", None))
self.seek_bBtn.setText(QCoreApplication.translate("VideoMainWindow", u"\u540e\u9000", None))
self.seek_pBtn.setText(QCoreApplication.translate("VideoMainWindow", u"\u524d\u8fdb", None))
self.FileMenus.setTitle(QCoreApplication.translate("VideoMainWindow", u"\u6587\u4ef6", None))
self.editMenus.setTitle(QCoreApplication.translate("VideoMainWindow", u"\u7f16\u8f91", None))
# retranslateUi