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 () |
![]() |
Tk
at the beginning will avoid to have to prefix
many names with "Tk.
".mainLoop
is the main interaction loop, which of course
does nothing in this elementary example.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 somewhereinside its parent window top ;
we can use pack to do so:pack [b]
|
![]() |
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. |
![]() |
pack [Tk.coe l; Tk.coe b]
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 () ;; |
![]() |
Textvariable.set v "" ;;
l
should display the
contents of the Textvariable v
:let l = Label.create ~textvariable:v top
n
is bound the
the same Textvariable v
:let n = Entry.create ~width:10 ~relief:`Sunken ~textvariable:v top
n
will appear at once in the Label l
.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 |
How to retrieve the state of a check button? | ![]() |
How to retrieve the state of a radio button? | ![]() |
State of a radio button | ![]() |
After making a choice: | ![]() |
Francois dot Thomasset at inria dot fr