Today, I came across Scheme and then Racket. The latter not only sounds interesting but looks so, too. On wikipedia (Racket features - Wikipedia) I found this neat interactive GUI demo, which shows toolkit API simplicity and compactness compared to other toolkit bindings out there. Historically I have been programming GUIs using Tcl/Tk. Here, doing GUI-programming is as simple as... with Racket/gui. What I want to tell here! See the code comparison between Racket demo code and the same functionality written in Tcl/Tk:
#lang racket/gui
;; A GUI guessing game
(define secret (random 5))
(define f (new frame% [label "Guessing game"])) ; toplevel window
(define t (new message% [parent f]
[label "Can you guess the number I'm thinking about?"]))
(define p (new horizontal-pane% [parent f])) ; horizontal container
(define ((make-check i) btn evt)
(message-box "." (cond [(< i secret) "Too small"]
[(> i secret) "Too big"]
[else "Exactly!"]))
(when (= i secret) (send f show #f))) ; success => close window
(for ([i (in-range 10)]) ; create all buttons
(make-object button% (format "~a" i) p (make-check i)))
(send f show #t) ; show the window to start the application
=============== Tcl/Tk: ================
# A GUI guessing game
package require Tk ;# also creates toplevel window .
ttk::setTheme clam
set secret [expr int(rand()*5)]
wm title . "Guessing game" ;# toplevel window title
pack [label .t -text "Can you guess the number I'm thinking about?"]
proc message {i} {
tk_messageBox -type ok -message [ expr {$i < $::secret ? "Too small"
: $i > $::secret ? "Too big"
: "Exactly!" }]
if {$i == $::secret} exit ;# success => close window
}
for {set i 0} {$i < 10} {incr i} { ;# create all buttons
pack [ttk::button .b$i -text $i -width 3 -command "message $i" ] \
-side left ;# left to right packing
}
# now enters event loop
Overall, quite comparable code size and in both cases simple object handling.
In Linux, the GUI appearance in Racket looks better than in Tcl/Tk. Bindings of Tk exists for other languages as well, but the code looks clearly more complex than in the original Tcl-binding.
Insofar: a good job done with Racket!