-->

PyQt - How do I fade the desktop background behind

2020-07-30 06:38发布

问题:

I'm wondering if pyqt in PYTHON has this much capability, but is it possible to have the dialog be in the center of the screen(i know that much is possible) and the sourounding area out side of the dialog has an opacity of about of about 50 percent so basicly its still visable but you can't totatlly see it. is this possible? Make sure you understand im talking about outside of the dialog box not inside. I want this for my simple lockscreen app it not very secure but I'll work on that later. I have tried

self.setWindowOpacity(.8)

but that only applies to the window i want to affet to the outside

I know this doesn't sound very easy to understand but if you need more explanation let me know in the comments.

回答1:

Do you want the background (desktop) to have less opacity? There's nothing "behind" the background, so changing opacity doesn't make any sense. I guess you want something similar to how Windows UAC dialog works. Like when you install software a confirm dialog shows up and the rest of the screen goes blackish.

This can be emulated by creating a fullscreen borderless window in a single color with reduced opacity and open your dialog on top of that.

For a frameless/borderless window i find references to QtCore.Qt.FramelessWindowHint and a method called QWindow.setMask(). Either one might work.

Heres an example of FramelessWindowHint usage.

Hope this is helpful.

Edit: Added code example (Based on the example from the link):

import sys
from PySide import QtCore, QtGui

class MyWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.b = QtGui.QPushButton("exit", self, clicked=self.close)
        self.setWindowOpacity(.8)
        self.setStyleSheet("QMainWindow { background: 'black'}");

        self.dialog = QtGui.QDialog()
        self.dialog.setModal(True)
        self.dialog.show()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyWindow()
    myapp.setGeometry(app.desktop().screenGeometry())
    myapp.show()
    sys.exit(app.exec_())

This will only work for a single screen. You'll have to enumerate screens and create a window for each seperate screen to cover multi-screen setups. I'll leave that as an exercise for you.



标签: python pyqt