Say*_*aul 6 python qt transparency pyqt 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())
Run Code Online (Sandbox Code Playgroud)
这给出了这个输出。
小智 2
这在 pyqt5 中是不可能的,但我并不是说不可能。您只需执行以下操作 -
注意:仅当您在无边框的全屏窗口中使用应用程序时,此功能才有效。
让我们看看如何。我要使用 KIVY,所以请下载它。
如果您有 kivy,请跳过此 kivy 安装指南。
请记住,Kivy 仅在 python 2.7、3.7 和 3.4 中受支持,因此如果有任何版本不匹配,请卸载您拥有的 Python。要查找当前的 Python 版本,请在 cmd 中输入“python version”。
在cmd中输入“pip install kivy”
安装过程中可能会出现问题,所以正如我所说,卸载不支持的Python版本。
需要以下软件包,如果您没有这些软件包,请下载它们。
说明:让我们看看这是如何工作的,在 Python 中使用起来有点困难
DWM(桌面窗口管理器)API 有助于模糊后面的窗口。然而,在 C++ 中,有一个名为 EnableBlurBehind() 的内置函数,可以使用 DWM api 模糊后面的窗口。
在我们的示例中,首先,我们将使用 pyautogui 包截取屏幕截图。第二,模糊图像(屏幕截图或背景)第三,在画布中设置图像。瞧,你后面的窗户模糊了。那么让我们把这个概念付诸实践吧。
主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()
Run Code Online (Sandbox Code Playgroud)
<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()
Run Code Online (Sandbox Code Playgroud)
如果您不知道如何在 IDE 中打开 file.kv,请在 google 上搜索。如果有任何非常规问题,请解决它们。
归档时间: |
|
查看次数: |
2557 次 |
最近记录: |