LablTk: basics

Click on one of the links below: a popup window will appear containing a small ml programme.
Save this programme somewhere and feed to labltk.
If labltk was properly installed on your machine, you should see the window reproduced on the right of the programme in this page.

Create a window
First of all, you have to start an application by creating the root window,
to which every widget in your application will be attached:
let top = openTk ()
 
Opening systematically module Tk at the beginning will avoid to have to prefix many names with "Tk.".
Function mainLoop is the main interaction loop, which of course does nothing in this elementary example.
Using Printexec.print allows to see any exception that might be raised by your application.


Create a button in a window
Button b must be placed somewhere
inside its parent window top; we can use pack to do so:
pack [b]
       
Note how we attach an action to clicking the button through the command argument. Here this action will cause exiting the application.


Bind actions to key strokes
When a key is pressed, it is echoed to the standard output, together with the code of the character.
Now here is the way to bind an action to the pressing of "Control-q". The action is to exit from the application.


Create a label in a window  


Create a button and a label together

The widgets of the button and the label have different types,
and yet must belong to the same list.
            
It is therefore necessary to call a coercion function to cast them into a common type:
pack [Tk.coe l; Tk.coe b]
or, since we opened module Tk at the beginning of the file:
pack [coe l; coe b]


An entry widget and an exit button

You can type text into an Entry widget.
         


A label, an entry, and an exit button
A "Textvariable" serves for communicating information
as a string to and from Tk widgets:
let v = Textvariable.create () ;;
        
This Textvariable is initialized to the empty string:
Textvariable.set v "" ;;
We specify here that Label l should display the contents of the Textvariable v:
let l = Label.create ~textvariable:v top
On the other hand, the text entered into the Entry n is bound the the same Textvariable v:
let n = Entry.create ~width:10 ~relief:`Sunken ~textvariable:v top
Thus the text you type in Entry n will appear at once in the Label l.
Note that on clicking the quit button, the text you have entered is displayed on standard output before leaving.


How to retrieve the text from an Entry widget?
Nearly the same example as before, except that when you click
on button b ("Echo the text"),
the command that is bound to b specifies that v,
i.e. the stuff to be displayed on l,
is to be updated using whatever you entered into n.
    


            
After entering some text   After echoing the text

Note that we have to "coe" the different widgets in order to pack them in the window.
You retrieve the texte entered into an Entry widget with Entry.get.
Clearly what you want to do with the data which are entered in an Entry, or specified through Check or Radio buttons, is off topic in the presents notes; it is the concern of your own application. As a consequence the only use we shall make here of such data will be to print them on standard output.

On my Mac (Tiger), I observed that Entry.get returns a string coded in UTF-8.
If you are sure that your text contains only latin1 characters, you can use the following recept to convert to a new chain in ISO-8859-1:


How to retrieve the state of a check button?  


State of a check button


How to retrieve the state of a radio button?  


State of a radio button             After making a choice:



François Thomasset -- INRIA, Rocquencourt -- November 2007
Email: Francois dot Thomasset at inria dot fr