10.41.3.7 Miscellaneous

There are a couple of other Tk commands that we ought to mention: destroy and update.

The destroy command is used to destroy a widget, i.e. remove it from the Tk interpreter entirely and so from the display. Any children that the widget may have are also destroy-ed. Anything connected to the destroyed widget, such as bindings, are also cleaned up automatically.

For example, to create a window containing a button that is destroyed when the button is pressed:

     button .b -text "Die!" -command { destroy . }
     pack .b

creates a button .b displaying the text ‘Die!’, which runs the command destroy . when it is pressed. Because the widget . is the main toplevel widget or window, running that command will kill the entire application associated with that button.

The command update is used to process any pending Tk events. An event is not just such things as moving the mouse but also updating the display for newly created and displayed widgets. This may be necessary in that usually Tk draws widgets only when it is idle. Using the update command forces Tk to stop and handle any outstanding events including updating the display to its actually current state, i.e. flushing out the pending display of any widgets. (This is analogous to the fflush command in C that flushes writes on a stream to disk. In Tk displaying of widgets is “buffered”; calling the update command flushes the buffer.)


Send feedback on this subject.