使用PyQt创建带有模糊背景的透明窗口

6

我想创建一个半透明且背后模糊的pyqt窗口。 我尝试使用 setWindowOpacity 来使其半透明,但是我无法添加模糊效果。 我的代码如下:

import sys
from PyQt5 import QtCore, QtWidgets, QtGui


class main(QtWidgets.QDialog):
    def __init__(self):
        super(main, self).__init__()
        self.setMinimumSize(800,500)


        self.setWindowFlags(
            self.windowFlags() | QtCore.Qt.FramelessWindowHint
        )

        # self.setAttribute(QtCore.Qt.WA_TranslucentBackground,on=True)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mw = main()
    mw.setWindowOpacity(.60)
    mw.show()
    sys.exit(app.exec())

这会产生以下输出。

我的输出

但我想要像这样的东西: 我的想象


你可能想尝试 mw.setGraphicsEffect(QtWidgets.QGraphicsBlurEffect()),但我不确定它是否适用于顶层窗口小部件。 - G.M.
2个回答

2
这在pyqt5中不太可能,但我并不是说不可能。你只需要按照以下步骤操作:
注意:这仅适用于在没有边框的全屏窗口中使用应用程序。
让我们看看如何操作。我将使用KIVY,请下载它。
如果您已经安装了kivy,请跳过此步骤,否则请按照kivy安装指南进行安装。
请记住,Kivy仅支持Python 2.7、3.4和3.7,因此请卸载您当前的Python版本,如果任何版本不匹配,请在cmd中输入“python version”查找您当前的Python版本。
1. 在cmd中键入“pip install kivy”
2. 安装过程中可能会出现问题,所以请如我所说卸载不受支持的Python版本。
3. 如果您没有以下包,请下载它们。
PIL # 安装 pip install PIL pyautogui # 安装 pip install pyautogui
解释:让我们看看这将如何工作,在Python中使用DWM(桌面窗口管理器)api有点困难,因为在C++中有内置函数EnableBlurBehind()来使用DWM api模糊后面的窗口。
在我们的示例中,首先,我们将使用pyautogui包拍摄屏幕截图。第二,模糊图像(截图或背景),第三,在画布中设置图像。窗口后面就模糊了。所以让我们开始实现这个概念。
如果您已经下载了所需的库,请将以下内容复制并粘贴到IDE中。
主Python文件,保存为name.py
# first take the screenshot else problem will occur
import pyautogui
# take screenshot
screenshot = pyautogui.screenshot()
screenshot.save('screenshot.png')

# import other required libraries
from PIL import Image, ImageFilter
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from PIL import Image, ImageFilter
from win32api import GetSystemMetrics
from kivy.animation import Animation


# set window size
Window.borderless = True
Window.size = GetSystemMetrics(0), GetSystemMetrics(1)
Window.left = 0
Window.top = 0


class Blur(Widget):

    # Transparent Blur Window Exmple the screenshot
    get_image = Image.open('screenshot.png')
    blur_image = get_image.filter(ImageFilter.GaussianBlur(radius=15))
    blur_image.save('blured.png')

    def anim(self):
        animator = Animation(x=1800, y=500)
        animator.start(self.ids.animate)


class Build(App):
    def build(self):
        return Blur()


Build().run()

     

将KV语言文件保存为build.kv,否则它将无法正常工作

<Blur>:
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'blured.png'
    Button:
        id: animate
        text: 'Here, you can add anything you want now. Click me!'
        bold: True
        italic: True
        pos: 700, 500
        font_size: 25
        size: 400, 300
        background_color: 0,0,0,0
        on_press: root.anim()

如果您不知道如何在IDE中打开file.kv,请搜索谷歌。如果您有任何非常规问题,请进行修复。

0

模糊

您可以执行以下操作:

pip安装BlurWindow

示例代码:

import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *

from BlurWindow.blurWindow import blur



class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.resize(500, 400)

        hWnd = self.winId()
        print(hWnd)
        blur(hWnd)

        self.setStyleSheet("background-color: rgba(0, 0, 0, 0)")



if __name__ == '__main__':
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接