8.1 Dialogs

A dialog displays some graphical information and several buttons. A simple abstraction to build dialogs is provided by the class TkTools.dialog.

Figure 8.1 shows an example dialog for deleting a file. The class TkTools.dialog is a subclass of Tk.frame. Creating and initializing an instance of this class creates a toplevel widget together with buttons displayed at the bottom of the toplevel widget. The instance itself serves as container for user-defined widgets that are displayed at the top of the dialog (as the label and the entry widget in our example).


D={New TkTools.dialog
   tkInit(title:   'Remove File' 
          buttons: ['Okay' # 
                    proc {$}
                       try 
                          {OS.unlink {E tkReturn(get $)}}
                          {D tkClose}
                       catch _ then skip 
                       end 
                    end 
                    'Cancel' # tkClose]
          default: 1)}
L={New Tk.label tkInit(parent:D text:'File name:')}
E={New Tk.entry tkInit(parent:D bg:wheat width:20)}
{Tk.batch [pack(L E side:left pady:2#m) focus(E)]}

Figure 8.1: A dialog to remove files.


The initialization message for a dialog must contain the title option, which gives the title of the dialog. The buttons to be displayed are specified by a list of pairs, where the first pair in the list describes the leftmost button. The pair consists of the label of the button and the action for the button. The action can be also the atom tkClose, which means that the action of the button sends a tkClose message to the dialog. In a similar manner, the action can be a unary tuple with label tkClose, which means that first the action specified by the tuple's argument is executed and then the dialog is closed. The default option specifies which button should be the default one. The default button is marked by a sunken frame drawn around the button.

In the above example, pressing the 'Okay' button executes an rm command to remove the file with the name as given by the entry widget E. Only if execution of this command returns 0, the dialog is closed.

The class TkTools.dialog is a subclass of Tk.frame. In particular it allows to wait until the dialog object gets closed. For example, the execution of

{Wait D.tkClosed}

blocks until the dialog in the above example is closed.


Christian Schulte
Version 1.4.0 (20080702)