A tkinter kick-off

Michael P. Reilly arcege at shore.net
Wed Jun 2 08:15:43 EDT 1999


Mark Butterworth <mrkbutty at mcmail.com> wrote:
: I've been using python for a few months now (I've actually managed to
: persuade my company that freeware is a viable tool) and I've just started
: looking at tkinter.  However,  my first little project doesn't  require an
: interactive window or dialog box.  What I do require is a window which
: displays the result's of my background processing.  The processing will
: simply be the compression and network transfer of some files.  I'm not
: looking for a full solution however, I just need a help starting off.  I can
: create a window containing a listbox alright, I just need to know how to
: process in the background and display results within the listbox (maybe a
: cancel button would also be useful).

Most windowing applications over the last ten to fifteen years have been
based on the event loop, and Tkinter is not exception.  But (of course
there is a but), Tkinter also has the nice feature of including various
other control mechanisms.

In this case I think you would want to be using the update_idletasks()
or update() methods.

  root = Tk()
  done = 0
  # function to turn off computations
  def canceler():
    global done
    done = 1
  # set up your widgets using root
  cancel = Button(root, text="Cancel", command=canceler)
  cancel.pack(side=BOTTOM)
  root.update_idletasks()
  while not done:
    # perform your computations, including updating the widgets
    root.update_idletasks()

You might also want to look into the after_idle() method.  The choice
to use after_idle or update_idletasks should be based on which is going
to take more time (and which is going to block for longer), the
computations/networking or the windowing.  You might also want to look
into threads (just made sure that Tkinter is controlled in the main
thread).

FYI, Curses uses this "idletasks" method (called refresh).

  -Arcege





More information about the Python-list mailing list