Tcl / Tk - sci.brooklyn.cuny.edusklar/teaching/columbia/f02/cs3157/...1 advanced programming -- fall...
Transcript of Tcl / Tk - sci.brooklyn.cuny.edusklar/teaching/columbia/f02/cs3157/...1 advanced programming -- fall...
1
advanced programming -- fall 2002lecture #23 -- mon dec 2
Tcl / Tk� current release: TTTTccccllll////TTTTkkkk 8888....4444� slides from:
��������� ���� � ������� ���and H. Schulzrinne (spring 2002)� reference books:� Tcl and the Tk Toolkit, by John Ousterhout, Addison-Wesley (only
covers Tcl 7.3 and Tk 3.6).� Practical Programming in Tcl and Tk, by Brent Welch, Prentice Hall, 1999, 3rd ed (covers Tcl/Tk 8.2).� on-line command resources:���������� ��������������� �� ��!"��#�$�%������ �&�� '��()� *�#�+����,�%���-�%���.������#���������� �����������0/��+1���� )2"�3��$����04�!"��5�4�6� �/��$���/",�%�.���*�,�,)!�6,�,)!"�
cs3157-fall2002-lect23
what is tcl?
� open source scripting language� binary installers for Windows and Macintosh� source releases for UNIX platforms� runs interactively, using an application such as 738�93:3;< also runs as script files< also runs with 71=
cs3157-fall2002-lect23
tcl history
< developed in late 1980s by John Ousterhout< first release ~1991< tk usable around 1992< see >�?�?�@�A BBC�C�C D ?�E�F�D ?�G�B�H�I�EB�?�EF�J�K�L�?INMPOQD >�?�R�F
cs3157-fall2002-lect23
basics (1)
< tcl scripts are made up of commands separated by newlines or semicolons< commands all have the same basic form, e.g.: ST @)MVU�WYX[Z�W< try this out:\�] K T�^ ?�EF�L�>_ S�T @`MVU�WaX[ZWb W_ S�T K�?\�] K T�^
cs3157-fall2002-lect23
basics (2)
< each Tcl command consists of one or more words separated by spaces< the first word is the name of a command and the other words are arguments to that command< all Tcl commands consist of words, but different commands treat their arguments differently<dcfe1g1h treats all of its arguments together as an arithmetic expression, computes the result of that expression, and returns the result as a string
cs3157-fall2002-lect23
basics (3)
< the division into words doesn't matter for cNe1g1h :ST @)MVU�W�X�Z�Wis the same as the previous exampleST @)MVU�WYX[Z�W< but for most commands, the word structure is important, with each word used for a distinct purpose
2
cs3157-fall2002-lect23
basics (4)
< all Tcl commands return results< if a command has no meaningful result, then it returns an empty string as its result
cs3157-fall2002-lect23
basics (5)
< g � 7�: writes its argument to the screen< example script (to run on cunix), "a.tcl":��� B�I�@�?�B�F"I�E���FB���K ] B�?�E�FL�>@ \ ?�L�> S F�FIYC�INM F�H�< execution:\�] K T�^ ��D ?�E�F> S F�F�I C�I`M"F�H\�] K T�^
cs3157-fall2002-lect23
syntax (1)
< for a scripting language, Tcl has a simple syntax
< 8 ����� h�� � h�� � h��< a Tcl command is formed by words separated by white space< the first word is the name of the command< the remaining words are arguments to the command
cs3157-fall2002-lect23
syntax (2)
<��������< the dollar sign ( � ) substitutes the value of a variable. In this
example, the variable name is foo.
cs3157-fall2002-lect23
syntax (3)
<�� 8�9 � 81= : c 8 ��� �1:��< square brackets execute a nested command< used to pass the result of one command as the argument to another< in above example, the nested command is���� ���!#"�$��� �%�&�"which gives the current time in seconds
cs3157-fall2002-lect23
syntax (4)
')(�*,+�-�./*�0�1,2�23(' double quotation marks group words as a single argument
to a command' dollar signs 4 and square brackets 576 are interpreted
inside double quotation marks
3
cs3157-fall2002-lect23
syntax (5)
'���*�+�- .�*�0�1�2�2��' curly braces also group words into a single argument' but elements within the braces are not interpreted
cs3157-fall2002-lect23
syntax (6)
' example script (to run on cunix), "b.tcl":��� ��� � ������� ������ � ��������������������� �"! #%$'& #(�����*)���� �"! #%$'& #+)�����-,���� �"! #%$'& #�.
' execution:�� ����/0�21 ����3�#� �� �"! #4$*&�#� �� �"! #4$*&�#�� ����/
cs3157-fall2002-lect23
syntax (7)
'65' the backslash is used to quote special characters' e.g., 587 generates a newline' the backslash also is used to "turn off" the special meanings
of the dollar sign, quotation marks, square brackets, and curly braces
cs3157-fall2002-lect23
syntax (8)' example script (to run on cunix), "c.tcl":��� ��� � ������� ������ � ����������
�����-/�� �:9�;' execution: � �����/<��1 ����
� �����/<��1 ����0���� ���������=�� �����/<��1 ����0>�?@ @�A%B�A C�@�D>�?�@@=A%B�A C�@ DE FG�H�I<J�K L�J @*M�>�?@@�A%BANCO@ D MP > ?�@ @�A%BANC�@�D�QE FG�H�I<J�K L�J @ P >�?@@�A%BANCO@ D�QR�P > ?@@=A4B�ANCO@ D R QE FG�H�I
cs3157-fall2002-lect23
variables (1)
S Tcl allows you to store values in variables and use the values later in commands
SUTWVYX is used to write and read variables, e.g.: Z ? L<H*[ \
S the command returns the new value of the variableS you can read the value of a variable by invoking set with only a
single argument:Z ? L<H
cs3157-fall2002-lect23
variables (2)
S you don't need to declare variables in TclS a variable is created automatically the first time it is setS Tcl variables don't have typesS any variable can hold any value.
4
cs3157-fall2002-lect23
variables (3)
S to use the value of a variable in a command, use variable substitution, e.g.:? H�� C I�H�� [
S when a �
appears in a command, Tcl treats the letters and digits following it as a variable name, and substitutes the value of the variable in place of the name
S in the example above, the actual argument received by V������will be ��� (assuming � was set as in previous example)
S you can use variable substitution in any word of any command, or even multiple times within a word:
Z ? L-J�� D<? H�� CZ ? L<H����I J�� D I�H�� I=H
cs3157-fall2002-lect23
command substitution (1)
S you can use the result of one command in an argument to another command, e.g.:Z ? L������Z ? L���� ? H�� C I��������
S when a � appears in a command, Tcl treats everything between it and the matching � as a nested Tcl command
S Tcl evaluates the nested command and substitutes its result into the enclosing command in place of the bracketed text
S in the example above, the second argument of the second set command will be � ��!
cs3157-fall2002-lect23
double quotes (1)
S double-quotes allow you to specify words that contain spaces, e.g.: Z ? L<H*\��
Z ? L#"$��%Z ? L�& M I�H�'*I�"-G Z(� ? H�� C I�H#'6I)"�� M
after which, &#* M \)��'���%<GZ+��\ MS everything between quotes is passed to T VYX as a single word
S command and variable substitutions are performed on the text between the quotes
S the quotes themselves are not passed to the commandS if the quotes were not present, TWVYX would have received 6
arguments, which would have caused an error
cs3157-fall2002-lect23
braces (1)
S curly braces ,.- provide another way of grouping information into words
S they differ from quotes in that no substitutions are performed on the text between the curly braces, e.g.Z ? L�& P IOH�'*I�"-GZ(� ? H � C I=H#'*I�" � Qafter which, &#* M IOH�'*I)"-GZ(� ? H�� C I�H#''I�" � M
cs3157-fall2002-lect23
grouping and substitution (1)
S the Tcl parser goes through three steps:
((((1111)))) argument groupingS determines how to organize the arguments to the
commands: white space separates arguments; double quotation marks and braces group multiple words into one argument
((((2222)))) result substitutionS after grouping arguments, Tcl performs string
substitutionsS e.g., I�/ A A is replaced with the value of the variable / AA
cs3157-fall2002-lect23
grouping and substitution (2)
((((3333)))) command dispatchS after substitution, Tcl uses the command name as a key
into a dispatch tableS it calls the C procedure identified in the tableS the C procedure implements the commandS command procedures can also be written in Tcl
5
cs3157-fall2002-lect23
control structures (1)
S Tcl provides a complete set of control structures includingS conditional execution� G�/��L >�? F�� ?�@ Z ?
� Z B G�L�J >S looping� / ANC
� / ANC ? �J >� B+> G @ ?
S procedures� � C A J�� CO? L E C FS Tcl control structures are just commands that take Tcl scripts
as arguments
cs3157-fall2002-lect23
control structures (2)
S������ � �� ������������ syntax:����������� ��� �!���#"%$�&�' (�$�)�)��*���#",+.-�/�0�-������1�����2�����!����3$4&�'�(4$�)�)2�*����3657-�/�0�-8$�&�' (�$�)�) �!����9� i.e., just like C��:<; delimit body of if and else clauses��:<; can also delimit conditional expression� statements within body are separated by newlines
cs3157-fall2002-lect23
control structures (3)
�=��>?��@�A�B� syntax:01C���D�E�F7�1'G���!����),) ��(��*�4H=I!' J ���K$ (��#".L����KM�"ON'#J����P$#(1��Q7L����1M Q�R� options:� -exact: use exact matching for string to pattern� -glob: use glob style matching for string to pattern� -regexp: use regular expression matching for string to
pattern (like in Perl)
cs3157-fall2002-lect23
control structures (3a)
� glob style matching:0�D�S���T�U,V�W�D�E�F%' J����K$ (��,)��4(��*�4HXOY matches any sequence of characters in string, including a
null stringXOZ matches any single character in string. X8[P\�]�^�_1`�a matches any character in the set given by chars. If a sequence of the form x-y appears in chars, then any character between x and y, inclusive, will matchXcb�d matches the single character x. This provides a way of avoiding the special interpretation of the characters Y�Z�[Pa�bin the pattern.
cs3157-fall2002-lect23
control structures (4)
egf�h?ie syntax:j�k _.l mon�p�m7mKq�l msr�q�t�msu�v�w1xy can also use z4{ |�}�~�|���� and ���K��� � , just like Cy test should almost always be enclosed in �<�y e.g.:� {��%������}6�����,���4��� �����7��~�|�z ���#�7�� ��}����P��~��������
� y since variable substitution will be made before the loop is executed…
cs3157-fall2002-lect23
control structures (5)
y foreachy syntax:y foreach varname list bodyy foreach varlist1 list1 varlist2 list2 bodyy can also use z�{�|�}�~�|���� and �#�1��� � , just like with � {��y example:
����},������ {��1����z��,��~,� �,�4�8�7z6�7� � �7�� � ��� ��|��,�8�4����~� y the value of � is ���7�7�7z � �#�
6
cs3157-fall2002-lect23
control structures (5a)
y another example:����},������ {��K����z��,~������,z��6���4��� ��� ���� � ��� ��|��,�8��~����� y the value of � is ���7���6�8z � ��� � �y one more example:����},������ {��K����z��,~������,z��7�4�����7���,� ��� �7�� � ��� ��|��,�8��~��������� y the value of � is ���7�7�7� ��� z����,��� �
cs3157-fall2002-lect23
control structures (6)
y�������� takes two arguments:y an expression ($p > 0)y a body, which is another Tcl scripty�������� evaluates its expression argument using rules similar to those of the C programming language and if the result is true (nonzero) then it evaluates the body as a Tcl scripty it repeats this process over and over until eventually the expression evaluates to false (zero)
cs3157-fall2002-lect23
control structures (7)
y� ������ takes three arguments:y the name of a procedurey a list of argument namesy the body of the procedure, which is a Tcl scripty example:� �K{�z � {������%��������� � �,�����}��1����� � }7�����~ � ����� ��� � �7�����} �K����� � }��*��� � �%� �K����� � }����4�������������} � �!��� � �.� ��� ���� �1��}�� ��|��#�K����� � }�
cs3157-fall2002-lect23
control structures (8)
y everything between the curly brace at the end of the first line and the curly brace on the last line is passed verbatim to ������as a single argumenty� �� ��� creates a new Tcl command named !#"�$�%'& that takes two arguments( you can then invoke power as follows:)+*-,�. /1032
)+*-,�. /54�6 4�787( when 9�:�;=<'> is invoked, the procedure body is evaluated( while the body is executing it can access its arguments as
variables ( ?�@=A�< = 1st arg, 9 = 2nd arg)
cs3157-fall2002-lect23
control structures (9)
( >#<B=C�>�D causes the procedure to exit with the value of variable result as the procedure's result
cs3157-fall2002-lect23
commands (1)
( all interesting features in Tcl are represented by commands:( statements are commands( expressions are evaluated by executing commands( control structures are commands( procedures are commands( Tcl commands are created in three ways:( bbbbuuuuiiiillllttttiiiinnnn commands — provided by the Tcl interpreter itself and are present in all Tcl applications.( eeeexxxxtttteeeennnnssssiiiioooonnnn commands — created using the Tcl extension mechanism( commands created using 9�>�:�E
7
cs3157-fall2002-lect23
commands (2)
( Tcl provides APIs that allow creation of new commands by writing procedures in C or C++ that implement the command( then the command procedure is registered with the Tcl interpreter by telling Tcl the name of the command that the procedure implements
( then whenever that particular name is used for a Tcl command,Tcl will call the command procedure to execute the command( the bbbbuuuuiiiillllttttiiiinnnn commands are also implemented using this same extension mechanism; their command procedures are simply part of the Tcl library
cs3157-fall2002-lect23
commands (3)
( an application can incorporate its key features into Tcl using the extension mechanism; thus( the set of available Tcl commands varies from application
to application( there are numerous extension packages that can be incorporated into any Tcl application( one of the best known extensions is TTTTkkkk, which provides
powerful facilities for building graphical user interfaces( other extensions provide object-oriented programming, database access, more graphical capabilities, etc.( key advantage of Tcl is ease with which it can be extended to incorporate new features or communicate with other resources
cs3157-fall2002-lect23
commands (4)
( typically:( extensions are used for lower-level functions where C programming is convenient( procedures are used for higher-level functions where it is easier to write in Tcl
cs3157-fall2002-lect23
other features (1)
( string manipulation( including a powerful regular expression matching facility( arbitrary-length strings can be passed around and manipulated just as easily as numbers( I/O( files on disk( devices such as serial ports( network sockets — Tcl provides particularly simple facilities for socket communication over the Internet
cs3157-fall2002-lect23
other features (2)
( file management( commands for manipulating file names( reading and writing file attributes( copying files( deleting files( creating directories( subprocess invocation( running other applications with the exec command and
communicating with them while they run
cs3157-fall2002-lect23
other features (3)
( lists:( easy to create collections of values (lists) and manipulate them in a variety of ways( arrays:( structured values can be created consisting of name-value pairs with arbitrary string values for the names and values( time and date manipulation( events:( allows scripts to wait for certain events to occur, such as an elapsed time or the availability of input data on a network socket
8
cs3157-fall2002-lect23
what is tk?
( provides a GUI for Tcl( uses widgets( interacts with window manager (placement, decoration)( application = single widget hierarchy( widgets have � names and are children of their parent widgets( affects resizing, placement( e.g., � ������� � � ����� � ���
� � is topmost widget
cs3157-fall2002-lect23
widgets (1)
� a widget is an user interface object/control� e.g., pushbutton, label, scrollbar
� application user interacts with the widgets to communicate with the application
� interaction is usually through mouse or keyboard� each widget belongs to a class of its own defining:
� appearance� configurable options such as its foreground color, font
� methods used to access and manipulate the widget� e.g., modify configurabel options
cs3157-fall2002-lect23
widgets (2)
� can be nested, depending on their class/type� e.g. menubars contain pulldown menus
� a widget-based application may contain one or more hierarchy of widgets� e.g., Fileselectionbox, a text editor with a menu item "open"
that pops up a fileselectionbox
cs3157-fall2002-lect23
widgets (3)
� there are three basic steps of widget programming:1. create an instance of the widget (usually by calling a
widget creation function) and specify values for attributes i.e.options for appearance (there will always be default settings so you only need to set the ones you want to)
2. specify behavior (which user actions invoke which functions)
3. tell the geometry manager to make the widget appear on the screen in its position with respect to its parent
cs3157-fall2002-lect23
widgets (4)
� behavior may be a single command such as "exit" when a "Quit" button is pressed
� or a set of commands with input parameters which invoke complex behaviour (e.g., selecting a button labeled "Beethoven" causes a search for a particular tape and playing it).
cs3157-fall2002-lect23
widgets (5)
� geometry management is an independent process� any widget can be managed by any geometry manager � multiple geometry managers coexist providing consistent
behavior (e.g., resizing the parent resizes all the children within the parents geometry)
� the geometry manager is invoked with options for positioning a particular widget� right/left justification� placement at the top/bottom/left/right� in relation to its parent/siblings
� if nothing is specified, the geometry manager decides the positioning based on default algorithms
9
cs3157-fall2002-lect23
tk widgets (1)
� tk provides all the basic widget classes� there are also many contributed widgets available� tk widget classes are distinguished by three things:
((((1111)))) configuration options� specify the appearance of the widget� specify what happens to the widget when the user clicks
on it
cs3157-fall2002-lect23
tk widgets (2)
((((2222)))) widget command� in Tk, when a widget is created, a unique command
associated with the widget is also created� the widget command has the same name as the widget� the widget command is used to communicate with the
widget to make it change its internal state - i.e. carry out actions - for instance change the background color
� for complex widgets, the actions that can be specified depend upon the class of the widget - for instance accessing, inserting, deleting items within a listbox or menu does not apply to a label widget class.
cs3157-fall2002-lect23
tk widgets (3)
((((3333)))) bindings� Tk widget classes also have a set of default bindings� a binding is a general mechanism for associating a
particular user action (event) with a specific application defined behavior� e.g., pressing the right mouse button in a particular
widget pops up a help window
cs3157-fall2002-lect23
e.tcl
� first tcl/tk program:��� ��� ��� ���� � ��� ��� ��� ������� �� ����� � ���������� �� � ������� �� ��� � � � ������� � �������������� ���� ��� � � ��� ��� ��� ����� ��� ���������� �� � ���������� �� � ������� �
� ��� �� � ���� � � �� ������� � �� � � ��� � �!� � � ��� ��� �� ���� �!����������� ��� � �"��
cs3157-fall2002-lect23
tk widgets (4)
� most widgets are inside the toplevel window, but some can betoplevel themselves
� widgets are created at run time: �� ��� � � � ������� � �# ��������� ��� � ��� � # � � ��� �� ��� ���� widgets can be deleted at run time:������ ��$ � ������� � � widgets can be modified after creation:
� ������� � %��� � � ��� � � # � � ��� ��� ��� ���� � # ������ �� � �� widgets can be invoked, e.g., invoke button as if it were
pressed:� ������� � ����& ��� �
cs3157-fall2002-lect23
tk widgets (5)
� ffffrrrraaaammmmeeeessss� colored rectangular region, with 3D borders� typically, containers for other widgets� no response to mouse or keyboard��� ��� ��� ���� � ���� ��� ��� �����!� �� � ��� � � � � ��� � � ����� ����� � � � ��� � � ��� � ��� & � �������� �� � ��� �(' � � � � � � � ����� )�* ����� ������� )�+���� � � ��� �
' � � � � � � � ��� � �������-,��� ��� �(' � � ��� � ������� � � � � ������������. �!������� $ . ��� � � ��� �� � � ��� � �!� � ��� � ��� ��� ���� �
10
cs3157-fall2002-lect23
tk widgets (6)
� llllaaaabbbbeeeellllssss� � ��� ��� ������� � ��� ��� �� ������� �� ��-� ��� � � � ����� �� � � � � ������� � � � � � ���������� � � ��� � � ��' ��������� �� � � � � ������� � & � ��� �����������&�� -' � � ���� � �� � ������� � � � � � � ������� � &�� ��� �!��������� � � � ��
� � ��� � �������� � ��� � �������� ��� ���� �� $�� � � � ������ ��� � � ����� ��� $
cs3157-fall2002-lect23
tk widgets (8)
� bbbbuuuuttttttttoooonnnnssss,,,, cccchhhheeeecccckkkkbbbbuuuuttttttttoooonnnnssss,,,, rrrraaaaddddiiiioooobbbbuuuuttttttttoooonnnnssss������ �� �������������������������� ������������ �� �� � �!�"#��������$� � �!�"#����������� ���� � �!�"
%'& ����(� ���)������������ *� �+� ���!�,�������-���!�����.��'&/���!�,���� ��)��'&10��)������������ *� �+� 2��!2����������43���!�2���� .��+&52���!�2������ ��)��'&10��)������������ *� �+� �� ����6&�!2� ����������7� ���+& !�2� ���� .��8&/� ���'& !�2 ������� �)��'&90 �����*� �:� ���!�;� �8� 2����!�2��� �+� � ����6& !�2� ���<�2���4�� ���%�2�!�!4�
%'& ����(� %& ����2��������� *� �2���<������>='2�����<���.��'&�2�����!���%��� �,� .���!���4�2���<����� ��)��8&90& ����2��������� *� )��!�.���2����,�������?��!�.����2��,� .��'&/%��� �,��.���!4)���!�.6���2�����@��� ���)�'&10
& ����2��������� *� �����'& 2��6&>��A�BCA�DE�F6G H�B'G>I J�K+G H�K�LMB�N�E�OA,I J�K�M�F�B4P�E�F6G�HB'GRQIK�O�P�SE'G1T
U K�P�V*W A�H�X�B�Y*W SB�M�J�BA�H�P�K;W P�E�F6G H�B6G>IYH�Z�B4A�E U I�N�HM�M�C,I�H�O*W NU K�P�V*W E6V(W K UU M�[\W P*W N�IY�H�Z�B�M�BNA from Ousterhout
cs3157-fall2002-lect23
tk widgets (9)
] mmmmeeeessssssssaaaaggggeeeessss] like labels, but display multi-line stringsX�B�YY�K^�B�W X�Y�^,I T6HZ�A�S�_P�I`F�Y�A�H�N�[,MBN�A�QI6G B�MH�B�N#G K�HY�B�Z,IL�Z�a>QIN�E�O�A,I b�Z�E�LB�I�c�B�M�J�B�A�HPK�I�d�B�Z�H�FX�I�e�I�f�E8G X�K�M�II�g�Ih�_i�Ig�QI�A�B�CA,j�k�E�F�S�K�J�B4XKZ�B4P�SK�O�^�B�Y4AE�A�S�HY4Z�E�P�F�X�B�OA�YH�OPB4A�S�B4M�K�YAA�H�X�B�H�A�T�K�Y4YKJ�B�Z8W1l�Y�H�A�m�n�A�E4Z�H�YP�K'G Z�A�S�B4P�S�K�O�^�B�Y�o�j
U K�P�V*W X�Y^
from Ousterhout
cs3157-fall2002-lect23
tk widgets (10)
] lllliiiissssttttbbbbooooxxxxeeeessssM�HYA�LEC;W P�E�ME+G YU K�P�V*W P�E�M�E8G YY�B�A�N*p E U B�O>q�E U A�q�D�rD�s�t�h�he�u�q�MH�L�q�t'hh�q8G ^�L:W A�C�A�vT'SH�M�B�w8p ^�B�A�Y�x�N�MHO�B�v�y�z4i�{�wW PE�M�E8G Y�HO�Y�B'G A�BOZ;p M+G K�O^�B�xMH�OB�|�B�O�Z�v
{P�M�E�Y�B4x�NL�HOZ;W P�E�M�E8G Y4}�~E�FLM�B�I�F�AA�E�O�I�h�y�wW PE�M�E8G Y�P�E�O�N�H�^�F6G B�IL�K�P�V�^+G E�F�O�Z;p Y�B�M�BP�A�HE�O�^�B�A�v
{
from Ousterhout
cs3157-fall2002-lect23
tk widgets (11)
] ssssccccrrrroooollllllllbbbbaaaarrrrssssM�H�Y�A�L�E�C;W N�HMB�Y�I6G BMH�BN(G K�H�YBZ4QI�L�E'G Z�B'G T'H�ZA�S4a�QI [6YP'G E�MM�j'W YP'G E�MM�YBA�j
U K�P�V*W N�H�MB�Y4I�YHZ�B4MB�N�AY�P'G E�MM�LK+G,W YP+G E�M�M�I�P�EX�X�K�O�Z,j+W N�HM�BY�[J�H�B�T6jU K�P�V*W Y�P'G E�MM�I�Y�HZ�B,G�H�^�SA�I�NH�M�M>[NE+G B�K�P�S�H�p MY�E'G A;p ^�M�E�L�g�vv�wW N�HM�B�Y4H�OYB+G A�B�O�Z�xH
{
from Ousterhout
cs3157-fall2002-lect23
tk widgets (12)
] ssssccccaaaalllleeeessssY�P�K�M�B*WG B�Z,IMK�LB�M�e�BZ�I�N6G E�X4i,I�A�E�a�����I�M�B�O^A�S�h�i�P�QI�E+G HB�O�A�S�E+G H�E�O�AK�M�I�P�EX�X�K�O�Z�OB�T�D�E�ME'G
Y�P�K�M�B*W ^'G B�BO�IM�K�LBM��8G B�BO�IN6G EX�i�I�A�E�a�����I�M�BO�^A�S�hi�P>QI�E+G HB�O�A�S�E+G H�E�O�AK�M�I�P�EX�X�K�O�Z�OB�T�D�E�ME'G
Y�P�K�M�B*W LMF�B�IMK�L�BM4��M�FB�IN'G EX�i,I�A�E�a����4IM�BO�^A�S�h�i�P�QI�E+G HB�O�A�S�E+G H�E�O�AK�M�I�P�EX�X�K�O�Z�OB�T�D�E�ME'G
N+G K�X�B*W Y�KX U M�B�I�S�BH�^�SA�h:W �P�I T'H�ZA�S4u�PU K�P�V*W�G�B�Z;W ^+G B�BO�W L�M�F�B�I�Y�HZ�B4AE UU K�P�V*W Y�KX U MB�IYH�Z�B4LE�AA�EX,I U K�Z�[,a�XU G E�P�OB�T�DE�M�E+GRJ�K�MFB�wY�BA�P�E�M�E8G�p NE'G XKA,j����6i�aC���i�a�C���i�a�C�j*pWG BZ�^�B�A�v�pW ^'G B�B�O4^�BA�vp�W LM�FB4^�B�A�v�vW YK�X U MB�P�E�O�N�H�^,I�LK�P�V�^8G E�F�OZ�xP�E�M�E+G
{
11
cs3157-fall2002-lect23
tk widgets (13)
] ggggeeeettttttttiiiinnnngggg vvvvaaaalllluuuueeeessss��������������� : e.g., scale invokes with new value, as in newColor
43�� ����������������� : get value����������������� : set variable] event bindings
cs3157-fall2002-lect23
tk widgets (14)
� eeeennnnttttrrrryyyy��� ��!"�$# ��� �"!��&%('"!�) '&* +�,��"!.- ��/"!�0 *! -1'32 45# ! -1'�2 46% 7�,�8 ' 9;:�<=%>2 !"��, ! ?A@"B�-"C�! -&%1�18&:&%�' !�)�' D ��2 ,�� �"��!;-1� /"!E � F C$# ���"��!"�$# !�-�'�2 4=%�@ ,18>!.�"!�?�'=% E ��8 )&G1/=% E ��8�46:1/
cs3157-fall2002-lect23
tk widgets (15)
H ccccaaaannnnvvvvaaaassssH display and manipulate graphical objects
H rectanglesH circlesH linesH bitmapsH text strings
H tagged objectsH manipulate all objects with same tag (drag)
H event bindings for objects
cs3157-fall2002-lect23
tk widgets (16)
I J K1L"J"MON I&P Q�R�S T U;V�W I&P1U"X�R�Y"U�T&V�N Z I[ J I"\$N IN IAI3] X1J"T X;^�R K XAV"I`_�N Z.V"IAV IAV"V�I;V�I;V V�IA_�N Z�Ia b ]`c�M�X TARA_"d&c1e"R`f&V�_ d&c�R K I�]gR�d&cM X�TAh5i X h [3]ge�R j V�kN I;I�](X1J�T>XA^ R K�X;e c1h"d I;V�I;e"c(h>d�IA_�N l IN I;I�](X1J�T>XA^ R K�X;e c1h"d�N W>Z1I;V I;e c(h>d�N W"Z�IA_�N m IN I;I�](X1J�T>XA^ R K�X;e c1h"d�N Z IAV IAe>c(h d�N Z�IA_�N n1IN I;I�](X1J�T>XA^ R K�X;e c1h"d�N n"Z1I;V I;e c(h>d�N n Z�IA_�N m IN I;I�](X1J�T>X.T"X�h�T&e c1h"d�N V>Z1ION n�Z IAP�T X�h T;e"R;P1J"K�I"U b ]oM1Q
d
cs3157-fall2002-lect23
a more complex example
H canvas items generate names:p�q�r�s�tvu�w1txtzy>q�{�r�q|t�}zy>t�~�q
w�w�w1�H canvas items can be tagged:
w(txt�y>q�{�r�q�����{�~�w�w�w=�� r�{���p�s�������{�~
w(t���q�~�q�r�q�s�������{�~w(tx}�r�q�s�t�������}�����y>q|t�}zy>t�~�q ���}�~�~�y"q��
H several items can have the same tagH one item can have multiple tags
cs3157-fall2002-lect23
the selection
H mechanism for passing information between widgets and applications
H first select, then get information about selectionH copy & paste, but also actions
12
cs3157-fall2002-lect23
window managers
H each X display has a window managerH controls arrangements of top-level windows on screenH basically the same as a geometry managerH provides decorative framesH allows iconify and de-iconify of windowsH examples: mwm, twm, fvwm95, KDE, Gnome, ...
cs3157-fall2002-lect23
tk wm
H e.g., add title:����������� �������������������������
� iconify a toplevel window������������������� �
� normally, user cannot resize Tk windows, but��������� ���!�"� �$#�%�%'&(%������)�*� ���!�"� ��+�%�%�#�&,%
cs3157-fall2002-lect23
tk modal interactions
� usually, user can select input focus (which widget the user is sending input to)
� modal interactions = restrict user choice� example: dialog box forces user to fill it out before continuing�.-0/�132 restricts interaction to few windows�5476�891;:<4 suspends script until an event happens� use only in exceptional cases
cs3157-fall2002-lect23
modal interaction example=,>@?�?�A,BDC E@FGBIH�J�C A,KMLN?,HIO�?MA,KMLIP@A,QIQIFGB@RTSU HI?MJ�F�=IH,JWVGXR,H U ?�Y A@Z[C EIF,B�HIJ\
=,>@?�?�A,BDC E@FGBIH�J�C P@FGBIPIH�J]LN?GH@O�?]P@FGBIPIH,JMLIPIAIQ,QIF,BIR]SU HI?MJ�F�=IH,J_^�F,BIP,HIJR,H U ?�Y A@Z[C EIF,B�HIJ\
E�F�P,K`C E�F�BIH,J�C AGKaL UIb R,HMJIH,c@?E�F�P,K`C E�F�BIH,J�C PIF,BIP,HIJML U�b RGHaJ�H�c@?d Y F,= U H�?`C E@F,B�H�J?(K eGF b ?_e b B�RIA@efC E�F�BIH�JE,>@? U]g J�F�=IH,J_h]iIJ�F�=IH�J g
cs3157-fall2002-lect23
getting information about widgets
� winfo provides information about widgets:
�����������*��� �� � �� returns 0 or 1
������������j������<k,�l� �� returns .w.a .w.b
��������������) � � �� returns Button
cs3157-fall2002-lect23
Tcl in C (1)
� C implements objects� manipulated by Tcl commands� often, action oriented:
robot turn r17� object oriented: one command for each object (e.g., Tk
widgets)� slides from Henning Schulzrinne, coms w3995, spring 2002
13
cs3157-fall2002-lect23
Tcl in C (2)
� two modes:� enhance wish or tclsh with additional C commands� use Tcl_AppInit()
� add Tcl interpreter to existing C program� create interpreter
cs3157-fall2002-lect23
Tcl in C: example Tcl_AppInit
���������������� ������ ������������ ���������������� � � ������� ��� ��� ����� ��������� � � � !"���$#� � � �����$ ��� ����% & '���� � �����(�� ���$!�)�� ����*+ �-, % ���� � & ��� ���.'
���� �/ � �0�% /�������$ �( � � �.1 � ���$2�3��� � �+4�� ���� � � 4 15���� ��� 6 �.17��� �.�8�$�.� 6�9�: ; &�<��� � ��4 �� � ��� �� ������� �$=�� � �$�
>
���� � ���+2�? 4�4 3����$ .% � ��� 2�3��� � � 4�� ���� � � 4 &=<� � % � ��� 2�3����� �% ���� � �+4 & ,�,�� /�@ 2�A$B�B+C�B�&�<� � �� � � � /D@ 2�A�B�B+C�B�'
>� ��� 2�/ � � �$/ ������� �$�%+���� � � 4 17E�� � �0�E�15/ � �0.17F�G�@�@�15F�G�@�@�&.' ���� 2DB���H$�����F ��� , E � � ��!���4�4D� ��E.'� � �� � � � /�@ 2�C�I '
>
cs3157-fall2002-lect23
Tcl in C: creating Tcl interpreters
JLK.MDN�O�PDQDR�S T�UWV�K.MDN�O.XYT�SDZDR�S�PDQ�R�S�T�U�[+\�].^�_.`JLK.MDN�O�a�\�ZDN�[ K.M.N�O�PDQ�R�S�T�UbVdc eYf�g�h+ikj�M�l�Z�TmVDn o.hYc idf+`JLK.MDN�O�a�\�ZDN.p�^�N.SY[�c eqf�g�h+ikj�M�l�ZYT"Vsr.t u v�w�xDy�v�z
cs3157-fall2002-lect23
Tcl in C: creating new Tcl commands
{}|D~.���D�D���W�D��|b�.�D���.�D��� �.�$���Y������������|��D��|.����u�t+v��q�+��xY�$x���.�D�����D��|������b�dt �Y��v��+�k���D��|�x �$���q�������Y�"��x��$�s�. +¡�zd¢
{£�.�D���.�Y���D��|������D�D���.���Y� �.�������D�D|������b�dt �Y��v��+�k�����D���"� ��yq¤�w�x�y�vs��.�D���.�D�.� ���$���W� ��yY¤D¥���¦.�s�8��� ������|�����|�����u�t+v��Y�+�.xY�$x���.�D���.�D���D���������D�D�D�D|�� ���$�.�b� ¤.v�u v ��v�¥���¦.��zd¢
cs3157-fall2002-lect23
Tcl in C: example
§�¨�©"ª+«�¬��®.¯ °�±�§�²�¨$©�³�´�©�´=° µ·¶�°�± ¸�¹�¨$©�²�º »�¼�§�¨�©�²Dº+».µ7§�¨$©�´.º ½�°�µ5°�¾�´�º¼�´�º ½�¿YÀ Á�Â�ç�Äů Æ�©�º+°$�».¯ ´.º ½�¿�À Ç�Á.µ5´.º ½�¿�À È�Á�Â�É�É�Ê�Â=Ã
§�¨$©�².º »�Ë$Ì.Í Î�Ï�Ð�Ñ�Ò�Ó"Ô$Õ�Ô.Ö× Î�Ñ�Ï�Î=Ø
Ù�Ú$Ò�Î.Í Û�Ü$Ì.Í Î�Ï�Ð�Ñ�Ò�Ó"Ô+Ý�Ô.Ö×Í+Î$Ò�Ð.Í ÚßÞ�à�á+â�ã�ä�Ö
×Ù�Ú�Ò�Î.Í Û�ÓßÞDå�Ñ âDà�Í Î�æ�Ò�Î�ç�Ú$Ò�Î�Í ÛDè+é.ÖÞ�å�Ñ âDà�Í Î�æ�Ò�Î�à�ê�ë�ë�æ�Ú�ì�è Ù�Ú�Ò�Î�Í Û í-Ô$Î$îDÔ�í-ï$î�à�ë�ì�í�è à�Ñ�Ù�Î�Ú�Ò�ð�æ�Ò�æ�é�ñ�ò�á�á.í
è Þ�å�Ñ+â�à�ë�ì�ð�Î�Ñ�Î�Ò�Î�ó�Í ê�å�ô�é�ñ�ò�á�á�é Ö
cs3157-fall2002-lect23
Tcl in C: Tcl results
õ÷ö.øqù ú û�ú üþý ö�ÿ����.ö�������� � ö údÿ�ù��������ÿ��sÿ�úYý���� ö����������Yÿ�ú.ú��sÿ������.üdÿ�ú ú��Yÿ������� � öþúdÿ ÿ � ÿ"! � �.ú��
#õ � � ö�ú�ÿDù�$"%�ÿ�ú�ý����.ö for constant stringsõ Tcl_Result(interp, "string", TCL_STATIC);õ&��'�!��(�)�!�*����!�+ : on stack frameõ&��!"' �, -�."*�/�"' : allocated via malloc
14
cs3157-fall2002-lect23
Tcl in C: Tcl variables from C
� Tcl_SetVar(Tcl_Interp *interp, char *varName, char *newValue, int flags)
� typically, global variable, but local if executed within function unless flags = TCL_GLOBAL_ONLY
� Tcl_SetVar(interp, "a", "44", 0);� char *Tcl_GetVar(Tcl_Interp *interp, char *varName, int flags)
� value = Tcl_GetVar(interp, "a", 0);
cs3157-fall2002-lect23
Tcl in C: variable linking
� associate Tcl variable with C variable� whenever Tcl variable is read, will read C variable� writing Tcl variable
�write C variable
� e.g.,int value = 32;
Tcl_LinkVar(interp, "x", (char *)&value, TCL_LINK_INT);