从自定义模块运行Boost-Python对象的方法

时间:2017-02-03 02:38:29

标签: python c++ boost boost-python

我试图使用boost :: python将自定义python对象(Chart)导入到某些c ++代码中。这是一些相关的python目标代码:

chart.py

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib.style as style
import pandas

class Chart(object):
    def __init__(self):
        self.fig = plt.figure()
        self.fig.canvas.set_window_title('Data')
        self.ax1 = self.fig.add_subplot(1,1,1)
        self.ani = animation.FuncAnimation(self.fig,self.animate, interval=10)
        self.data = pandas.DataFrame({"time":np.array([0]),"c":np.array([0]),"b":np.array([0])})
        self.data.set_index("time", drop=True, inplace=True)
        self.visible = False

    def show(self):
        if self.visible == False:
            self.visible = True
            plt.show()

我试图在下面的一些c ++代码中使用该对象:

grapher.cpp

#import <boost/python.hpp>
#import <string>
#import <Python.h>
#include <iostream>

namespace py = boost::python;

int main(int argc, char ** argsv)
{
  // Try to run, otherwise give an error
  try{
       // Add the current working directory to the python path variable so that chart.py can be imported.
       setenv("PYTHONPATH", ".", 1);
       Py_Initialize();

       // Import the chart module
       py::object mod = py::import("chart");
       // Create the chart object
       py::object myChart = mod.attr("Chart")();
       // Show the chart
       myChart.show();
   }
   catch(boost::python::error_already_set const &){
       std::string perror_str = parse_python_exception();
       std::cout << "Error in Python: " << perror_str << std::endl;
   }

  return 0;
}

但是,当我尝试编译时,我收到以下错误:

grapher.cpp: In function ‘int main(int, char**)’:
grapher.cpp:73:16: error: ‘class boost::python::api::object’ has no member named ‘show’
    myChart.show();

我还不是特别流利的C ++,所以我完全有可能错过一些明显的东西。任何帮助将不胜感激!

0 个答案:

没有答案