Main Content

Keeping you Tkinter display up to date while monitoring

Archive - Originally posted on "The Horse's Mouth" - 2014-04-06 10:40:21 - Graham Ellis

Have you ever updated a web page just to find that something changes and it needs to be updated again? Have you every tidied the kitchen just to find it messy again within a few minutes? Are there time when it's sensible to say "we'll leave that until ..."?

It's the same when you're programming a Graphic User Interface and changing the display. Multiple changes - perhaps dozens of them - are to be done in quick succession, and it's going to be inefficient (and distracting to the viewer) to see the changes flashing up with lots of intermediate resizes and colour changes. So graphic user interfaces buffer up the changes and apply them all at once - and by default, that application of the changes is when all the tasks to be done have been completed, all incoming events handled, and the program's about to go back into an idle state waiting for the next user event.

Most of the times, this approach is spot on. Changes are stored internally and applied, and the changing of the screen provides a very clear signal to the user that it's time to interact again. Mentally, it's spot on. But that's most of the time, and just occasionally you may want an intermediate update. Take for example a GUI that's monitoring an ongoing process; you'll want to update the display even if no user input is expected should the monitoring reveal something's changed. So you can force an update using update_idletasks (that's the TkInter name) or update idletasks (Tk). Here's an example of a Python / Tkinter program that uses this technique - it (a) does an update, (b) sleeps for 2 seconds and (c) does another update. With the update_idleatsks() I have used, the changes appear with a gap of 2 seconds between them; without that line, nothing happs for 2 seconds after pressing the button then both changes appear.

  #!/usr/bin/env python
   
  import Tkinter as tk
  import time
   
  top = tk.Tk()
   
  def addText():
    # make first change
    oldText = L.cget("text")
    newText = oldText + '\nfirst change'
    L.configure(text=newText)
   
    # wait 2 seconds
    top.update_idletasks()
    time.sleep(2)
   
    # make second change
    newText += '\nsecond change'
    L.configure(text=newText)
   
  B = tk.Button(top, text ="Change text", command = addText)
  L = tk.Label(top,text='orignal text')
   
  B.pack()
  L.pack()
  top.mainloop()


Many thanks to my correspondent for triggering this question / post - I've modified his code to show how to change the GUI behaviour. Readers may have noticed a reduction in technical answers such as this one of late - partly because there's so much going on, and partly because we already have a huge resource of answers and can point people to them when asked about things. Just occasionally, I'm still posting new descriptions of issues such as this one to keep them fresh, modern and perhaps to give better answers than I have in the past