PyQT5
给控件设置Opacity
,方法:QGraphicsOpacityEffect
下面这段示例代码中,myshow
是一个QPushButton
,给其设置透明度的方法如下:
op = QtWidgets.QGraphicsOpacityEffect()
op.setOpacity(0.5)
myshow.setGraphicsEffect(op)
myshow.setAutoFillBackground(True)
完整示例,可以直接粘贴使用:
from PyQt5 import QtWidgets, QtCore
import sys
from PyQt5.QtCore import *
import time
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myshow = QtWidgets.QPushButton('Button')
myshow.setStyleSheet("""
padding-left: 10px;
padding-right: 10px;
padding-top: 1px;
padding-bottom: 1px;
border:1px solid #0073df;
border-radius:5px;
background: #167ce9;
color: #fff;
""")
def changeOpacity(_):
op = QtWidgets.QGraphicsOpacityEffect()
op.setOpacity(0.5)
myshow.setGraphicsEffect(op)
myshow.setAutoFillBackground(True)
myshow.clicked.connect(changeOpacity)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(myshow)
main = QtWidgets.QWidget()
main.setLayout(layout)
main.show()
sys.exit(app.exec_())