gpt4 book ai didi

python - 在 GTK 应用程序中嵌入 python 程序(C 语言)

转载 作者:行者123 更新时间:2023-11-30 17:23:46 26 4
gpt4 key购买 nike

我正在编写一个 GTK+ GUI(C 语言)应用程序,我之前已经快速开发了一个执行某些任务的 pygtk GUI 应用程序。现在,我不再用 C 和 GTK+ 重写整个 Pygtk GUI 及其计算例程,而是希望在 C GUI 中按一个按钮并启动 python GUI。

在最初的尝试中,我尝试从 C 代码调用 python GUI,但它在运行时崩溃,它提示 GTK 导入和段错误。

请注意,Python GUI 独立应用程序运行良好(独立文件的名称是 FPG.py)。

那么如何有效地将模块和库从 python 导入到 C/C++ 中

这就是我所做的:

#include <Python.h>

int
main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]); /* optional but recommended */
Py_Initialize();

FILE *fp = fopen ("FPG.py", "r+");
PyRun_SimpleString("#!/usr/bin/python2.7");
PyRun_SimpleString("import sys ");
PyRun_SimpleString("from scipy.integrate import odeint ");
PyRun_SimpleString("import math ");
PyRun_SimpleString("import csv ");
PyRun_SimpleString("from decimal import *");
PyRun_SimpleString("from math import pi ");
PyRun_SimpleString("import numpy as np");
PyRun_SimpleString("from datetime import datetime");
PyRun_SimpleString("from time import mktime ");
PyRun_SimpleString("from gi.repository import Gtk, Gio");
PyRun_SimpleString("import os");


PyRun_SimpleString("from datetime import datetime");
PyRun_SimpleString("from time import mktime");

// the actual file
PyRun_SimpleFile(fp,"FPG.py");
Py_Finalize();
return 0;
}

我编译程序如下(我知道它是 C++ 编译器):

g++ $(python-config --cflags) -o simple $(python-config --ldflags) ./simple.cpp

该程序即使编译,在运行时也会崩溃,并显示以下输出可执行文件的摘录。

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib64/python2.7/site-packages/gi/importer.py", line 68, in load_module
dynamic_module._load()
File "/usr/lib64/python2.7/site-packages/gi/module.py", line 291, in _load
overrides_modules = __import__('gi.overrides', fromlist=[self._namespace])
File "/usr/lib64/python2.7/site-packages/gi/overrides/Gtk.py", line 1502, in <module>
initialized, argv = Gtk.init_check(sys.argv)
AttributeError: 'module' object has no attribute 'argv'

(process:25913): Gtk-CRITICAL **: _gtk_style_cascade_get_for_screen: assertion `GDK_IS_SCREEN (screen)' failed

(process:25913): Gtk-CRITICAL **: _gtk_style_provider_private_lookup: assertion `GTK_IS_STYLE_PROVIDER_PRIVATE (provider)' failed

(process:25913): Gtk-CRITICAL **: _gtk_css_lookup_resolve: assertion `GTK_IS_STYLE_PROVIDER_PRIVATE (provider)' failed

(process:25913): Gtk-CRITICAL **: _gtk_css_rgba_value_get_rgba: assertion `rgba->class == &GTK_CSS_VALUE_RGBA' failed

(process:25913): Gtk-CRITICAL **: _gtk_style_provider_private_lookup: assertion `GTK_IS_STYLE_PROVIDER_PRIVATE (provider)' failed

(process:25913): Gtk-CRITICAL **: _gtk_css_lookup_resolve: assertion `GTK_IS_STYLE_PROVIDER_PRIVATE (provider)' failed

(process:25913): Gtk-CRITICAL **: _gtk_css_rgba_value_get_rgba: assertion `rgba->class == &GTK_CSS_VALUE_RGBA' failed

(process:25913): Gtk-CRITICAL **: _gtk_css_rgba_value_get_rgba: assertion `rgba->class == &GTK_CSS_VALUE_RGBA' failed


.
.
.

最佳答案

您输入的 import numpy as np 错过了 i。此外,您还必须为 argv 导入 sys 模块。

关于python - 在 GTK 应用程序中嵌入 python 程序(C 语言),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27427897/

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