Skip to content

GUI Toolkit Tk

Wolf Wejgaard edited this page Feb 22, 2018 · 9 revisions

TclForth contains Tk, the widely used multi-platform GUI toolkit (Tcl, Perl, Python, Ruby). You can use all features of Tk in TclForth Code words, and access Tk directly in Forth. - For a quick overview of the Tk command set see here. - However, for a real use of Tk you might prefer a deep look into these books.

Forth widgets

In addition, TclForth includes prototypes for Forth widgets that you can address in Colon words. The Forth widgets are written with a leading upper-case to discriminate from the Tk notation (e.g. Canvas versus canvas)

Widgets in TclForth

Forth Console

console.fth contains Forth widgets for Window and Menus

Chess Game

chess.fth contains Chessboard, drawMan, bindBoard, drawChess - Tk: Panel

A detailed tutorial Example:

Build a list box that displays the available system fonts.

The Listbox in Tk notation

Let's create the box in a separate window.

tcl toplevel .l

.l is the internal name. Use it to add a title.

tcl wm title .l "The Font Box"

Insert the listbox as a child widget.

tcl listbox .l.f -listvariable fontlist

The list variable determines the contents of the list box. Make i a list of the system fonts.

tcl set fontlist [font families]

Now arrange the window and widget on the screen (make it visible).

tcl pack .l.f

pack is one of Tk's three geometry managers. Like everything in Tk, it has a large set of attributes that are initially set to default values. You get a working result at once and can fine-tune it later.

Finally, define an event and its callback function (as an action script).

tcl bind .l.f <Double-Button-1> {
	%W configure -font [list [%W get [%W curselection]] 12] }

%W delivers the current window's id.

Start fine-tuning: Change the size of the list box.

tcl .l.f configure -height 30 -width 60

The top window now delimits the box size. Enlarge it to see the full box.

Or make the box fill the top window and change its size accordingly.

tcl pack .l.f -expand true -fill both

A little more tuning. Insert a little margin on the left.

tcl .l configure -padx 25

And remove the frame of the list box

tcl .l.f configure -relief flat

All together now in a TclForth Code word

Code FontBox {} 
	toplevel .l -padx 25 
	wm title .l "The Font Box"
	listbox .l.f -listvariable ::fontlist -relief flat -width 30 -height 30 
	pack .l.f  -expand true -fill both
	set ::fontlist [lsort -dictionary [font families]]
	bind .l.f <Double-Button-1> {
		%W configure -font [list [%W get [%W curselection]] 12]}

FontBox

The Forth Listbox

Here is the same list box in postfix notation as a Forth colon word.

: TF-FontBox {}
	".t" "toplevel" Widget Top  "-padx 25" Top config
	"The TF-Font Box" Top title
	".t.f" "listbox" Widget Listbox  
	"-expand true -fill both" Listbox pack
	"-listvariable fontlist -relief flat -width 30 -height 30" Listbox config
	"<Double-Button-1> {push %W ; ChangeFont}" Listbox bind

"[font families]" variable fontlist

Code ChangeFont { w -- } $w configure -font [list [$w get [$w curselection]] 12]

TF-FontBox

Clone this wiki locally