user32.SetWindowCompositionAttribute 使用 PyQt5 导致 ctypes.ArgumentError
user32.SetWindowCompositionAttribute with PyQt5 causes ctypes.ArgumentError
最近我通过
安装了一个名为 BlurWindow 的 python 包
pip install BlurWindow
此 python 包有助于实现应用程序的透明模糊背景 window。虽然它说这个模块适用于 tkinter、qt 和许多其他包,但在我的例子中它只适用于 tkinter 和 PySide2 但不适用于 PyQt5。
我的代码如下:
import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *
# from PyQt5.QtWidgets import *
# from PyQt5.QtCore import *
from BlurWindow.blurWindow import GlobalBlur
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setAttribute(Qt.WA_TranslucentBackground)
# self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint | Qt.WindowMinMaxButtonsHint) # set window flags
self.resize(500, 400)
l = QLabel('How do you like the blurry window?', self)
GlobalBlur(self.winId(), Dark=False, Acrylic=False, QWidget=self)
self.setStyleSheet("color: white; background-color: rgba(0, 0, 0, 0)")
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
这段代码工作正常。但是当我评论这两行(第3行和第4行)时:
from PySide2.QtWidgets import *
from PySide2.QtCore import *
并取消注释这两行(第 6 行和第 7 行):
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
发生此错误:
Traceback (most recent call last):
File "E:\PythonProjects\Zambo\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "E:\PythonProjects\Zambo\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "E:\PythonProjects\Zambo\lib\site-packages\BlurWindow\blurWindow.py", line 191, in <module>
mw = MainWindow()
File "E:\PythonProjects\Zambo\lib\site-packages\BlurWindow\blurWindow.py", line 185, in __init__
GlobalBlur(hWnd,Dark=True,QWidget=self)
File "E:\PythonProjects\Zambo\lib\site-packages\BlurWindow\blurWindow.py", line 160, in GlobalBlur
blur(HWND,hexColor,Acrylic,Dark)
File "E:\PythonProjects\Zambo\lib\site-packages\BlurWindow\blurWindow.py", line 136, in blur
user32.SetWindowCompositionAttribute(HWND, data)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: Don't know how to convert parameter 1
我不知道为什么这段代码适用于 PySide2 但不适用于 PyQt5!
这是我在所示代码中使用的 blurWindow.py 模块。
我在 windows 10 并在 anaconda 虚拟环境中使用 Python 3.9.5。但是我已经在 Python 3.6 和 3.8 上测试了这段代码,但是 PyQt5 库在任何地方都不起作用,而是显示相同的错误。
我即将完成我将实现此模糊效果的项目。而且由于我在我的项目中使用了 PyQt5 库,所以将 PyQt5 库替换为 PySide2 对我来说将是一个很大的麻烦。任何帮助将不胜感激!
一开始以为和是一回事,结果发现简单多了。
这是 BlurWindow 中的错误。 运行 blurWindow.py 产生相同的行为(PyQt5.12.3).
[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q068651351]> sopr.bat
### Set shorter prompt to better fit when pasted in Whosebug (or other) pages ###
[prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.08.07_test0\Scripts\python.exe" BlurWindow\blurWindow.py
Traceback (most recent call last):
File "BlurWindow\blurWindow.py", line 188, in <module>
mw = MainWindow()
File "BlurWindow\blurWindow.py", line 182, in __init__
GlobalBlur(hWnd,Dark=True,QWidget=self)
File "BlurWindow\blurWindow.py", line 157, in GlobalBlur
blur(HWND,hexColor,Acrylic,Dark)
File "BlurWindow\blurWindow.py", line 133, in blur
user32.SetWindowCompositionAttribute(HWND, data)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: Don't know how to convert parameter 1
[prompt]> :: Test the 2 modules
[prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.08.07_test0\Scripts\python.exe" -c "import PyQt5.QtWidgets as pqqw; help(pqqw.QWidget.winId)"
Help on built-in function winId:
winId(...)
winId(self) -> sip.voidptr
[prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.08.07_test0\Scripts\python.exe" -c "import PySide2.QtWidgets as psqw; help(psqw.QWidget.winId)"
Help on method_descriptor:
winId(self) -> int
winId(self) -> int
如图所示,2 个模块 QtWidgets.QWidget.winId 的 return 值之间存在“小”差异。 CTypes不知道要转换PyQt5的。
短篇小说
由于您在此处复制了 MainWindow 代码,因此修复很简单,替换为:
GlobalBlur(self.winId(), Dark=False, Acrylic=False, QWidget=self)
作者:
GlobalBlur(int(self.winId()), Dark=False, Acrylic=False, QWidget=self)
它应该可以正常工作。但是请查看下一节开头的 URL,看看您可能 运行 进入什么。
说来话长
这是关于 BlurWindow 的更多信息。错误是通过 CTypes[=72= 调用 (!!!undocumented!!!) user32.SetWindowCompositionAttribute ].
这里有一个重点:。请注意,它是 Undefined Behavior,它只能靠运气。
已提交 [GitHub]: Peticali/PythonBlurBehind - Fix SetWindowCompositionAttribute failure(合并 于 210805)。它包含对当前问题的正确修复。
您可以下载补丁,并在本地应用更改。检查 (修补 ut运行ner 部分)了解如何在 Win[ 上应用补丁=72=](基本上,以 一个“+” 符号开头的每一行,以及以 一个“-” 开头的每一行标志熄灭)。我正在使用 Cygwin、btw。
但由于它只是一个文件,您可以下载它并覆盖现有文件。
无论如何,先备份!
最近我通过
安装了一个名为 BlurWindow 的 python 包pip install BlurWindow
此 python 包有助于实现应用程序的透明模糊背景 window。虽然它说这个模块适用于 tkinter、qt 和许多其他包,但在我的例子中它只适用于 tkinter 和 PySide2 但不适用于 PyQt5。
我的代码如下:
import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *
# from PyQt5.QtWidgets import *
# from PyQt5.QtCore import *
from BlurWindow.blurWindow import GlobalBlur
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setAttribute(Qt.WA_TranslucentBackground)
# self.setWindowFlags(Qt.Window | Qt.FramelessWindowHint | Qt.WindowMinMaxButtonsHint) # set window flags
self.resize(500, 400)
l = QLabel('How do you like the blurry window?', self)
GlobalBlur(self.winId(), Dark=False, Acrylic=False, QWidget=self)
self.setStyleSheet("color: white; background-color: rgba(0, 0, 0, 0)")
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
这段代码工作正常。但是当我评论这两行(第3行和第4行)时:
from PySide2.QtWidgets import *
from PySide2.QtCore import *
并取消注释这两行(第 6 行和第 7 行):
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
发生此错误:
Traceback (most recent call last):
File "E:\PythonProjects\Zambo\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "E:\PythonProjects\Zambo\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "E:\PythonProjects\Zambo\lib\site-packages\BlurWindow\blurWindow.py", line 191, in <module>
mw = MainWindow()
File "E:\PythonProjects\Zambo\lib\site-packages\BlurWindow\blurWindow.py", line 185, in __init__
GlobalBlur(hWnd,Dark=True,QWidget=self)
File "E:\PythonProjects\Zambo\lib\site-packages\BlurWindow\blurWindow.py", line 160, in GlobalBlur
blur(HWND,hexColor,Acrylic,Dark)
File "E:\PythonProjects\Zambo\lib\site-packages\BlurWindow\blurWindow.py", line 136, in blur
user32.SetWindowCompositionAttribute(HWND, data)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: Don't know how to convert parameter 1
我不知道为什么这段代码适用于 PySide2 但不适用于 PyQt5!
这是我在所示代码中使用的 blurWindow.py 模块。
我在 windows 10 并在 anaconda 虚拟环境中使用 Python 3.9.5。但是我已经在 Python 3.6 和 3.8 上测试了这段代码,但是 PyQt5 库在任何地方都不起作用,而是显示相同的错误。
我即将完成我将实现此模糊效果的项目。而且由于我在我的项目中使用了 PyQt5 库,所以将 PyQt5 库替换为 PySide2 对我来说将是一个很大的麻烦。任何帮助将不胜感激!
一开始以为和
这是 BlurWindow 中的错误。 运行 blurWindow.py 产生相同的行为(PyQt5.12.3).
[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q068651351]> sopr.bat ### Set shorter prompt to better fit when pasted in Whosebug (or other) pages ### [prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.08.07_test0\Scripts\python.exe" BlurWindow\blurWindow.py Traceback (most recent call last): File "BlurWindow\blurWindow.py", line 188, in <module> mw = MainWindow() File "BlurWindow\blurWindow.py", line 182, in __init__ GlobalBlur(hWnd,Dark=True,QWidget=self) File "BlurWindow\blurWindow.py", line 157, in GlobalBlur blur(HWND,hexColor,Acrylic,Dark) File "BlurWindow\blurWindow.py", line 133, in blur user32.SetWindowCompositionAttribute(HWND, data) ctypes.ArgumentError: argument 1: <class 'TypeError'>: Don't know how to convert parameter 1
[prompt]> :: Test the 2 modules [prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.08.07_test0\Scripts\python.exe" -c "import PyQt5.QtWidgets as pqqw; help(pqqw.QWidget.winId)" Help on built-in function winId: winId(...) winId(self) -> sip.voidptr [prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.08.07_test0\Scripts\python.exe" -c "import PySide2.QtWidgets as psqw; help(psqw.QWidget.winId)" Help on method_descriptor: winId(self) -> int winId(self) -> int
如图所示,2 个模块 QtWidgets.QWidget.winId 的 return 值之间存在“小”差异。 CTypes不知道要转换PyQt5的。
短篇小说
由于您在此处复制了 MainWindow 代码,因此修复很简单,替换为:
GlobalBlur(self.winId(), Dark=False, Acrylic=False, QWidget=self)
作者:
GlobalBlur(int(self.winId()), Dark=False, Acrylic=False, QWidget=self)
它应该可以正常工作。但是请查看下一节开头的 URL,看看您可能 运行 进入什么。
说来话长
这是关于 BlurWindow 的更多信息。错误是通过 CTypes[=72= 调用 (!!!undocumented!!!) user32.SetWindowCompositionAttribute ].
这里有一个重点:
已提交 [GitHub]: Peticali/PythonBlurBehind - Fix SetWindowCompositionAttribute failure(合并 于 210805)。它包含对当前问题的正确修复。
您可以下载补丁,并在本地应用更改。检查
但由于它只是一个文件,您可以下载它并覆盖现有文件。
无论如何,先备份!