Embedding problem

Cliff Penton cliff.penton at b_i_g_f_o_o_t.c_o_m
Wed Oct 18 18:22:07 EDT 2000


I looking at Python as a possible language for embedding in an application I
am working on.  Unfortunately I don't seem to be able to get a very simple
script to work the same when embedded and when run within Python.  To help
illustrate my problem I have included a simple example, consisting of the
following bits:

embed.c         is a simple program that executes the hello()
                function in the embed.py script.
embed.py        is a simple script, using Tk, that displays a
                window with a button in it
embed.traceback is the result when running hello() inside
                embed.c

If I try:

> python embed.py

everything works fine, if i try:

> embed

I get the traceback guff detailed below.

What am I doing wrong?

I am using Python 2.0 release.

Help!

Cliff


--- embed.c starts ---

#include <Python.h>

int main(int argc, char** argv)
{
   PyObject* module = 0;
   PyObject* func   = 0;
   PyObject* args   = 0;
   PyObject* rc     = 0;

   Py_SetProgramName(argv[0]);
   Py_Initialize();
   PySys_SetArgv(argc, argv);
   printf("Welcome to a brave new world\n\n");

   /* Initialise the Python module and function */
   module = PyImport_ImportModule("embed");
   if (module == NULL) {
      PyErr_Clear();
      printf("Unable to import embed module\n");
      goto bye;
   }

   func = PyObject_GetAttrString(module, "hello");
   if (func == NULL) {
      PyErr_Clear();
      printf("Unable to bind to hello function in detect module\n");
      goto bye;
   }

   rc = PyEval_CallObject(func, NULL);
   if (rc == NULL) {
      PyErr_Print();
   } else {
      Py_DECREF(rc);
   }
bye:
   Py_XDECREF(func);
   Py_XDECREF(module);
   Py_Exit(0);
}

--- embed.c ends ---

--- embed.py starts ---

from Tkinter import *

def hello():
   ok = Button(None, text='Ok')
   ok.pack()
   ok.mainloop()

if __name__ == '__main__':
   hello()

--- embed.py ends ---

--- embed.traceback starts ---

Traceback (most recent call last):
  File "embed.py", line 4, in hello
    ok = Button(None, text='Ok')
  File "e:\program files\python20\lib\lib-tk\Tkinter.py", line 1819, in
__init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "e:\program files\python20\lib\lib-tk\Tkinter.py", line 1752, in
__init__
    BaseWidget._setup(self, master, cnf)
  File "e:\program files\python20\lib\lib-tk\Tkinter.py", line 1727, in
_setup
    _default_root = Tk()
  File "e:\program files\python20\lib\lib-tk\Tkinter.py", line 1482, in
__init__
    self.tk = _tkinter.create(screenName, baseName, className)
TclError: Can't find a usable init.tcl in the following directories:
    {} {e:/program files/python20/lib/tcl8.3} E:/Home/embed/lib/tcl8.3
E:/Home/lib/tcl8.3 E:/Home/lib/tcl8.3/library E:/Home/library
E:/Home/../tcl8.3/library E:/../tcl8.3/library



This probably means that Tcl wasn't installed properly.

--- embed traceback.ends ---






More information about the Python-list mailing list