Home > Article > Backend Development > How to set control transparency in pyqt5
pyqt5 Set control transparency method: first use the QGraphicsOpacityEffect class to set the transparency effect of the graphic element; then use "element name.setOpacity (transparent value)" to set the transparency of the element, the parameter value is between 0 and "1.0" That’s it.
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
PyQT5 sets Opacity for the control, method: QGraphicsOpacityEffect
In the following sample code, myshow is a QPushButton, and the method for setting transparency is as follows :
op = QtWidgets.QGraphicsOpacityEffect() op.setOpacity(0.5) myshow.setGraphicsEffect(op) myshow.setAutoFillBackground(True)
Complete example, you can paste it directly and use it:
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_())
Related free learning recommendations: python video tutorial!
The above is the detailed content of How to set control transparency in pyqt5. For more information, please follow other related articles on the PHP Chinese website!