simplified grid

Richard Suchenwirth 2007-12-11 - grid is a powerful geometry manager in Tk. Following is a set of functions that allows to use grid functionality in very compact code, based on these considerations:

  • In grid, all widgets are at the same level. If we don't need their names, we can just auto-generate them by incrementing an integer.
  • Short (one-letter) names for widgets: b for button, e for entry, l for label etc.
  • arguments to widget wrappers limited to bare necessities

While being simple, this approach also allows introduction of custom composite widgets, like the path selector p which comes up as an entry with a button labeled "..." that brings up a directory chooser dialog.

proc show_ui {} {
    package require Tk
    grid x                  [l "Example configuration"]
    grid [l Project]        [e project]       -sticky ew
    grid [l "Project path"] [p project_path]  -sticky ew
    grid [l "\# feeders"]   [s n_feeders 1 8] -sticky w
    grid x [l ""]           ;# extra empty row
    grid x                  [b OK generate]   -sticky ew
    wm resizable . 0 0
set winid 0
#---------------------- Here come the simple widget wrappers:
proc b {label cmd} {button .[incr ::winid] -text $label -command $cmd}
proc e varname {entry .[incr ::winid] -textvariable ::$varname -width 30}
proc l label   {label .[incr ::winid] -text $label -anchor w}
proc p varname {
    set f [frame .[incr ::winid]]
    button $f.2 -text ... -command "set $varname \[tk_chooseDirectory\]" -pady 0
    entry $f.1 -textvar $varname
    pack $f.2 $f.1 -side right
    pack $f.1 -fill both -expand 1
    set f
proc s {varname from to} {
    spinbox .[incr ::winid] -textvariable ::$varname -from $from -to $to

proc generate {} {exit}  ;# should do something before exit