python - 透明窗口,后面带有 pyqt 模糊

标签 python qt pyqt transparency blur

我正在尝试创建一个半透明且后面模糊的 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())

这给出了这个输出。

my output

但我想要这样的东西:
my imagination

最佳答案

这在 pyqt5 中是完全不可能的,但我并不是说不可能。您只需要执行以下操作 -
注意:这仅适用于您在无边框的全屏窗口中使用您的应用程序。
让我们看看如何。我将使用 KIVY,所以请下载它。
如果你有 kivy 跳过这个,kivy 安装指南。
请记住,仅在 python 2.7、3.7 和 3.4 中支持 Kivy,因此如果任何版本不匹配,请卸载您拥有的 Python。要查找您当前的 Python 版本,请在 cmd 中键入“python 版本”。

  • 在 cmd 中输入“pip install kivy”
  • 安装过程中可能会出现问题,所以我说卸载不支持的 Python 版本。
  • 以下软件包是必需的,因此如果您没有这些软件包,请下载它们。
  • PIL # 安装 pip install PIL
  • pyautogui # 安装 pip install pyautogui

  • 说明:让我们看看它是如何工作的,在 Python 中它有点难以使用

  • DWM(桌面窗口管理器)api,有助于模糊后面的窗口。然而,在 C++ 中有一个名为 EnableBlurBehind() 的内置函数可以使用 DWM api 模糊后面的窗口。
    在我们的示例中,首先,我们将使用 pyautogui 包进行截图。第二,模糊图像(屏幕截图或背景)第三,在 Canvas 中设置图像。瞧,你后面的 window 模糊了。所以让我们让这个概念发挥作用。
    仅当您已下载所需的库时才复制并粘贴到 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,请在 google 上搜索。解决任何非常规问题(如果有)。

    关于python - 透明窗口,后面带有 pyqt 模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54807743/

    相关文章:

    python - PyQt 应用程序中的线程 : Use Qt threads or Python threads?

    python - 等到 QtWidgets.QMainWindow 关闭后再继续

    python - 用空行填充QtableView的底部空间

    python - 使用 openpyxl 保存更改

    python - 如何在 Pycharm 中调试

    qt - QML 桌面中的单选按钮

    python - 使用 QXmlStreamReader 的 XML 解析不返回所有元素

    python - 用 Python 处理 HTML 表单数据?

    python - 如果日期时间早于 Python 中的 x 个月,如何锻炼

    Qt 5.4-QT_MESSAGELOGCONTEXT