back to coding
TCL is a good environment for making cross platform gui's.
Its syntax is pretty easy to understand and it's very flexible.
It's not like C, Javascript or Python though in that the syntax doesn't make you use braces or indents, it just uses spaces and different quoting to separate the word from the arguments and other statements.
That makes it feel kind of like shell.
Cheat sheet:
bell
sounds the system bell
puts "something"
write to standard output or console
wm title . "My Window"
configures the window title
.button config -font {{DejaVu Sans Mono} 18}
configure the font used in a button
[.buttonAcknowledge cget -text]
an expresion to get the text configuration from the .buttonAcknowledge widget
label .label -text "Label" -textvar label
button .button -text "I'm a button"
creates buttons and labels. Can use a -textvar option to make it change text by using set
widgets
- button
- label
- entry
- checkbutton
- radiobutton
- listbox
- scrollbar
- text
- canvas
pack .label
puts the label into the window
place .label -in . -x 10 -y 15
can use instead of pack to absolutely position the element
place forget .label
unmaps a widget
using grid for layout:
- grid .label1 -sticky w -padx 15 -pady 5
- grid .labelTime -sticky w -padx 15
- grid .frame1 -sticky w -padx 15 -pady 5
- grid .frame1.radioMinutes .frame1.entryMinutes -sticky w -padx 15 -pady 5
- grid .frame1.radioTime .frame1.entryTime -sticky w -padx 15 -pady 5
- grid .buttonAcknowledge -padx 15 -pady 5
- grid .labelAlarmTime -sticky w -padx 15 -pady 5
- grid .labelTimeRemaining -sticky w -padx 15 -pady 5
after 1000 timer
runs the timer after 1000 milliseconds, timer is a proc name
wm withdraw .
hides main window
wm deiconify .
puts it back up
tk_messageBox -message "My Message" -title "My Title" -icon error/info/question/warning -type okcancel
message box
winfo children .
lists children
winfo class .b
returns what class
winfo geometry .b
80x35+5+10
place config .b
returns a list with the sizes and positions
proc procname {args} {body}
define a procedure
global variablename
makes global variable available in a proc
parsing -
- words
- #comment
- $variablename or ${variable name} also $name(index)
- \ backslash escapes the next character
- {} -no further processing
- "" -escapes whitespace and semicolons only
- [] -replaced by the result of the command
expr 1 + 1
- expression
- - ~ ! Unary minus, bitwise NOT, logical NOT.
- * / % Multiply, divide, remainder.
- + - Add, subtract.
- << >> Left shift, right shift.
- < > <= >= Comparison: less, greater, less or equal, greater or equal.
- == != eq ne Equal, not equal, string equal (Tcl 8.4), string not equal (Tcl 8.4).
- & Bitwise AND.
- ^ Bitwise XOR.
- | Bitwise OR.
- && Logical AND.
- || Logical OR.
- x?y:z If x then y else z.
toplevel .toplevel
makes a new window
button .toplevel.button -text hello -command dothing
pack .toplevel.button
create button in other window. dothing is the proc it will call when pressed
destroy .toplevel
delete the window
set variable ""
assignment
while {expression} {procedure}
while loop
if {true} {puts "hi"}
shows hi
if {false} {puts "hi"}
shows nothing
any non zero number is true
0 is false, 1 is true
it also accepts yes true on as true
no false off as false
expr int(rand()*20)+1
random number from 1 to 20, expr evaluations math expressions
rename dothing ""
delete a proc
unset variablename
array size arrayname
get size of array
array names arrayname
get list of keys
foreach {key value} [array get arrName] {#do something with key and value}
foreach key [array names arrName] { #get current value with $arrName($key) }
lists
- lappend variablename value
- adds to the end of the list
- lindex $variablename 0
- returns that element, starting with 0
- llength $variablename
- returns list length
- lset listVar index index value
- sets a certain list element
- foreach value $listVar {#do something with $value, like puts $value}
argc
number of arguments
argv0
name of program
argv
array of arguments
info exists variablename
returns 1 if that variable is defined, can be used with arrays with subscripts
set array(key) value
how to assign a value into the array
string length $variable
returns string length
string range $variable $begin $end
returns sub string, begin and end are numeric
load lua_tcl.dll
loads the init code from a shared library
source
loads a file of tcl in
info body xproc
shows the text of the proc defination of xproc
bind .widget <<ListboxSelect>> xproc
runs xproc when the selection of .widget listbox changes
other bindings
- <Return>
- <Control-n>
- <Double-1>
- <ButtonRelease-3>
- <ButtonPress>
- <Motion>
- <<Modified>>
resize event
bind . <Configure> {confProc %w %h %W}
proc confProc {x y w} {if {$w eq "."} {.label config -text "x $x y $y w $w"}}
.listbox insert end text
add text to the end of the .listbox
.listbox delete 0 end
clear out all .listbox items
.listbox config -listvariable myvar
sets myvar to the list
lindex $myvar [.listbox index anchor]
returns the text of the current selection
exec mycommand
runs mycommand as an external program
glob *
lists files
file attributes thefile
gets file attributes of thefile
cd ..
change directory
save file
set id [open $Filename w]
fconfigure $id -encoding utf-8
puts -nonewline $id [.t get 1.0 end-1c]
close $id
open file
set id [open $Filename r]
fconfigure $id -encoding utf-8
.t delete 1.0 end
.t insert 1.0 [read $id]
.t see insert
close $id