import sys
from PyQt5.QtWidgets import QApplication, QDialog, QVBoxLayout, QLabel
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QPalette, QColor
class UnclosableDialog(QDialog):
def __init__(self, parent=None):
super(UnclosableDialog, self).__init__(parent, Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint) # Always top and borderless
# Set window transparency
self.setWindowOpacity(0.5)
# Remove the close button
self.setWindowFlags(self.windowFlags() & ~Qt.WindowCloseButtonHint)
# Set the window size and position
self.setGeometry(100, 100, 300, 200) # Here (100, 100) is the coordinates of the upper left corner of the window, not the upper right corner
# Layout and Controls
layout = QVBoxLayout()
label = QLabel("This is a top dialog that cannot be closed!", self)
layout.addWidget(label)
self.setLayout(layout)
# Rewrite keyPressEvent to ignore ESC key
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
event.ignore() # Ignore ESC key events
else:
super().keyPressEvent(event) # For other keys, call the implementation of the base class
def main():
app = QApplication(sys.argv)
dialog = UnclosableDialog()
dialog.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()