PyQt5小部件旨在透明地显示QVideoPlayer通过视频并采用QMainWindow背景

2024-06-28 20:33:07 发布

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

我有一个QMainWindow,其中一个QVideoWidget被设置为中心小部件。这段视频就像我正在制作的21点游戏主窗口的背景。我有两个按钮,我想放在视频顶部的部分透明度

这就是我希望窗口的外观:

enter image description here

但我尝试了几十种方法来实现透明的按钮背景,但都做不到。任何透明的尝试都会直接“剪切”视频并显示主窗口的背景。例如,我将主窗口背景设置为蓝色

这是运行时显示的内容:

enter image description here

如何使视频显示在透明按钮后面

我尝试过但没有成功:堆叠布局设置为StackAll,在按钮样式表中插入透明背景图像,使用透明颜色代码(rgba(0,0,0,0)),将videowidget设置为按钮的父窗口小部件

我的代码的相关部分:

from PyQt5 import QtWidgets, QtMultimediaWidgets, QtMultimedia, QtCore, QtGui, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QLineEdit, QComboBox
from PyQt5.QtGui import QTransform
import sys
import random


class MyWindow(QMainWindow):


    def __init__(self):
        super(MyWindow, self).__init__()
        self.setGeometry(0, 0, 1920, 1080)
        self.setWindowTitle("Cutie Pie Casino")
        self.setStyleSheet('background-color: blue;')
        self.Welcome()


    def Welcome(self):

        welcome_font = QtGui.QFont()
        welcome_font.setPointSize(30)
        welcome_font.setFamily('Broadway')

        welcome_font_small = QtGui.QFont()
        welcome_font_small.setPointSize(20)
        welcome_font_small.setFamily('Broadway')

        # create link to movie file
        movie_file = QtCore.QUrl.fromLocalFile('C:/Users/Owner/PycharmProjects/Black Jack/video/'
                                               'Cutie Pie Casino Video.mp4')
        vid_media = QtMultimedia.QMediaContent(movie_file)

        # create video widget
        self.videoWidget = QtMultimediaWidgets.QVideoWidget()
        self.videoWidget.setGeometry(0,0,1920,1080)
        self.videoWidget.setStyleSheet('background-color: blue')


        # create media player object   (video widget goes in media player)
        self.mediaPlayer = QtMultimedia.QMediaPlayer(None,
                                                     QtMultimedia.QMediaPlayer.VideoSurface)
        self.mediaPlayer.setVideoOutput(self.videoWidget)
        # playlist
        self.playlist = QtMultimedia.QMediaPlaylist()
        self.playlist.setCurrentIndex(0)
        self.playlist.setPlaybackMode(QtMultimedia.QMediaPlaylist.Loop)
        self.playlist.addMedia(vid_media)
        # add content to media player
        self.mediaPlayer.setPlaylist(self.playlist)
        self.mediaPlayer.play()
        self.setCentralWidget(self.videoWidget)


        self.PlayCardsButton = QPushButton(self)
        self.PlayCardsButton.setText('Play Blackjack')
        self.PlayCardsButton.setFont(welcome_font_small)
        self.PlayCardsButton.setGeometry(765,500,400,150)
        self.PlayCardsButton.clicked.connect(self.InitializeTable)
        self.PlayCardsButton.setStyleSheet('background-color: rgba(0,0,0,0); color: black')


        self.GameSetupButton = QPushButton(self)
        self.GameSetupButton.setGeometry(765, 700, 400, 150)
        self.GameSetupButton.setFont(welcome_font_small)
        self.GameSetupButton.setText('Game Setup')
        self.GameSetupButton.setStyleSheet('background-color: rgba(0,0,0,0); color: black')

Tags: importself视频按钮mediaplaylistcolorsmall