Trouble Importing Tkinter in embedded C program

Discussion in 'Python' started by Rick Olson, Nov 18, 2003.

  1. Rick Olson

    Rick Olson Guest

    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
     
    Rick Olson, Nov 18, 2003
    #1
    1. Advertising

  2. Rick Olson

    David Boddie Guest

    Rick Olson wrote:

    > 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).


    [...]

    Sounds like a fairly familiar issue with compiled extensions, but I could
    be wrong.

    > 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/


    [...]

    > 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.


    If you are able to change the conditions under which the C program is
    built (paths, libraries, etc.) then you may want to link against the
    _tkinter.so library in the /usr/lib/python2.2/lib-dynload directory.

    That's just a guess, however. Maybe this is answered in an FAQ somewhere.

    --
    http://www.boddie.org.uk/david/Projects/
     
    David Boddie, Nov 18, 2003
    #2
    1. Advertising

  3. Rick Olson

    Rick Olson Guest

    David Boddie <-and.ac.uk> wrote in message news:<bpe51c$odr$>...
    > > 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.

    >
    > If you are able to change the conditions under which the C program is
    > built (paths, libraries, etc.) then you may want to link against the
    > _tkinter.so library in the /usr/lib/python2.2/lib-dynload directory.
    >
    > That's just a guess, however. Maybe this is answered in an FAQ somewhere.


    Thanks for the suggestion.

    I tried to check the FAQ and newsgroup, but was unsuccessful. I may
    well have missed it. I'm not a serious programmer and am frequently
    unclear on exactly how to compile, link...

    Nevertheless, I tried linking _tkinter.so by changing the makefile a
    few different ways all of which amount to the makefile below. FWIW if
    I drop the definition of DLL this makefile will compile and run the
    examples in Extending and Embedding the Python Interpreter.

    Thanks again--
    Rick



    CC = gcc

    PY = /usr/include/python2.2
    PYLIB = /usr/lib/python2.2/config/libpython2.2.a
    PYINC = -I/usr/include/python2.2 -I/usr/include

    # Compiler flags
    OPT= -g
    CFLAGS= $(OPT)

    LIBS= -L/usr/lib \
    -L/usr/X11R6/lib \
    -ltk8.4 -ltcl8.4 -lnsl -lX11 -ldl -lreadline -lieee -lpthread -lutil
    LDFLAGS=-Xlinker -export-dynamic
    SYSLIBS=-lm
    DLLS= /usr/lib/python2.2/lib-dynload/-tkinter.so
    ALLLIBS=$(PYLIB) $(LIBS) $(SYSLIBS)

    testtk: testtk.o
    $(CC) $(LDFLAGS) testtk.o $(DLLS) $(ALLLIBS) $(OPT) -o testtk

    testtk.o: testtk.c
    $(CC) testtk.c -c $(OPT) $(PYINC)
     
    Rick Olson, Nov 19, 2003
    #3
  4. Rick Olson

    David Boddie Guest

    (Rick Olson) wrote in message news:<>...

    > Thanks for the suggestion.


    No problem.

    > I tried to check the FAQ and newsgroup, but was unsuccessful. I may
    > well have missed it. I'm not a serious programmer and am frequently
    > unclear on exactly how to compile, link...


    Sorry, it was more of a general "wondering out loud" comment, not a request
    for you to do more work. ;-)

    > Nevertheless, I tried linking _tkinter.so by changing the makefile a
    > few different ways all of which amount to the makefile below. FWIW if
    > I drop the definition of DLL this makefile will compile and run the
    > examples in Extending and Embedding the Python Interpreter.


    I'll work through this in pieces and try to see what could be causing the
    problems:

    > CC = gcc
    >
    > PY = /usr/include/python2.2
    > PYLIB = /usr/lib/python2.2/config/libpython2.2.a


    You could also try /usr/lib/libpython2.2.so if it exists, but I don't think that
    it will make much difference.

    > PYINC = -I/usr/include/python2.2 -I/usr/include
    >
    > # Compiler flags
    > OPT= -g
    > CFLAGS= $(OPT)
    >
    > LIBS= -L/usr/lib \
    > -L/usr/X11R6/lib \
    > -ltk8.4 -ltcl8.4 -lnsl -lX11 -ldl -lreadline -lieee -lpthread -lutil


    You probably won't need the -ltk8.4 -ltcl8.4 and -lX11 libraries to be included
    explicitly unless your main application requires them.

    > LDFLAGS=-Xlinker -export-dynamic
    > SYSLIBS=-lm
    > DLLS= /usr/lib/python2.2/lib-dynload/-tkinter.so


    I'm sure you meant _tkinter.so at the end, there. ;-)

    > ALLLIBS=$(PYLIB) $(LIBS) $(SYSLIBS)
    >
    > testtk: testtk.o
    > $(CC) $(LDFLAGS) testtk.o $(DLLS) $(ALLLIBS) $(OPT) -o testtk
    >
    > testtk.o: testtk.c
    > $(CC) testtk.c -c $(OPT) $(PYINC)


    I quickly put together an test program which embeds a TCL/Tk enabled Python
    interpreter. Noting that I'm using Python 2.3, installed in /usr/local, my Makefile
    reads like this (I apologise in advance to any Makefile experts reading this):

    PYVERSION = 2.3
    PREFIX = /usr/local
    PYHOME = $(PREFIX)/lib/python$(PYVERSION)
    PYINCLUDE = -I$(PREFIX)/include/python$(PYVERSION)
    PYLIB = -L$(PREFIX)/lib/libpython$(PYVERSION).so \
    -lpython$(PYVERSION) \
    -ldl -lpthread -lutil -lm \
    $(PREFIX)/lib/python$(PYVERSION)/lib-dynload/_tkinter.so

    default: all

    %.o: %.c
    gcc -c -fPIC $(PYINCLUDE) $<

    all: main

    main: main.o
    gcc $< $(PYLIB) -o main

    clean:
    rm -f *~ *.o *.so core core.* main


    Of course, some of this changes depending on your system's configuration
    but, if you still have problems after applying some of the above ideas, then
    I'll send you the example to look at.

    Have fun,

    David
     
    David Boddie, Nov 21, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    1,377
    Mahesh
    Mar 24, 2006
  2. Vio
    Replies:
    1
    Views:
    321
    Miki Tebeka
    Jun 10, 2004
  3. plb
    Replies:
    2
    Views:
    360
  4. Marcin Krol

    Importing modules in embedded Python

    Marcin Krol, Jul 2, 2008, in forum: Python
    Replies:
    0
    Views:
    292
    Marcin Krol
    Jul 2, 2008
  5. snakeinmyboot
    Replies:
    8
    Views:
    114
    David
    Aug 9, 2013
Loading...

Share This Page