tcltkslides.ppt

Download Report

Transcript tcltkslides.ppt

Tcl and Tk
CSE 470 Fall 1999
Revised by Prasad
Objective
• To use Tcl Tk to develop GUI and prototype
of the project
What is Tcl/Tk ?
• Tcl (Tools Command Lanuage): is a simple string based
scripting language for controlling and extending
applications.
• Tcl provides generic programming facilities such as
variables, loops, and procedures.
• Tk: a Tool kit based on X-window system.
• Tk provides facilities (widgets) for building user
interfaces.
• Tcl/Tk are excellent tools for Rapid prototyping.
Tcl/Tk .. continued
• Tcl/Tk is interpreted rather than compiled
( The interpreter is in !/opt/bin/wish )
• It is a weakly typed language (strings and integer variables
are interchangeable).
• Tcl syntax is very much similar to UNIX shell
programming.
Tcl primitives
• Assignment
set name
set ID 89
set number ID
set number $ID
• if statement:
if {$turn == “0”}
{ computer_move }
else
{ player_move }
Value
““
89
ID
89
Tcl Primitives
• Procedures, expressions …
proc diag {a b} {
set c [expr sqrt($a * $a + $b * $b) ]
return $c
}
• Loops (the while loop)
while {$i <= 10} {
set I [expr $i+1]
}
Tk Fundamentals
• Tk provides commands for creating and manipulating
widgets.
• Widgets types include buttons, scroll bars, text boxes,
scales, canvas etc.
• The three step procedure for creating a widget.
1) Declare the widget
2) Define any event procedure
3) Pack the Widget.
A simple example: Push Button
• Declaring the widget
button .pushbutton -text pushme -command procpush
• Defining Event Procedure
proc procpush {} {
.pushbutton config -text “I am pushed”
}
• Packing
pack .pushbutton -side top
• Execute the script
wish filename.tk
Simple Example: contd ...
Naming conventions
• Widgets should be referred in a hierarchical fashion.
.
• Names of all top level widgets start with a “ “ (period).
• Example: Say we have top level entity as frame and inside
it we have a button.
frame
.entryframe
button .entryframe.pushbutton -text “push me”
• Thus all widgets have a parent-child relationship.
Timer
• The after command:
• after 1000
Makes the program wait for 1000ms before proceeding
further.
• after 1000 proc
Fires the procedure proc after 1000ms but proceeds
immediately to the next statement after after statement.
Example 2
Examples of widgets
• Canvas
canvas .colorwin
-width 7c
-height 2c
• Scale
scale .redscale -label Red
-from 0 -to 255
-length 7c
• Frame
frame .entryframe
• Text Box
entry .entryframe.Namebox
-width 7
-height 2