The Tk toolkit is a scripted GUI toolkit that is designed to be used from dynamic languages (initially Tcl, but also Perl and Python).
Overview
The Tk toolkit is a GUI toolkit that is designed to be used from dynamic languages. It was developed originally by John Ousterhout for use with Tcl, but has subsequently been evolved to be supported with many other languages (notably Perl, Python and Ruby).
Tk is a native toolkit on Windows and Mac OS X. On other Unix-based platforms, it is built directly on top of X11, and by default emulates the look traditionally associated with Motif (though this is configurable). It is recommended that newer applications use widgets from the Ttk set (where appropriate) as these use a theming engine that is more suitable for handling modern look-and-feels.
One of the key features of Tk is that its behaviors are defined almost entirely through scripting (plus a powerful event binding mechanism). This gives user code great flexibility to redefine what is happening without writing new low-level programs. The low-level drawing engine is written in C and takes care to postpone actual drawing activity until an appropriate moment (typically after all pending GUI events are processed) making Tk feel extremely responsive to user activity.
Examples
Tk is a remarkably simple toolkit. The following example shows how to create a window with the label "Hello, world". The example is in Tcl and designed to be run by the wish interpreter that comes with every tcl/tk installation:
label .l -text "Hello, world"
pack .l
Other languages are only slightly more verbose. Unlike with wish, other languages typically require you to import the tk library, create the root window, and start the event loop.
Here is the same example in Python 2:
import Tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Hello, world")
label.pack()
root.mainloop()
Related tags
- ttk - questions related to themed tk widgets
- tkinter - questions related to the python implementation of tk
- perltk - questions related to the perl implementation of tk
General reference links
- Tk 8.6 reference documentation – Note that this is Tcl-binding specific and definitive.
- Tk tutorial and higher-level documentation – Includes documentation for multiple languages.
- The Tcl wiki's Tk page