pyqt5 QStackedWidget QStackedLayout 阴影 覆盖 透明

本文展示了一个使用PyQt5创建透明窗口的示例代码,包括三个子窗口,每个窗口都有不同的透明背景设置和布局。通过调整窗口属性和样式表,可以实现窗口的透明效果,同时保持窗口内容的清晰可见。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:

import sys
from PyQt5.QtWidgets import QWidget, QToolTip, QApplication
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys


class ChildOneWin(QWidget):
    def __init__(self, parent=None):
        super(ChildOneWin, self).__init__(parent)

        self.main_layout = QVBoxLayout()
        self.top_widget = QWidget()
        self.setLayout(self.main_layout)
        self.top_widget.setObjectName("ChildOneWin_wdt")
        self.top_widget.setStyleSheet("#ChildOneWin_wdt{background:rgb(255,255,255)}")
        self.main_layout.addWidget(self.top_widget)
        self.main_layout.setContentsMargins(0, 0, 0, 0)
        self.main_layout.setSpacing(0)
        top_widget_layout = QVBoxLayout()
        self.top_widget.setLayout(top_widget_layout)

        self.test_btn = QPushButton('窗口1')
        top_widget_layout.addStretch(1)
        top_widget_layout.addWidget(self.test_btn)

class ChildTwoWin(QWidget):
    def __init__(self, parent=None):
        super(ChildTwoWin, self).__init__(parent)

        self.setWindowFlags(Qt.FramelessWindowHint | Qt.BypassWindowManagerHint | Qt.Tool | Qt.WindowStaysOnTopHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.wdt = QWidget()
        #self.wdt.setAttribute(Qt.WA_TranslucentBackground)
        self.wdt.setObjectName("ChildTwoWin_wdt")
        self.wdt.setStyleSheet("#ChildTwoWin_wdt{background:rgba(0,0,0,0.2)}")

        self.layout = QGridLayout(self)
        self.layout.addWidget(self.wdt)
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layout.setSpacing(0)

        main_layout = QVBoxLayout()

        self.wdt.setLayout(main_layout)

        self.test_btn = QPushButton('窗口2')
        main_layout.addStretch(1)
        main_layout.addWidget(self.test_btn)
        main_layout.addStretch(1)




# class ChildTwoWin(QWidget):
#     def __init__(self, parent=None):
#         super(ChildTwoWin, self).__init__(parent)
#
#         self.main_layout = QVBoxLayout()
#
#         self.top_widget = QWidget()
#         self.setLayout(self.main_layout)
#         self.top_widget.setObjectName("ChildTwoWin_wdt")
#         self.top_widget.setStyleSheet("#ChildTwoWin_wdt{background:rgb(255,255,255)}")
#         self.main_layout.addWidget(self.top_widget)
#
#         self.main_layout.setContentsMargins(0, 0, 0, 0)
#         self.main_layout.setSpacing(0)
#
#         top_widget_layout = QVBoxLayout()
#         self.top_widget.setLayout(top_widget_layout)
#
#         self.test_btn = QPushButton('窗口2')
#         top_widget_layout.addStretch(1)
#         top_widget_layout.addWidget(self.test_btn)
#         top_widget_layout.addStretch(1)


class ChildThreeWin(QWidget):
    def __init__(self, parent=None):
        super(ChildThreeWin, self).__init__(parent)

        self.setWindowFlags(Qt.FramelessWindowHint | Qt.BypassWindowManagerHint | Qt.Tool | Qt.WindowStaysOnTopHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.wdt = QWidget()
        #self.wdt.setAttribute(Qt.WA_TranslucentBackground)
        self.wdt.setObjectName("tipWaitingWindow_back")
        self.wdt.setStyleSheet("#tipWaitingWindow_back{background:rgba(0,0,0,0.2)}")

        self.layout = QGridLayout(self)
        self.layout.addWidget(self.wdt)
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layout.setSpacing(0)

        main_layout = QVBoxLayout()

        self.wdt.setLayout(main_layout)

        self.test_btn = QPushButton('窗口3')
        main_layout.addWidget(self.test_btn)
        main_layout.addStretch(1)


class Winform(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('透明窗口测试')
        self.resize(330, 350)

        main_layout = QVBoxLayout()

        self.setLayout(main_layout)

        top_widget = QWidget()
        top_widget_layout = QHBoxLayout()
        top_widget.setLayout(top_widget_layout)

        self.test_btn = QPushButton('测试')
        self.test_btn.clicked.connect(self.call_back_test_btn)

        self.control_btn = QPushButton('控制')
        self.control_btn.clicked.connect(self.call_back_control_btn)

        self.show_btn = QPushButton('显示')
        self.show_btn.clicked.connect(self.call_back_show_btn)

        top_widget_layout.addWidget(self.test_btn)
        top_widget_layout.addStretch(1)
        top_widget_layout.addWidget(self.control_btn)
        top_widget_layout.addStretch(1)
        top_widget_layout.addWidget(self.show_btn)

        bottom_widget = QWidget()
        self.bottom_widget_layout = QStackedLayout()
        self.bottom_widget_layout.setStackingMode(QStackedLayout.StackAll)
        bottom_widget.setLayout(self.bottom_widget_layout)

        main_layout.addWidget(top_widget)
        main_layout.addWidget(bottom_widget)
        main_layout.setStretch(0, 1)
        main_layout.setStretch(1, 5)

        child_one_win = ChildOneWin()
        child_two_win = ChildTwoWin()
        child_three_win = ChildThreeWin()
        self.bottom_widget_layout.addWidget(child_one_win)
        self.bottom_widget_layout.addWidget(child_two_win)
        self.bottom_widget_layout.addWidget(child_three_win)

        self.bottom_widget_layout.setCurrentIndex(0)

    def call_back_show_btn(self):
        self.bottom_widget_layout.setCurrentIndex(2)

    def call_back_test_btn(self):
        self.bottom_widget_layout.setCurrentIndex(0)

    def call_back_control_btn(self):
        pass
        self.bottom_widget_layout.setCurrentIndex(1)



class WinUIform(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle('透明窗口测试')
        self.resize(330, 350)

        main_layout = QVBoxLayout()

        self.setLayout(main_layout)

        top_widget = QWidget()
        top_widget_layout = QHBoxLayout()
        top_widget.setLayout(top_widget_layout)

        self.test_btn = QPushButton('测试')
        self.test_btn.clicked.connect(self.call_back_test_btn)

        self.control_btn = QPushButton('控制')
        self.control_btn.clicked.connect(self.call_back_control_btn)

        self.show_btn = QPushButton('显示')
        self.show_btn.clicked.connect(self.call_back_show_btn)

        top_widget_layout.addWidget(self.test_btn)
        top_widget_layout.addStretch(1)
        top_widget_layout.addWidget(self.control_btn)
        top_widget_layout.addStretch(1)
        top_widget_layout.addWidget(self.show_btn)

        self.bottom_widget = QStackedWidget()
        # self.bottom_widget.layout().setStackingMode(QStackedLayout.StackAll)
        bottom_widget_layout = self.bottom_widget.layout()
        bottom_widget_layout.setStackingMode(QStackedLayout.StackAll)

        main_layout.addWidget(top_widget)
        main_layout.addWidget(self.bottom_widget)
        main_layout.setStretch(0, 1)
        main_layout.setStretch(1, 5)

        child_one_win = ChildOneWin()
        child_two_win = ChildTwoWin()
        child_three_win = ChildThreeWin()
        self.bottom_widget.addWidget(child_one_win)
        self.bottom_widget.addWidget(child_two_win)
        self.bottom_widget.addWidget(child_three_win)

        self.bottom_widget.setCurrentIndex(0)

    def call_back_show_btn(self):
        self.bottom_widget.setCurrentIndex(2)

    def call_back_test_btn(self):
        self.bottom_widget.setCurrentIndex(0)

    def call_back_control_btn(self):
        pass
        self.bottom_widget.setCurrentIndex(1)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    # 下面两种方法都可以
    win = WinUIform()
    #win = Winform()
    win.show()
    sys.exit(app.exec_())

演示效果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值