Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent window with blur behind with pyqt

I am trying to creating a pyqt window which is semi-transparent and blurred behind. I have tried with setWindowOpacity to make it semi-transparent but I cannot add blur effect. my code is :

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())

This gives this output.

my output

but I want something like this : my imagination

like image 529
Sayan Paul Avatar asked Feb 21 '19 13:02

Sayan Paul


1 Answers

That's quite not possible in pyqt5 but I am not saying impossible. You just need to do the following-

NOTE: This works only if you use your app in full screen window without borders.

Lets see how. I'm going to use KIVY so please download it.

If you have kivy skip this, kivy installation guide.

Remember, Kivy is supported only in python 2.7, 3.7 and 3.4 so uninstall the Python you have if any version does not match. To find your current Python version type 'python version' in cmd.

  1. Type 'pip install kivy' in cmd

  2. There might be an issue during the installation so as I said uninstall unsupported Python version.

  3. Following packages are required so please download them if you don't have any of them.

    • PIL # installation pip install PIL
    • pyautogui # installation pip install pyautogui
  4. Explanation: Let's look how this is going to work, in Python it's a bit difficult to use

DWM (Desktop Window Manager) api that helps to blur the window behind. However in C++ there is built-in function called EnableBlurBehind() to blur the window behind using DWM api.

In our example, first, we are going to take a screenshot using pyautogui package. Second, blur the image(screenshot or background) third, set the image in canvas. Voila, you have window blurred behind. So let's get the concept working.

Copy this and paste into IDE only if you have downloaded required library

Main python file, save as 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 Language file save as build.kv else it won't work

<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()

If you don't know how to open file.kv in your IDE search it on google. Fix any unconventional issues if you have them.

like image 80
PyGuy Avatar answered Sep 22 '22 02:09

PyGuy