Python QtMediaplayer无法在无框半透明背景PyQt5上工作

Python QtMediaplayer无法在无框半透明背景PyQt5上工作,python,python-3.x,qt,pyqt5,qt5,Python,Python 3.x,Qt,Pyqt5,Qt5,我正在用QMediaplayer制作一个视频播放器,但它不能在无框半透明背景窗口上工作。我想制作一个圆角窗口,所以我需要无框半透明窗口。 这是我的密码: from PyQt5.QtCore import Qt, QUrl from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer from PyQt5.QtMultimediaWidgets import QVideoWidget from PyQt5.QtWidgets import Q

我正在用QMediaplayer制作一个视频播放器,但它不能在无框半透明背景窗口上工作。我想制作一个圆角窗口,所以我需要无框半透明窗口。 这是我的密码:

from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import QApplication,QMainWindow,QFrame
import sys     
class Player(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("PyQt Video Player Widget Example") 
        self.resize(600,400)
        self.frame=QFrame(self)
        self.frame.setStyleSheet('background:grey;border-radius:20px;')
        self.setCentralWidget(self.frame)
        #self.setWindowFlag(Qt.FramelessWindowHint)
        #self.setAttribute(Qt.WA_TranslucentBackground)
        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        videoWidget = QVideoWidget(self.frame)
        videoWidget.setGeometry(10,10,580,380)
        self.resize(600,400)
        self.mediaPlayer.error.connect(self.handleError)
        self.mediaPlayer.setVideoOutput(videoWidget)
        self.mediaPlayer.setMedia(
                         QMediaContent(QUrl.fromLocalFile("C:/Users/mishra/Desktop/HiddenfilesWindow/10000000_1874628825927192_6229658593205944320_n(1).mp4")))
        self.mediaPlayer.play()
    def handleError(self):
        print("Error: " + self.mediaPlayer.errorString())
          
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    window = Player()
    
    window.show()
    sys.exit(app.exec_())
设置半透明背景后,它只播放音频而不播放视频。有人知道如何修复它吗?

试试:

import sys
from PyQt5.QtCore import Qt, QUrl, QRectF
from PyQt5.QtGui import QPainterPath, QRegion
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import QApplication, QMainWindow, QFrame, QWidget, QHBoxLayout
     

class Player(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("PyQt Video Player Widget Example") 
        self.resize(600,400)
        self.frame= QFrame(self)  
        
#        self.frame.setStyleSheet('background:grey; border-radius: 20px;')
        self.setStyleSheet("Player {background: #000;}")                   # +++
        
        self.setCentralWidget(self.frame)
#        self.setWindowFlag(Qt.FramelessWindowHint)
#        self.setAttribute(Qt.WA_TranslucentBackground)

        layout = QHBoxLayout(self.frame)                                   # +++
        videoWidget = QVideoWidget()                                       # +++
        layout.addWidget(videoWidget)                                      # +++

        
        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
#        videoWidget = QVideoWidget(self.frame)             
#        videoWidget.setGeometry(10,10,580,380)
#        self.resize(600,400)
        self.mediaPlayer.error.connect(self.handleError)
        self.mediaPlayer.setVideoOutput(videoWidget)
        self.mediaPlayer.setMedia(
            QMediaContent(QUrl.fromLocalFile("Samonastrojka.avi")))
        self.mediaPlayer.play()

# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv        
    def resizeEvent(self, event):
        path = QPainterPath()
        path.addRoundedRect(QRectF(self.rect()), 20, 20)
        reg = QRegion(path.toFillPolygon().toPolygon())
        self.setMask(reg)
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        
    def handleError(self):
        print("Error: " + self.mediaPlayer.errorString())
   
   
if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = Player()
    window.show()
    sys.exit(app.exec_())

我不会在PyQt5 5.9.2Did u uncomment上重现问题,因为取消注释意味着删除我提到的注释行。但无论如何,我在5.15@alecYes中遇到了这个问题。我取消注释了这些行,我现在能做什么?这个问题很有趣。我无法核实你的答案,但我知道我可以相信你的测试。尽管如此,您的答案仍然没有提供足够的上下文(再次)。它能解决问题吗?对它是否提供实际的教学帮助(这是SO最重要的原则之一,因为应该始终提供上下文/论证)?绝对不是。别误会我的意思:我感谢你的努力(我能认出你的“箭头答案”截图,我很喜欢),但我还是要说,你的答案不是好答案,只要你写下你的“试试看”.是的,它工作得很好。这意味着半透明背景有问题,或者是一个Bug?你能像我上一个一样使角落平滑吗?它的边缘不太平滑。谢谢你的努力@尼克