[英]Fail to run pyqt5 app after compiling the code using cython
Here is a simple PyQt5 application.这是一个简单的 PyQt5 应用程序。 There is a QCheckBox
in it and I connected its clicked
signal to foo()
function. By clicking on this checkbox I pass two parameters to the foo()
function and print those parameters to the console.其中有一个QCheckBox
,我将其clicked
信号连接到foo()
function。通过单击此复选框,我将两个参数传递给foo()
function 并将这些参数打印到控制台。 It works fine however after I compile this code using cython compiler by clicking on this checkbox I get a TypeError
.它工作正常但是在我通过单击此复选框使用 cython 编译器编译此代码后我得到一个TypeError
。
main.py:主要文件:
from PyQt5.QtWidgets import QMainWindow, QApplication, QCheckBox
from functools import partial
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.chb = QCheckBox(self)
self.chb.setText('My CheckBox')
self.chb.move(10, 10)
self.chb.clicked.connect(
partial(self.foo, 'para1', 'para2')
)
def foo(self, p1, p2):
print(p1, p2)
def main():
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Error:错误:
Traceback (most recent call last):
File "main.pyx", line 15, in main.MainWindow.foo
def foo(self, p1, p2):
TypeError: foo() takes exactly 3 positional arguments (4 given)
setup.py:安装程序.py:
from setuptools import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize("main.pyx"))
I'm using python 3.7 on a windows 10 machine.我在 windows 10 机器上使用 python 3.7。
Cython==0.29.24 and PyQt5==5.15.4 Cython==0.29.24 和 PyQt5==5.15.4
It seems that when cythonized the default argument is sent, so a possible solution is to pass that argument to it:似乎当 cythonized 发送默认参数时,一个可能的解决方案是将该参数传递给它:
def foo(self, p1, p2, checked):
print(p1, p2)
Another option is not to use functools.partial but rather nested functions:另一种选择是不使用 functools.partial 而是使用嵌套函数:
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.chb = QCheckBox(self)
self.chb.setText("My CheckBox")
self.chb.move(10, 10)
self.chb.clicked.connect(self.foo('para1', 'para2'))
def foo(self, p1, p2):
def wrapper():
print(p1, p2)
return wrapper
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.