473,597 Members | 2,699 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble Importing Tkinter in embedded C program

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(mai nWindow, 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("hell o.py","r++");
PyRun_SimpleFil e(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_Import Module("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
Jul 18 '05 #1
2 3493
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/
Jul 18 '05 #2
David Boddie <da****@dcs.s t-and.ac.uk> wrote in message news:<bp******* ***@hercules.bt internet.com>.. .
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)
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7014
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter import * class App:
1
3842
by: corrado | last post by:
Hello I have an application running several thread to display some financial data; basically I have a thread displaying HTML tables by means of Tkhtml, another implementing a scrolling ticker based on a Text widget with embedded windows and a thread running the Tkinter mainloop plus several other thread dealing with the scheduling of the contents and the acquisition of data but not using graphic widgets. I run the same code on Linux...
12
2376
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In some cases there is a further complication: module importing through an indirect mechanism, like: exec "from " + xxx + " import *". A part the fact that I have not understood the "real" difference between import and from ... import (or also from......
2
1519
by: vduber6er | last post by:
First off this is in unix. I have a C file that has python embedded in it. In the script I have the following PyRun_SimpleString(code); where code = "import mymodule"
6
2561
by: gmarkowsky | last post by:
Hi all, I'm trying to import a class from a module. The class looks like this: class App: def __init__(self, master): frame = Frame(master) frame.pack()
44
4973
by: bg_ie | last post by:
Hi, I'm in the process of writing some code and noticed a strange problem while doing so. I'm working with PythonWin 210 built for Python 2.5. I noticed the problem for the last py file processed by this script, where the concerned tmp file is only actually written to when PythonWin is closed. In other words, after I run this script, one of the generated tmp files has a size of 0kB. I then close PythonWin and it is then written to.
0
978
by: Alexnb | last post by:
I am writing a program in which there is a textbox and in it the program auto inputs a series of words, and definitions for each word. I want to make it formatted nicely to where the word is indented, and the definitions indented even more. I don't even know how to make the margins work. I have read all the documentation but just need an example so that will help. But my bigger issue is that the way I have written it is like this: for...
8
3273
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to my pc. But, it is not getting installed and is failing by throwing the below errors. Should i need to configure / install any specific files for resolving this issue ?
0
1827
by: lee.walczak | last post by:
I actually post a topic relating to my problem here: (http://groups.google.co.uk/group/comp.lang.python/browse_thread/ thread/a073d532c4481bfe?hl=en# ) But I thought it could be useful to place an example of my problem here aswell. This a small piece of testcode that creates a TableList. When the cell is selected, a ComboBox Widget is used temporarily during the editing phase. It is during this ( EditStartCmd call) that I wish to...
0
7977
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7894
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8261
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6705
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3893
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2409
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1496
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.