gpt4 book ai didi

python - 非 PyQt 模块在 QApplication 启动后停止工作

转载 作者:太空宇宙 更新时间:2023-11-04 05:20:40 26 4
gpt4 key购买 nike

在 2014 年,我编写了一个 PyQt4 应用程序,其中使用了一个非 PyQt 模块,并且它正常工作了将近两年。现在,非 PyQt 模块已在 PyQt 应用程序中按预期停止工作(我有 qt4 和 qt5 端口,两者都有相同的问题)。下面是一个示例代码来说明发生了什么:

#! /usr/bin/env python3
import sys, getopt
from PyQt5.QtWidgets import QApplication # or PyQt4

# non PyQt module:
import mymodule

# demo of the mymodule use. It returns nested list
# with info about objects saved in fname

fname = "/my/file/name"
res0 = mymodule.getnames(fname)
# res0 = [[...],...,[...]] the way it should
#
app = QApplication(sys.argv)
...
# with or without actual run of the application via
# app.exec_()
...
res2 = mymodule.getnames(fname)
# this time the result is:
# res2 = None

最初,我从 PyQt 应用程序中调用 mymodule 的方法,它返回嵌套列表,现在它返回 None。我试图找到任何解释或澄清,为什么观察到的行为发生了变化但没有这样做。

我已经将负责呈现 mymodule 行为的代码隔离到调用 QApplication([sys.argv]) 并且想知道是否有人知道发生了什么。最让我烦恼的是,该模块在应用程序启动之前是可用的,但之后就不再可用了。

关于 mymodule 的一些进一步细节:它是用 mymodule.c 编写并通过 python setup.py install 调用安装的 C 库的包装器。

提前感谢您提出任何意见!

[编辑] 关于 mymodule 的更多信息:

mymodule.c 代码片段

#include <Python.h>
#include <extlib.h>
#include <math.h>

...
static PyObject * mymodule_getnames(PyObject *self, PyObject *args)
{
char *filename;

if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
FILE *fp = extlib_func(filename);
...
PyObject *names = PyList_New(0);
...
PyList_Append(names, Py_BuildValue("s", title));
...
return names;
}

...

int main(int argc, char *argv[])
{
Py_SetProgramName(
#if PY_MAJOR_VERSION >= 3
(wchar_t *)
#endif
argv[0]);
Py_Initialize();
return 0;
}

最佳答案

我已经通过不使用 anaconda 作为我的 python 包管理器解决了这个问题。通过在系统中安装所有东西,它又可以正常工作了。但问题仍然存在,因为它在 anaconda 中不起作用(但在这么长时间内起作用)的原因仍然超出我的理解范围。

我已经发布了这个答案,但如果有人对这种现象做出解释,我不会接受它。如果一段时间后没有其他回复,我会将其标记为已接受以整理问题池。

感谢大家的宝贵时间和建议。

关于python - 非 PyQt 模块在 QApplication 启动后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40466172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com