Using IronPython Constructively ❘
115 The problem occurs with the second part of the example. The process is relatively straightforward. The
TestIDLE
class contains a method called
ShowAbout()
.
ShowAbout()
creates a window and then dis-
plays the IDLE About dialog box in it. The code then destroys the window it created and ends.
When you try to run this example using IronPython by typing IPY IDLETest.py at the command
prompt and pressing Enter, the first message you see says that IronPython knows nothing about a
Tkinter
module, which provides access to the windowing environment provided by
Tk
. You can
read all about
Tkinter
at
http://www.pythonware.com/library/tkinter/introduction/
. The
bottom line is that
Tkinter
is a windowing environment that many Python developers use, so you’re
going to run into it when working with IronPython.
At this point, you notice that IronPython does indeed include support for
Tkinter
in the
\Program Files\IronPython 2.6\Lib\lib-tk
folder. So you add this directory to your
IRONPYTHONPATH environment variable by typing
Set IRONPYTHONPATH=%IRONPYTHONPATH
%;C:\Program Files\IronPython 2.6\Lib\lib-tk
and pressing Enter. This command adds the
required directory to your application environment, so you type
IPY IDLETest.py
again and press
Enter. Now you see an error message, “ImportError: No module named _tkinter.”
Unfortunately, you’ll never find a
_tkinter
module in the IronPython folders. You’ll find it in the
\Python26\DLLs
directory as
_tkinter.pyd
and in the
\Python26\libs
directory as
_tkinter
.lib
. The alarms should be going off in your head at this moment. Python implements
Tkinter
as a
C library, which means that IronPython doesn’t support it, despite the fact that IronPython includes
the required
Tkinter.py
file. The point of this whole exercise is that you’re going to run into some
very popular Python features that simply won’t work in IronPython because they require C support
(despite the fact that it appears that IronPython does support it). Your only choice (in most cases) is
to avoid using the module or rely on straight Python instead. Happily, some developers are working
on the
Tkinter
problem. You can read about one such solution at
http://www.voidspace.org
.uk/ironpython/cpython_extensions.shtml
.
Whenever you have a question about IronPython support for a particular Python feature, check the not-supported-module-list at http://ironpython
.codeplex.com/wikipage?title=List of Standard Library components
not included in the MSI
.