user32.SetWindowCompositionAttribute与PyQt5一起导致ctypes.ArgumentError

2024-10-02 14:15:57 发布

您现在位置:Python中文网/ 问答频道 /正文

最近我安装了一个名为BlurWindowVia的python包

pip install BlurWindow

这个python包有助于实现应用程序窗口的透明模糊背景。虽然它说这个模块与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。但是我已经在Python3.6和3.8上测试了这段代码,但是PyQt5库在任何地方都不起作用,而是显示了相同的错误

我几乎完成了我的项目,我将在其中实现这个模糊效果。由于我在项目中使用了PyQt5库,用PySide2替换PyQt5库对我来说是个大麻烦。任何帮助都将不胜感激


Tags: 代码infrompyimportselflibline
1条回答
网友
1楼 · 发布于 2024-10-02 14:15:57

起初,我认为它可能与[SO]: ctypes.ArgumentError when using kivy with pywinauto相同,但事实证明它更简单

这是模糊窗口中的一个bug。运行blurWindow.py会产生相同的行为(使用PyQt5.12.3

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q068651351]> sopr.bat
### Set shorter prompt to better fit when pasted in StackOverflow (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

如图所示,两个模块qtwidts.QWidget.winId的返回值之间存在“小”差异CTypes不知道如何转换PyQt5

短篇小说

由于您在此处复制了主窗口代码,因此修复非常简单,请替换:

GlobalBlur(self.winId(), Dark=False, Acrylic=False, QWidget=self)

作者:

GlobalBlur(int(self.winId()), Dark=False, Acrylic=False, QWidget=self)

它应该很好用。但是在下一节的开头检查URL,看看你可能会遇到什么

长话短说

这是关于模糊窗口的更多信息。错误出现在通过CTypes调用(!!!未记录!!!user32.SetWindowCompositionAttribute
这里有一点很重要:[SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer)。请注意,它是Un定义的Behavior,并且它只能靠运气工作

提交[GitHub]: Peticali/PythonBlurBehind - Fix SetWindowCompositionAttribute failure于210805合并)。它包含对当前问题的正确修复

您可以下载补丁,并在本地应用更改。查看[SO]: Run/Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (@CristiFati's answer)修补utrunner部分),了解如何在Win上应用修补程序(基本上,以一个“+”符号开头的每一行进入,以一个“-”符号开头的每一行退出)。我正在使用Cygwinbtw
但由于它只是一个文件,您可以下载它并覆盖现有文件
在任何情况下,首先备份它

相关问题 更多 >

    热门问题