Ich möchte ein Vollbild-Fenster mit halbtransparenten Hintergrund, aber voll sichtbare Kinder-Widgets (Art der Overlay-Effekt) erstellen.PyQt5: Erstellen Sie ein halbtransparentes Fenster mit nicht transparenten Kindern
Hier ist, was ich bisher:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
app = QApplication(sys.argv)
# Create the main window
window = QMainWindow()
window.setWindowOpacity(0.3)
window.setAttribute(Qt.WA_NoSystemBackground, True)
window.setWindowFlags(Qt.FramelessWindowHint)
# Create the button
pushButton = QPushButton(window)
pushButton.setGeometry(QRect(240, 190, 90, 31))
pushButton.setText("Finished")
pushButton.clicked.connect(app.quit)
# Center the button
qr = pushButton.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
pushButton.move(qr.topLeft())
# Run the application
window.showFullScreen()
sys.exit(app.exec_())
Dies schafft eine halbtransparente Wirkung, sondern auch die Taste ist halbtransparent.
Ich habe auch versucht
window.setAttribute(Qt.WA_TranslucentBackground, True)
aber ohne Erfolg die
window.setWindowOpacity(0.3)
mit diesem Aufruf zu ersetzen, in diesem Fall ist der Hintergrund vollständig transparent war (während die Taste war richtig voll sichtbar).
Lösung: (realisiert durch Aaron Vorschlag):
Der Trick ist, eine benutzerdefinierte paintEventO für das Hauptfenster bei der Umsetzung.
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class CustomWindow(QMainWindow):
def paintEvent(self, event=None):
painter = QPainter(self)
painter.setOpacity(0.7)
painter.setBrush(Qt.white)
painter.setPen(QPen(Qt.white))
painter.drawRect(self.rect())
app = QApplication(sys.argv)
# Create the main window
window = CustomWindow()
window.setWindowFlags(Qt.FramelessWindowHint)
window.setAttribute(Qt.WA_NoSystemBackground, True)
window.setAttribute(Qt.WA_TranslucentBackground, True)
# Create the button
pushButton = QPushButton(window)
pushButton.setGeometry(QRect(240, 190, 90, 31))
pushButton.setText("Finished")
pushButton.clicked.connect(app.quit)
# Center the button
qr = pushButton.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
pushButton.move(qr.topLeft())
# Run the application
window.showFullScreen()
sys.exit(app.exec_())
Was ist passiert, dass der Knopf noch halbtransparent, und der Hintergrund wird mit einer systemdefinierten Farbe versehen (anstelle von Schwarz ist er jetzt weißlich). Ich bin mir nicht sicher, dass setWindowOpacity auf Nicht-Fenster-Widgets funktioniert. – Enuy
Redigierte meine Antwort. Ich habe es manchmal so gemacht und es hat immer so funktioniert, wie du es brauchst. – Aaron
Okay, ich werde versuchen, diesen QT-Pseudocode in PyQT umzuwandeln und zu sehen, wie das funktioniert. – Enuy