Click here to Skip to main content
Click here to Skip to main content

Embedding Python In Your C++ Application

By , 22 May 2006
 
py_embed_res.zip
article_res
gui_demo
README
Screenshot.png
pyembed
README
test
README
#include <iostream>
#include <vector>
using std::cout;
using std::cin;

#include "pyembed.h"

static void print_menu();
static void list_users();

// this is the only function that should interest you.
// all python calls go here
static void add_user();

// data of users created using the UI are stored here
// in a simple text format
static std::vector<std::string> users;

static int global_argc;
static char** global_argv;

int
main(int argc, char** argv)
{
  global_argc = argc;
  global_argv = argv;
  print_menu();
  return 0;
}

void
print_menu()
{
  cout << "1. List users\n";
  cout << "2. Add user\n";
  cout << "3. Quit\n";
  cout << ">> ";
  int option = 0;
  cin >> option;
  switch (option)
    {
    case 1:
      list_users();
      break;
    case 2:
      add_user();
      break;
    case 3:
      exit(0);
      break;
    default:
      cout << "Not a valid option.\n";
    }
  print_menu();
}

void
list_users()
{
  size_t s = users.size();
  for (size_t i=0; i<s; i++)
    cout << users[i] << '\n';
  cout << '\n';
}

void
add_user()
{
  try
    {
      pyembed::Python python(global_argc, global_argv);
      python.load("adduserform");

      // call the python function that creates and displays the GUI
      pyembed::String_map ret; // attributes of the new user are returned here
      pyembed::Arg_map args; // our empty argument list
      python.call("show_win", args, ret);

      if (ret.size() <= 1) // the user clicked cancel
	return;

      // create a single string from the returned map
      // and add it to the users list
      std::string user = "";
      pyembed::String_map::const_iterator it;
      for (it = ret.begin(); it != ret.end(); it++)
	{
	  user += '[';
	  user += it->first;
	  user += ':';
	  user += it->second;
	  user += ']';
	}
      users.push_back(user);
    }
  catch (pyembed::Python_exception ex)
    {
      cout << "Python error: " << ex.what() << '\n';
    }
}


By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

About the Author

AnOldGreenHorn
India India
Member
No Biography provided

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 23 May 2006
Article Copyright 2006 by AnOldGreenHorn
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid