Tk (toolkit)

from Wikipedia, the free encyclopedia

Tcl-Tk universal scripting.svg
Basic data

Maintainer TCL core team
developer John Ousterhout
Current  version 8.6.10
operating system cross-platform
programming language C.
category GUI toolkit
License BSD license
German speaking No

Tk is a free , cross-platform GUI toolkit for programming of graphical user interfaces (GUIs).


Tk was developed by John Ousterhout since 1988 as an extension for the script language Tcl , but since he only worked on the software on the side, it was only published in 1991.

Tk was written primarily for Unix X11 , but it is now cross-platform compatible and can therefore also run under MacOS and Windows . Due to the widespread use of X11 systems at the time, primarily at universities, the software quickly became known and used by a large number of developers because it was easier to use than existing libraries.

In 1994 John Ousterhout went to Sun Labs , where he put together a team to work on Tcl / Tk. During this time the software spread and in mid-1994 the first version of the Python library Tkinter appeared , shortly afterwards PerlTk was published.

Widgets that were frequently used in applications (e.g. trees, combo boxes, tabbed notebooks) were not available in the Tk core, but only via several, often competing add-ons. This was changed in a version of Tk (8.5.0) released on December 20, 2007, with the introduction of the so-called themed widget set . This meant that you could create applications without major tweaks or add-ons.


The common combination of Tcl (Tool Command Language) and the GUI toolkit Tk is referred to as Tcl / Tk .

Tk is well suited for “quick and dirty” programming and prototypes, but also for creating small to medium-sized programs for Linux and Unix systems including macOS and Windows (from 95). The kit was developed for the Tcl programming language and is easy to use from there. Tk is released as free software under a BSD-style license .

Voice connections

There are various language connections for Tk, including the following programming languages:


Tk provides the following interface elements:

The following top-level windows are also available

  • tk_chooseColor - color selection
  • tk_chooseDirectory - folder selection
  • tk_dialog - Modal dialog
  • tk_getOpenFile - file selection
  • tk_messageBox - message box
  • tk_popup - Post a popup menu
  • toplevel - General toplevel window

Geometry manager

Tk's geometry managers (also called layout managers ) are pack, gridand place. The differences between the geometry managers:

Widgets are placed in the “remaining” space on the top, left, right or bottom. There is space for further widgets on the opposite side. With this you can only create very simple layouts at first, but they can also become more complicated by using (nested) frames. A particular advantage of pack(and also grid) is that with the appropriate settings, the layout adapts itself perfectly to changing window sizes.
Divides the space into a grid of rows and columns, similar to tables in a spreadsheet program . In particular, forms (left column field names, right column input fields) can be created nicely with this.
Allows completely free arrangement of widgets on the area. A major disadvantage is that the arrangement is fixed by default and cannot adapt to changing window sizes. Many other geometry managers also work this way, e.g. B. MS Access forms.
However, it is possible to arrange the widgets with percentages. This allows adjustments to the window size to a certain extent.

More complex layouts can be achieved by using (nested) frames. Only one of the geometry managers may be used in each frame, but all three can certainly occur within an application.

With support for Unicode by Tcl simply international characters can Tk applications are used in.

From versions Tcl / Tk 8, a 'native look and feel' is offered, i.e. H. with Mac applications the menu bar is always at the top of the screen, with Windows and Unix it is at the top of the respective application window. Other widgets also look the same as you are used to on the various platforms.

Tk is generally included in the scope of delivery of Tcl / Tk and is freely available. There is plenty of additional software, see also the article on Tcl. It is available in most Linux distributions; it can be downloaded for free from the Internet for Windows and Mac, also as a binary version. H. you don't need to translate it yourself.


A minimal drawing program in Tcl: a canvas widget .c is used, which can be drawn on with the mouse. To do this, “bindings” are agreed for the events “left mouse button pressed” and “movement with left mouse button pressed”. The procedure doodleestablishes these bindings:

 proc doodle {w {color black}} {
    bind $w <1>         [list doodle'start %W %x %y $color]
    bind $w <B1-Motion> {doodle'move %W %x %y}

With a left mouse click, a line object (whose start and end point still coincide) is created:

 proc doodle'start {w x y color} {
    set ::_id [$w create line $x $y $x $y -fill $color]

When moving the mouse, the current line object is extended by a certain distance:

 proc doodle'move {w x y} {
    $w coords $::_id [concat [$w coords $::_id] $x $y]

The main program is just three lines long:

 package require Tk
 pack [canvas .c -bg white] -fill both -expand 1
 doodle .c
 bind .c <Double-3> {%W delete all}

The last instruction defines that the drawing is deleted when you double-click the right mouse button.

The content of the following example corresponds to the Hello World example in the Wikipedia entry on Java Swing

# lädt ggf. das Toolkit (bei Ausführung mit der wish entbehrlich)
package require Tk

# setzt den Titel des Hauptfensters
wm title . "Hallo Welt mit Tk"

# fügt "Hallo Welt"-Text hinzu
pack [label .beispielLabel -text "Hallo Welt"] -side left

# setzt die Fenstergröße
wm geometry . 300x200

# Hinweise:
#   - Das Hauptfenster muss nicht erzeugt werden, sondern
#     ist von vornherein da.
#   - Das Hauptfenster muss nicht auf "sichtbar" gesetzt
#     werden, sondern ist von vornherein sichtbar.
#   - Dem Programm muss nicht beigebracht werden, dass
#     es beim Schließen des Fensters beendet werden
#     soll, weil das die Standardfunktionalität ist.
#   - Um ein Event Dispatching muss man sich nicht kümmern.

Web links

Individual evidence

  1. ^ Tcl / Tk Core Development. Retrieved July 16, 2018 .
  2. ^ The tk Open Source Project on Open Hub: Languages ​​Page . In: Open Hub . (accessed on July 18, 2018).
  3. ^ History of Tcl. Retrieved May 24, 2019 .
  4. a b c TkDocs: Background. Retrieved May 24, 2019 .
  6. - download options for Tcl / Tk 8.5.10