pyqt 窗口透明

窗口透明,按钮不透明

        # 窗口属性:透明、置顶

        flags = Qt.WindowType.FramelessWindowHint | Qt.WindowType.WindowStaysOnTopHint | Qt.WindowType.Tool | Qt.WindowType.X11BypassWindowManagerHint
        self.setWindowFlags(flags)

        self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
        self.setAttribute(Qt.WidgetAttribute.WA_NoSystemBackground, True)
        self.setAttribute(Qt.WidgetAttribute.WA_ShowWithoutActivating, True)

demo:



from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QFont
import sys

class TransparentWindow(QWidget):
    def __init__(self):
        super().__init__()
        # 窗口透明

        # 窗口属性:透明、置顶

        flags = Qt.WindowType.FramelessWindowHint | Qt.WindowType.WindowStaysOnTopHint | Qt.WindowType.Tool | Qt.WindowType.X11BypassWindowManagerHint
        self.setWindowFlags(flags)

        self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
        self.setAttribute(Qt.WidgetAttribute.WA_NoSystemBackground, True)
        self.setAttribute(Qt.WidgetAttribute.WA_ShowWithoutActivating, True)

        self.setGeometry(100, 100, 800, 800)

        # 子控件:不透明(黑底 + 白字)
        self.label = QLabel("窗口透明,控件不透明", self)
        self.label.setGeometry(50, 50, 500, 300)
        self.label.setAlignment(Qt.AlignCenter)
        self.label.setFont(QFont("Arial", 20))
        self.label.setStyleSheet("background-color: black; color: white; border-radius: 20px;")
        # self.setAttribute(Qt.WA_TranslucentBackground, True)
        # self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
        # 也可以设置图片
        # pix = QPixmap("test.png")
        # self.label.setPixmap(pix)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = TransparentWindow()
    w.show()
    sys.exit(app.exec_())

Pyqt5是一个用于创建图形用户界面(GUI)应用程序的Python库。要创建一个透明背景的窗口,可以使用QMainWindow或QWidget类,并使用setStyleSheet()方法来设置窗口的样式。 首先,导入必要的模块: ``` from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtGui import QColor ``` 然后,创建一个继承自QMainWindow或QWidget的自定义窗口类: ``` class TransparentWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint) # 去除窗口边框 self.setAttribute(QtCore.Qt.WA_TranslucentBackground) # 设置窗口背景透明 self.setGeometry(100, 100, 400, 300) # 设置窗口位置和大小 self.setStyleSheet("background-color: rgba(0, 0, 0, 0);") # 设置背景颜色为透明 ``` 在窗口类的构造函数中,我们首先将窗口的样式设置为没有边框的窗口(通过去除窗口标志位)。然后,通过设置窗口属性来使窗口背景透明。接下来,设置窗口的位置和大小。最后,使用setStyleSheet()方法将窗口的背景颜色设置为透明。 最后,我们创建一个QApplication对象,并在应用程序的主循环中显示窗口: ``` if __name__ == "__main__": app = QApplication(sys.argv) window = TransparentWindow() window.show() sys.exit(app.exec_()) ``` 在这个例子中,我们创建了一个透明窗口,它没有边框,并且背景颜色是透明的。通过这种方式,可以轻松创建一个具有透明背景的窗口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值