视频小部件调整其大小

2024-09-30 16:27:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我对使用PyQt5还比较陌生,一直在用它来创建我自己的媒体播放器,我遇到的问题是,当我播放或暂停时,视频小部件会自动调整大小,如何将主窗口设置为与正在播放的视频相同的大小,以及如何阻止它调整大小

import sys
from PyQt5 import QtWidgets,QtGui
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import QDir, Qt, QUrl

class Window(QtWidgets.QMainWindow):

def __init__(self):
    super().__init__()
    self.k=0
    self.init_ui()

def init_ui(self):
    self.play=QtWidgets.QPushButton(self)
    self.stop=QtWidgets.QPushButton(self)
    self.play.setText('Play')
    self.stop.setText('Stop')
    self.play.move(0,700)
    self.stop.move(80,700)
    self.setWindowTitle('SyncMedia')
    menu = self.menuBar()
    file = menu.addMenu(' &File ')
    open =QtWidgets.QAction(QtGui.QIcon('open.png'), '&Open', self)
    file.addAction(open)
    #file.addAction(close)
    self.play.clicked.connect(self.play_click)
    self.mediaPlayer =QMediaPlayer(None, QMediaPlayer.VideoSurface)
    self.videoWidget = QVideoWidget()
    wid =QtWidgets.QWidget(self)
    self.setCentralWidget(wid)
    controlLayout=QtWidgets.QHBoxLayout()
    controlLayout.addWidget(self.play)
    controlLayout.addWidget(self.stop)
    layout =QtWidgets.QVBoxLayout()
    layout.addWidget(self.videoWidget)
    layout.addLayout(controlLayout)
    wid.setLayout(layout)
    self.videoWidget.move(500,500)
    self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile("(videofilepath")))
    self.mediaPlayer.setVideoOutput(self.videoWidget)
    self.videoWidget.show()
    self.mediaPlayer.play()
    self.show()
def play_click(self):
    if(self.k==1):
        self.mediaPlayer.play()
        self.play.setText('Pause')
        self.k=0
    elif(self.k==0):
        self.mediaPlayer.pause()
        self.play.setText('Play')
        self.k=1

app=QtWidgets.QApplication(sys.argv)
window=Window()
sys.exit(app.exec_())

Tags: fromimportselfplayinitsyspyqt5stop