Tkinter doesn't work for MFC applications with Embedded Python?

Georg Mischler schorsch at schorsch.com
Sat Jul 7 08:30:57 EDT 2001


Chris Tavares wrote:

> "Deepak" wrote:
>> Hi,
>> I tried to go through all the related postings on the newsgroup
>> regarding this.
>> However, I haven't figured out a way to get Tkinter working in an MFC
>> application that uses embedded python interpreter... Here is the
>> simple code
> 
> [... snip ...]
> 
> Tkinter and MFC don't mix in the same process. Neither will Tkinter and
> WxWindows, or MFC and WxWindows, or Tkinter and GTK+, or any other gui
> toolkits.
> 
> The basic problem is that they all want control of the message loop - and
> the first one there gets it. This is also the reason you can't run Tkinter
> apps under PythonWin.
> 
> It may be possible to get this to work, but not without hacking on Tk's C
> source code.
 
In fact, it is quite easy to get this to work, albeit with a few
limitations. What would be harder to implement, is to allow
nonmodal interaction with both kinds of windows in parallel. 

If you just want to open a modal Tk dialog or two from an MFC app,
and continue with the rest of the program after that dialog is
closed, then there should be no problem at all. One example that
easily allows this is my Pyacad:
  <http://starship.python.net/crew/schorsch/py_acad/>

Based on Pyacad, I develop and sell Rayfront:
  <http://www.schorsch.com/rayfront/>

Rayfront is a standalone Tkinter application in itself, but
can be run within Autocad/Intellicad as an extension to those.

If you want to open nonmodal Tk windows within a MFC app, then
a little more work will be required. You could create a method
for your MFC application object that simulates the work of the
Tkinter event loop, and hook this up to the OnIdle MFC event (or
something to that effect). It may be tricky to get that right,
but I'm pretty sure it can be done without any advanced magic,
especially you're more familiar with MFC than I am. In the worst
case, you'd have to rewrite and/or extend the MFC event loop.

I don't expect that any Tk source hacking would be required, as
the Tkinter event loop is implemented in plain Python. The other
question is of course, why you would want to use MFC at all, once
you have Tk/Tkinter at your disposal... ;)

>> try:
>>    import Tkinter
>>    print 'Tkinter successfully imported'
>>    try:
>>       root = Tkinter.Tk ()
>>       print 'Successful'
>>    except:
>>       print 'failed'
>> except:
>>    print 'Could not import Tkinter'

Your catch-all "except:" clauses are not very helpful.
If you have no idea about what types of exceptions to expect,
then you should have a look at the traceback module, and replace
your simple error messages with something like this:

  import traceback
  tb_strs = traceback.format_exception(
        sys.exc_type,
        sys.exc_value,
        sys.exc_traceback)
  print 'failed to create root window: %s' % string.join(tb_strs))

This will then tell you *why* the Tkinter.Tk() call didn't succeed,
which may be anything from a missing DLL to missing %TK_LIBRARY%
and %TCL_LIBRARY% environment variables.
Whatever the reason, it will have nothing (directly) to do with MFC.


Have fun!

-schorsch

-- 
Georg Mischler  --  simulations developer  --  schorsch at schorsch.com
+schorsch.com+  --  Lighting Design Tools  --  http://www.schorsch.com/



More information about the Python-list mailing list