Posted to tcl by Stu at Thu Jul 20 12:58:45 GMT 2023view raw

  1. $ cat incomp.tcl
  2.  
  3. #! /bin/sh
  4. # \
  5. exec tclsh${TCL} "$0" "$@"
  6.  
  7.  
  8. proc createGui {w compact} {
  9.  
  10. set code [gub "
  11.  
  12. # A row of labels over a row of entries
  13. # Use row/column adjustments to turn it
  14. # into a vertical stack of widgets.
  15.  
  16. # This is understandable when working it out
  17. # but I'm not sure it can be easily understood
  18. # just by reading it.
  19.  
  20.  
  21. ;L L L L ;# Four labels.
  22. ;E E E E ;# Four entries.
  23.  
  24. ;Li ;# Another lable to show info.
  25.  
  26. :Li -columnspan 4 ;# Grid it to span all the columns.
  27.  
  28. .{L} -text a b c d ;# Give the labels some text.
  29.  
  30. # The power of Tcl!
  31.  
  32. [expr {$compact ? {
  33.  
  34. !:-L1-3 +2 ;# Move labels L1-3 down by 2 rows
  35. ;# relative to what would be their rows.
  36.  
  37. !:-E0 -6 ;# This pushes E0 down by 6 rows
  38. ;# so it has to be pulled back up.
  39.  
  40. !:-E1-3 +2 ;# Entries E1-3 have to be pushed
  41. ;# down like with L1-3.
  42.  
  43. !:|L1-3,E1-3 -1 ;# Pull back the columns for the widget by 1.
  44. ;# This will put them all in column 0.
  45.  
  46.  
  47. .Li -text Compact
  48. } : {
  49. .Li -text Normal
  50. }}]
  51. " $w]
  52.  
  53. eval $code
  54.  
  55. puts $code
  56. puts ""
  57. }
  58.  
  59.  
  60. package require gub
  61. namespace import ::gub::gub
  62.  
  63. package require Tk
  64.  
  65. toplevel .q
  66.  
  67. createGui . no
  68. createGui .q yes
  69.  
  70.  
  71. # EOF
  72.  
  73.  
  74.  
  75. $ ./incomp.tcl
  76.  
  77. grid [ttk::label .l0 -text a] -row 0 -column 0
  78. grid [ttk::label .l1 -text b] -row 0 -column 1
  79. grid [ttk::label .l2 -text c] -row 0 -column 2
  80. grid [ttk::label .l3 -text d] -row 0 -column 3
  81. grid [ttk::entry .e0] -row 1 -column 0
  82. grid [ttk::entry .e1] -row 1 -column 1
  83. grid [ttk::entry .e2] -row 1 -column 2
  84. grid [ttk::entry .e3] -row 1 -column 3
  85. grid [ttk::label .l4 -text Normal] -columnspan 4 -row 2 -column 0
  86.  
  87. grid [ttk::label .q.l0 -text a] -row 0 -column 0
  88. grid [ttk::label .q.l1 -text b] -row 2 -column 0
  89. grid [ttk::label .q.l2 -text c] -row 4 -column 0
  90. grid [ttk::label .q.l3 -text d] -row 6 -column 0
  91. grid [ttk::entry .q.e0] -row 1 -column 0
  92. grid [ttk::entry .q.e1] -row 3 -column 0
  93. grid [ttk::entry .q.e2] -row 5 -column 0
  94. grid [ttk::entry .q.e3] -row 7 -column 0
  95. grid [ttk::label .q.l4 -text Compact] -columnspan 4 -row 8 -column 0
  96.