我正在尝试创建一个半透明且后面模糊的 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())
这给出了这个输出。
但我想要这样的东西:
最佳答案
这在 pyqt5 中是完全不可能的,但我并不是说不可能。您只需要执行以下操作 -
注意:这仅适用于您在无边框的全屏窗口中使用您的应用程序。
让我们看看如何。我将使用 KIVY,所以请下载它。
如果你有 kivy 跳过这个,kivy 安装指南。
请记住,仅在 python 2.7、3.7 和 3.4 中支持 Kivy,因此如果任何版本不匹配,请卸载您拥有的 Python。要查找您当前的 Python 版本,请在 cmd 中键入“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/