Reputation: 439
I am attempting to make the top border of my app semi transparent (rounded edges) by using the partially transparent PNG below:
This does not work and ends up having the corners filled in when the app is launched. A simplified code is below:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(819, 682)
MainWindow.setMinimumSize(QtCore.QSize(819, 682))
MainWindow.setMaximumSize(QtCore.QSize(819, 682))
MainWindow.setWindowOpacity(1.0)
MainWindow.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.TopBarFrame = QtWidgets.QFrame(self.centralWidget)
self.TopBarFrame.setGeometry(QtCore.QRect(-1, -1, 821, 31))
self.TopBarFrame.setStyleSheet("background-image:url(\"TopBar.png\")")
self.TopBarFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.TopBarFrame.setFrameShadow(QtWidgets.QFrame.Raised)
self.TopBarFrame.setObjectName("TopBarFrame")
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Using the .setAttribute(QtCore.Qt.WA_TranslucentBackground)
only makes the whole window black. All help appreciated.
Upvotes: 3
Views: 1336
Reputation: 558
Why do you useMainWindow.setWindowOpacity(1.0)
?This completely changes window as well as all widget opacity(transparency).And if you want round corner then dont use any image just writeborder-radius:10px;
(for example)this do the same.And
MainWindow.setAttribute(QtCore.Qt.WA_TranslucentBackground)
works if you set your window to frameless(borderless) otherwise just a black screen appears.After some modification in your code this is final result
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(819, 682)
MainWindow.setMinimumSize(QtCore.QSize(819, 682))
MainWindow.setMaximumSize(QtCore.QSize(819, 682))
MainWindow.setWindowFlags(
QtCore.Qt.FramelessWindowHint
| QtCore.Qt.WindowStaysOnTopHint )
MainWindow.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.TopBarFrame = QtWidgets.QFrame(self.centralWidget)
self.TopBarFrame.setGeometry(QtCore.QRect(-1, -1, 821, 31))
self.TopBarFrame.setStyleSheet('''background:red;
border-top-left-radius:15px;
border-top-right-radius:15px;''')
self.TopBarFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.TopBarFrame.setFrameShadow(QtWidgets.QFrame.Raised)
self.TopBarFrame.setObjectName("TopBarFrame")
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
let me know if you have any problem.
Upvotes: 1