窗口透明,按钮不透明
# 窗口属性:透明、置顶
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_())
530

被折叠的 条评论
为什么被折叠?



