PyQt5+Designer写一个播放器

发布于:2023-01-04 ⋅ 阅读:(325) ⋅ 点赞:(0)

本人是第一次发文章,不喜勿喷

首先先用designer写一个界面:

 装饰进度条RCC语法:

QSlider::groove:horizontal{

height: 10px;

left: 5px;

right: 5px;

background: #dcdcdc;

}

QSlider::handle:horizontal{

border-radius: 10px;

width: 20px;

margin: -5px -1px;

background: green;

}

QSlider::sub-page:horizontal{

background: qlineargradient(

spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(27, 5, 27, 255),

stop:0.25 rgba(99, 20, 102, 255),

stop:0.5 rgba(154, 30, 158, 255),

stop:1 rgba(173, 57, 176, 255)

);

}

(RCC语法也可以根据自己的喜好来写)

然后把中间的widget提升一下部件:

 最后全写完了把他保存为gui.ui

(唠叨一句)ui转py的方式是python -m PyQt5.uic.pyuic xxx.ui -o xxx.py

 然后再创建一个随便一个名字.py

from sys import *
from tkinter.messagebox import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from gui import Ui_Dialog
from PyQt5.QtGui import QIcon
from PyQt5.QtMultimedia import *
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import *
class video_Error(SystemError):
    pass

class main(QMainWindow):

    def __init__(self, parent=None):
        super(QMainWindow, self).__init__(parent)
        self.loadgui = Ui_Dialog()
        self.loadgui.setupUi(self)
        QApplication.processEvents()
        self.play = QMediaPlayer()
        self.play.durationChanged.connect(self.getDuration)
        self.play.positionChanged.connect(self.getPosition)
        self.play.setVideoOutput(self.loadgui.video)
    def up(self):
        pass
    def open_file(self):
        try:
            l = QFileDialog.getOpenFileUrl()[0]
            self.mv = l
            self.play.setMedia(QMediaContent(l))
            self.loadgui.bf.setStyleSheet("image: url(l.png);\nfont: 25pt \"Arial\";")
            self.play.play()

        except Exception as error:
            showerror("error", error)

    def bf(self):
        if self.play.state() == 1:
            self.play.pause()
            self.loadgui.bf.setStyleSheet("image: url(l.png);\nfont: 25pt \"Arial\";")
        else:
            self.loadgui.bf.setStyleSheet("image: url(RC.png);\nfont: 25pt \"Arial\";")
            self.play.play()

    
    def getDuration(self, d):
        self.loadgui.vedio1.setRange(0, d)
        self.loadgui.vedio1.setEnabled(True)
        # 视频实时位置获取

    def getPosition(self, p):
        self.loadgui.vedio1.setValue(p)

    def updata(self, v):
        self.play.setPosition(v)


if __name__ == "__main__":
    app = QApplication(argv)
    app.setWindowIcon(QIcon('ico.png'))
    QApplication.processEvents()
    al = main()
    al.show()
    exit(app.exec_())

最后我在给菜菜们附上gui.py的代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'gui.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_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(823, 563)
        Dialog.setMaximumSize(QtCore.QSize(824, 563))
        Dialog.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.193, y1:0.0795455, x2:1, y2:1, stop:0.130682 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));")
        self.frame_2 = QtWidgets.QFrame(Dialog)
        self.frame_2.setGeometry(QtCore.QRect(0, 0, 821, 491))
        self.frame_2.setStyleSheet("background-color: qlineargradient(spread:reflect, x1:0.289773, y1:0.409, x2:1, y2:1, stop:0.130682 rgba(35, 30, 50, 255), stop:1 rgba(255, 255, 255, 255));")
        self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_2.setObjectName("frame_2")
        self.open_file = QtWidgets.QPushButton(self.frame_2)
        self.open_file.setGeometry(QtCore.QRect(0, 0, 92, 51))
        self.open_file.setStyleSheet("font: 25pt \"Arial\";\n"
"image: url(lo.png)\n"
"")
        self.open_file.setText("")
        self.open_file.setObjectName("open_file")
        self.widget_3 = QVideoWidget(self.frame_2)
        self.widget_3.setGeometry(QtCore.QRect(0, 50, 811, 431))
        self.widget_3.setObjectName("widget_3")
        self.video = QVideoWidget(self.widget_3)
        self.video.setGeometry(QtCore.QRect(0, 0, 821, 431))
        self.video.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.352273, y1:0.256, x2:1, y2:1, stop:0.278409 rgba(15, 105, 164, 255), stop:1 rgba(255, 255, 255, 255));")
        self.video.setObjectName("video")
        self.frame = QtWidgets.QFrame(Dialog)
        self.frame.setGeometry(QtCore.QRect(0, 490, 821, 71))
        self.frame.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0.307, y1:0.329545, x2:1, y2:1, stop:0 rgba(17, 21, 17, 255), stop:1 rgba(255, 255, 255, 255));")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.horizontalLayoutWidget = QtWidgets.QWidget(self.frame)
        self.horizontalLayoutWidget.setGeometry(QtCore.QRect(20, 10, 801, 48))
        self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.horizontalLayoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.bf = QtWidgets.QPushButton(self.horizontalLayoutWidget)
        self.bf.setStyleSheet("image: url(l.png);\n"
"\n"
"font: 25pt \"Arial\";")
        self.bf.setText("")
        self.bf.setObjectName("bf")
        self.horizontalLayout.addWidget(self.bf)
        self.vedio1 = QtWidgets.QSlider(self.horizontalLayoutWidget)
        self.vedio1.setStyleSheet("\n"
"\n"
"QSlider::groove:horizontal{ \n"
"                                height: 10px; \n"
"                                left: 5px; \n"
"                                right: 5px; \n"
"                                background: #dcdcdc; \n"
"                              } \n"
"    QSlider::handle:horizontal{ \n"
"                            border-radius: 10px; \n"
"                            width:  20px; \n"
"                            margin: -5px -1px; \n"
"                            background: green; \n"
"                            } \n"
"    QSlider::sub-page:horizontal{\n"
"                                background: qlineargradient(\n"
"                                                            spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 rgba(27, 5, 27, 255), \n"
"                                                            stop:0.25 rgba(99, 20, 102, 255), \n"
"                                                            stop:0.5 rgba(154, 30, 158, 255), \n"
"                                                            stop:1 rgba(173, 57, 176, 255)\n"
"                                                            );\n"
"                                }\n"
"")
        self.vedio1.setOrientation(QtCore.Qt.Horizontal)
        self.vedio1.setObjectName("vedio1")
        self.horizontalLayout.addWidget(self.vedio1)
        self.label = QtWidgets.QLabel(self.horizontalLayoutWidget)
        self.label.setText("")
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)

        self.retranslateUi(Dialog)
        self.open_file.clicked.connect(Dialog.open_file)
        self.bf.clicked.connect(Dialog.bf)
        self.vedio1.sliderMoved.connect(Dialog.updata)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
from PyQt5.QtMultimediaWidgets import QVideoWidget

效果如下:

 

听说家里有孩子的人才可对出下一句:

come on得喂

爱你孤身走暗巷

好了这就是本期内容,如有意见评论区见,白白

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到