Tcl/Tk 1 CS 414, Software Engineering I Mark Ardis Rose-Hulman Institute December 3, 2002
2 Outline Install Tcl/Tk Run some demos Create some simple widgets
3 Install Tcl/Tk 1.Follow the instructions on the handout to copy and install Tcl/Tk 2.Run some demo programs: Start/Programs/ActiveState ActiveTcl / Demos/Tk 3.Check out the documentation ActiveTcl html/index.html Right click on Package Documentation to Open in New Window
4 start.tcl 1.Follow the instructions on the handout to copy start.tcl 2.Double-click start.tcl 3.Close start.tcl 4.Open start.tcl with WordPad (or Notepad)
5 Variables in Tcl Use set for assignment set foo 123 set tw.top Use $ for reference set bar $foo toplevel $tw
6 Widgets in Tk Widget names follow a hierarchy. top 2 b1b2b3b4.top.2.b4
7 frame and pack Use frame to create a logical group of widgets Use pack to display a widget frame $tw.2 pack $tw.2
8 button The button command creates a button button $tw.2.b1 Attributes may be specified when creating the widget or later with configure : button $tw.2.b1 -text "7" -command "Digit 7" or $tw.2.b1 configure -text "7" -command "Digit 7"
9 foreach foreach takes 3 arguments: a loop variable a list of items to assign to the loop variable a block of code The block of code is executed once for each item in the list
10 foreach example foreach col { } { pack $tw.$row.b$col } is the same as: pack $tw.$row.b1 pack $tw.$row.b2 pack $tw.$row.b3 pack $tw.$row.b4
11 proc proc foo {bar} { global baz set baz $bar } proc Digit {arg} { DisplayMe $arg } The global variable baz needs to be declared inside foo
12 Finishing the lab 1.Add more buttons 2.Add a label widget for a display 3.Change DisplayMe to update the label you just created