473,654 Members | 3,084 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 3500
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
7022
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
3847
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
2380
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
1520
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
2562
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
4979
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
979
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
3280
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
8290
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
8815
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
8708
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...
1
8489
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4149
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...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
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
2
1596
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.