dhruvvyas90
dhruvvyas90

Reputation: 1205

Internal working of Tkinter after method

I'm working Tkinter GUI for quite sometime. And I was wondering, how after method actually works on it. My questions are very simple.

  1. How after method is implemented internally in Tkinter ?
  2. Are after calls threaded ?
  3. What are the cons (if any) of multiple after calls in Tkinter ?
  4. How are after callbacks scheduled ?

I did go through a couple of links like this. But couldn't get these informations.

Upvotes: 2

Views: 487

Answers (2)

Bryan Oakley
Bryan Oakley

Reputation: 385930

It's really quite simple: Tkinter maintains a queue of work to be done (the event queue), and mainloop (as well as update) processes that queue. after merely adds a timestamped event to the queue. No threads are involved.

Upvotes: 0

taleinat
taleinat

Reputation: 8701

  1. after() calls Tck/Tk's after command, which registers a callback to be called later by the Tcl/Tk event loop.

  2. after() calls are not threaded.

  3. One issue is that a blocking or long-running operation may cause something scheduled with after() to run significantly later than you meant. There are many more possible pros and cons which may be relevant, but you'll need to specify your concerns or use cases...

  4. A callback is registered into Tcl/Tk event loop, which takes care of the scheduling. For details on how that works, see the documentation.

Explanation:

As you may already know, Tkinter uses Tcl/Tk internally.

Taking a look at the code for the after() method, it appears that Tkinter is just calling the Tcl/Tk after command.

The documentation for Tcl/Tk's after command doesn't mention anything about threads. This makes sense given Tcl/Tk's single-threaded event loop design.

So, my conclusion is that Tkinter's after() method does not use threads, rather it just registers a callback using the internal Tcl/Tk after command, which will be called at the most appropriate time possible by the Tcl/Tk event loop.

Upvotes: 3

Related Questions