I am having difficulties in embedding python in a win32 application...

David Sheets dsheets at wam.umd.edu
Sun Jun 27 03:33:17 CEST 1999

    I have been trying to create a program that does some math and graphics
using win32 code, and then uses Python to do a lot of GUI stuff. I'd like to
be able (eventually) to send a  message to the main windows code whenever
certain actions take place in the Python GUI. I'd also like to be able to
have Python create a window, and pass a handle to this window back to the
main windows code to have windows draw into this window. But, really, all of
these problems are secondary...
    The main problem I am having right now is just getting the dang Python
code to run in the first place! :)  I must admit, that my Python skills are
poor at best. But, I have created a python program, that I can run from the
command line (ie. python.exe test.py) which creates a little window, lets me
press some buttons, and then close it. I've been trying to have my windows
code call this python code, but for some reason it is not working. The
module loads fine, the python function loads fine, but then when I try to
call the function, nothing happens. A snipet of code follows:

void Python_thread(PVOID pvoid)
    volatile P_THREAD_PARAMS pparams; // pointer to the parameters passed
into the thread
    PyObject *p_module,    // python objects used to start python
    int status, result;
    char *c_str;      // result returned when python exits

    pparams = (P_THREAD_PARAMS) pvoid;


    p_module = PyImport_ImportModule("P_Menu");
    if(p_module == NULL)
        _RPT0(_CRT_ERROR, "\nPython module P_Menu was not loaded.\n");

    p_dictionary = PyModule_GetDict(p_module);
    if(p_dictionary == NULL)
        _RPT0(_CRT_ERROR, "\nPython dictionary was not loaded.\n");

    p_function = PyObject_GetAttrString(p_module, "start_menu");
    if(p_function == NULL)
        _RPT0(_CRT_ERROR, "\nPython function start_menu was not loaded.\n");

    // this calls the function start_menu in the P_Menu module
    //_RPT0(_CRT_ERROR, "\n Python start_menu is now being called.\n\n");
    p_result = PyEval_CallObject(p_function, NULL);
    // p_result = PyRun_String("P_Menu.start_menu()", eval_input,
p_dictionary, p_dictionary);

    // etc....

    What am I doing wrong here? I don't get any errors loading the module or
the function, but then when I call PyEval_CallObject, nothing happens. The
python function I am calling does not take any arguments, it's just creating
a simple window... Here is the Python code:

def kill_python():
 print 'kill python'

def kill_draw():
 print 'kill draw'

def start_menu():
 from Tkinter import *
 win = Frame(None)
 Label(win, text='Python menu').pack(side=TOP)
 Button(win, text='Kill python', command=kill_python).pack(side=LEFT)
 Button(win, text='Kill draw', command=kill_draw).pack(side=RIGHT)

    Another strange problem, which I am not quite sure if it is related, is
that if I move the "from Tkinter import *" to before all the def's then the
module will not load at all from the win32 code. ie. when I call
PyImport_ImportModule("P_Menu"), it returns null. But I can still load the
module perfectly find from the Python command line by doing an "import
P_Menu" and then "start_menu()".
    I'm really not sure if I am doing something wrong in my C code, or my
Python code. Any help would be greatly appreciated. Because before I start
spending my time creating my Python GUI, I'd like to make sure I'll be able
to use it from my C code! :)
    Also, is there some way to create a window using Python, and then have
win32 code draw into that window? (and/or X/Windows code, since I will
eventually be tryng to create a program that will run under both
environments). Thanks to anyone who might be able to help me out...

    --David Sheets

More information about the Python-list mailing list