Trouble Importing Tkinter in embedded C program
Rick Olson
r_olson at sandiego.edu
Tue Nov 18 15:52:38 EST 2003
I'm trying to add a Tkinter interface to an existing C program with
embedded python, but seem to have trouble importing Tkinter (or
accessing it). I tried a simple program that would run the script
"hello.py"
This is hello.py:
from Tkinter import *
mainWindow=Tk()
label=Label(mainWindow, text="Hello")
label.grid(row=0,column=0)
mainloop()
My first attempt at embedding this in C was:
#include <Python.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
Py_Initialize();
PySys_SetPath(".");
FILE *fp=fopen("hello.py","r++");
PyRun_SimpleFile(fp,"hello.py");
Py_Finalize();
}
On execution I was told:
Traceback (most recent call last):
File "hello.py", line 1, in ?
from Tkinter import *
ImportError: No module named Tkinter
Using Py_GetPath reported:
/usr/lib/python2.2/:/usr/lib/python2.2/plat-linux2:/usr/lib/python2.2/lib-tk:
/usr/lib/python2.2/lib-dynload
On my SuSE 8.2 installation Tkinter.py is in usr/lib/python2.2/lib-tk/
So I tried to explicitly load the Tkinter module in the program by
inserting the following right after the SetPath:
PyObject *importModule = PyImport_ImportModule("Tkinter");
if (importModule == NULL)
{ printf(" Tkinter import unsuccessful\n");
return 1;
}
When I run this, importModule is coming back as NULL.
Can someone guide me in the right direction to allow calls to Tkinter
through the embedded python? I know there are ways of using Tk/Tcl
through C, but I'm not familiar enough with Tk/Tcl to try that.
Thanks in advance--
Rick Olson
More information about the Python-list
mailing list