PyQt5 - 如何创建半透明窗口?
当我们在 PyQt5 中设计应用程序时,默认情况下使用主窗口出现,该窗口是不透明的,但我们也可以将其设为透明。我们可以通过使用属于QWidget class
的setWindowOpacity()
方法来做到这一点。
Syntax : setWindowOpacity(0.5)
Argument : It takes float value as argument : 0 for completely transparent and 1 for opaque.
Action performed : Makes the window transparent.
代码 :
# importing the required libraries
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
# set the title
self.setWindowTitle("Python")
self.setWindowOpacity(0.5)
# setting the geometry of window
self.setGeometry(60, 60, 600, 400)
# creating a label widget
self.label_1 = QLabel("transparent ", self)
# moving position
self.label_1.move(100, 100)
self.label_1.adjustSize()
# show all the widgets
self.show()
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :