[Python-Dev] Event loops, PyOS_InputHook, and Tkinter

Mark Hammond mhammond at skippinet.com.au
Thu Nov 10 22:49:20 CET 2005

Michiel wrote:
> Guido van Rossum wrote:
> >On 11/9/05, Michiel Jan Laurens de Hoon
> <mdehoon at c2b2.columbia.edu> wrote:
> >
> >
> >>My application doesn't need a toolkit at all. My problem is that because
> >>of Tkinter being the standard Python toolkit, we cannot have a decent
> >>event loop in Python. So this is the disadvantage I see in Tkinter.
> >>
> >>
> >
> >That's a non-sequitur if I ever saw one. Who gave you that idea?
> There is no connection.
> >
> I have come to this conclusion after several years of maintaining
> a scientific plotting package and trying to set up an event loop
> for it. Whereas there are some solutions that more or less work,
> none of them work very well, and the solutions that we found tend
> to break. Other visualization packages are struggling with the
> same problem. I'm trying the best I can to explain in my other
> posts why I feel that Tkinter is the underlying reason, and why
> it would be difficult to solve.

I believe this problem all boils down to this paragraph from the first mail
on this topic:

: Currently, event loops are available in Python via PyOS_InputHook, a
: pointer to a user-defined function that is called when Python is idle
: (waiting for user input). However, an event loop using PyOS_InputHook
: has some inherent limitations, so I am thinking about how to improve
: event loop support in Python.

Either we have an unusual definition of "event loop" (as many many other
toolkits have implemented event loops without PyOS_InputHook), or the
requirement is for an event loop that plays nicely with the "interactive
loop" in Python.exe.

Assuming the latter, I would suggest simply not trying to do that!  Look at
the "code" module for a way you can create your own interactive loop that
plays nicely with your event loop (rather than trying to do it the other way

Otherwise, I suggest you get very specific about what this event loop should
do.  From a previous mail in this thread (an exchange between you and

> >> Which is why Tkinter needs to be in charge of the event loop. For
> >> other GUI toolkits, I don't see a reason why they'd need their own
> >> event loop.
> >
> > They need to fetch events from the operating system level, and dispatch
> > them to the widgets.

> This is a perfect task for an event loop located in Python, instead of
> in an extension module.

I believe the point Martin was trying to make is that we have 2 "unknown"
quantities here - the "operating system" and the "widgets".  Each OS
delivers raw GUI events differently, and each GUI framework consumes and
generates events differently.  I can't see what a single event loop would
look like.  Even on Windows there is no single, standard "event loop"
construct - MFC and VB apps both have custom message loops.  Mozilla XUL
applications (which are very close to being able to be written in Python
<wink>) have an event loop that could not possibly be expressed in Python -
but they do expose a way to *call* their standard event loop (which is quite
a different thing - you are asking to *implement* it.)

> I could write a prototype event loop for Python
> to demonstrate how this would work.

I think that would be the best way forward - this may all simply be one big
misunderstanding <wink>.  The next step after that would be to find even one
person who currently uses an event-loop based app, and for whom your event
loop would work.


More information about the Python-Dev mailing list