Sei sulla pagina 1di 21

TK

Now we move on to the graphical areas of Tcl - Tk. If you have ever tried your hand
in programming graphical programs in C++, you are going to scream in anger when
you see how easy it is to make graphical programs in Tk. You will e very angry that
you had not found this language efore. You are warned.
Now we go ack to the !ello "orld program that I showed at the eginning.
#$%usr%local%in%wish
#&ake a lael '!ello "orld'
lael .hello -te(t '!ello "orld'
pack .hello
I am showing this part for the second time - I want to make sure that you know how
this is done.
The first line - )#!/usr/local/bin/wish) is not needed in windows. In *inu(, it tells the
name of the script language processor. +on)t understand what that means, +on)t
worry your gray cells over it. -ust put it at the top of the file. .ut if you are planning
to make a good portale script, don)t use that line. /se the following one instead.
#$%in%sh
#The ne(t line e(ecutes wish - wherever it is 0
e(ec wish '12' '13'
"hy, 4ee Tcl%Tk in /ni( for the reason.
The second line - This is a comment. 5ny line that starts with a )#) char is a
comment. Comments are not of any use in the program. It is used y programmer to
talk to themselves. 5 programmer cannot e e(pected to rememer every thing a
script does. 4o he uses a comment to write it down. Ne(t time he edits the script, he
can read the comment and understand what the program is for. It is good practice to
make as much comments as possile.
The third line - )label .hello -text "Hello World") makes a lael and writes '!ello
world' in it. You can change the te(t to any thing you like. Note the structure of the
command -
lael - The name of the widget. 5 widget is a user interface o6ect in 7 graphical user
interfaces. Confused, Yes, &e too. *ets 6ust say that it is the name of the o6ect that
appears on screen. There are many other widgets too. If you want to display a
utton, you use the utton widget. 8or te(t, you use the te(t widget. 8or entry, you
guessed it, the entry widget.
.hello - The name assigned to the widget. 9ver widget must have a /NI:/9 name.
This name will e used when ever that widget must e accessed. This is called the
path.
-te(t '!ello "orld' - The option for this widget. This option says that this widget
must e given the te(t '!ello "orld'. ;ptions change according to the widgets - a
utton widget will not have all the options of the lael widget and vise versa. .ut
there will e many common ones. You can keep writing other options can also e
written here. 8or e(ample, let us make a lael for showing the te(t '!ell "orld'. The
other lines are same as the !ello "orld program.
lael .hell -te(t '!ell "orld' -font courierfont -relief raised
In this e(ample, a lot more options are used. The font option is used to tell which
font must e used to make the te(t and the relief option tells whether the te(t
should appear raised, sunken, flat etc. To know all the options for a particular widget,
read the manual that comes with Tcl. It lists every widget and every option they
have. If you are going to program in Tcl, you will find your self peeking into the
manual every few minutes.
4o we have the final synta(.
<Name;f"idget= <path= ,<option >= <option ?= ...,
The fourth line - pack .hello - this line tells the you how to pack the widget. This line
asks the interpreter to pack the widget called '.hello'. 5nd the interpreter )packs) it.
Now pack is a geometry manager. 5nother geometry manager is )grid). @ersonally, I
like grid etter.
Now lets learn more aout the individual widgets...
Widgets 1: Button, Entry, Label
Three things need to e said aout widgets. 8irst is the path. This I have e(plained
earlier. The paths of all widgets must e uniAue and will e used when ever that
widget needs to e accessed. 4econd is the options. 9ach widget has some options
which can e used to configure it. This is usually done when the widget is declared,
ut it can e done afterward also. The final thing is commands. 9ach widget has
some commands which also can e used to configure it or make it do some thing.
.ut efore we egin, we need to know a little aout the pack command. I have
e(plained this earlier ut 6ust doing it one more time so that you don)t have to push
the ack utton. @ack is a geometry manager. 5nother geometry manager is )grid)
and I like it etter - we will e(plore that latter. @ack is much more simpler than grid.
The line pack .hello tells the interpreter to pack the widget called '.hello'.
If the command was pack .hello -in .frame, the widget .hello will e packed in
another widget called .frame. In the asence of the )-in) option, the specified widget
is put in the main window.
button
This will make a utton. It can e configured to e(ecute some code when pushed.
This will usually refer to a function so when the utton is pushed, the function will
run. 5n utton is shown using !T&* elow.
Some Options
-te(t 'T97T' T97T will e the te(t displayed on the utton
-command 'C;+9' C;+9 will e the code that is e(ecuted when the utton is pushed
proc pushButton CD C
... whatever ...
D
utton .ut -te(t '@ush &e' -command 'pushButton'
pack .ut
Entry
5n entry is a widget that displays a one-line te(t string and allows the user to input
and edit te(t in it. "hen an entry has the input focus it displays an insertion cursor
to indicate where new characters will e inserted. 5n entry element is shown using
Some Options
-width N/&.9E "idth of the input field. N/&.9E should e an integer.
-te(tvariale F5EI5.*9
The contents of the variale F5EI5.*9 will e displayed in the
widget. If the te(t in the widget is edited, the variale will e
edited automatically. F5EI5.*9 should e given without a
preceding )1) sign.
-state 4T5T9
The state of the input field. It can e normal, disabled, or
readonly. If it is readonly the te(t can)t e edited.
Some Commands
Synta !es"ription Eample
path get
The te(t inside input field can e
taken y this command
set name G.ent getH
path delete 8IE4T ,*54T,
+elete one or more elements of the
entry. 8IE4T is the inde( of the first
character to delete, and *54T is the
inde( of the character 6ust after the
last one to delete. If last isn)t
specified it defaults to 8IE4T+>, i.e.
a single character is deleted. This
command returns an empty string.
.ent delete 2 end
path insert index 4TEINI Insert the characters of 4TEINI 6ust .ent insert end '!ello'
efore the character indicated y
index. Inde( is 2 for the first
character. The word 'end' can e
used for the last character
Eample
proc pushButton CD C
.ent insert end '!ello'
D
entry .ent
utton .ut -te(t '@ush &e' -command 'pushButton'
pack .ent
pack .ut
Label
This widget display te(t messages.
Eample
proc pushButton CD C
.ent insert 2 '!ello '
D
lael .la -te(t '9nter nameJ'
entry .ent
utton .ut -te(t '@ush &e' -command 'pushButton'
pack .la
pack .ent
pack .ut
Widgets #: $rame, Tet, S"rollbar, S"ale
Frame
5 frame is a simple widget. Its primary purpose is to act as a spacer or container for
comple( window layouts. The only features of a frame are its ackground color and
an optional K-+ order to make the frame appear raised or sunken.
Some Options
-relief 4TY*9 4pecifies the K-+ effect desired for the widget. 5cceptale values are
raised, sunken, flat, ridge, solid, and groove. The value indicates how
the interior of the widget should appear relative to its e(teriorL for
e(ample, raised means the interior of the widget should appear to
protrude from the screen, relative to the e(terior of the widget.
Eample
proc pushButton CD C
.ent insert 2 '!ello '
D
frame .frm -relief groove
lael .la -te(t '9nter nameJ'
entry .ent
utton .ut -te(t '@ush &e' -command 'pushButton'
pack .la -in .frm
pack .ent -in .frm
pack .frm
pack .ut
text
5 te(t widget displays one or more lines of te(t and allows that te(t to e edited.
4imilar to the entry widget ut a larger version of it.
Some Options
-(scrollcommand C;&&5N+
This is to enale communication etween a te(t widget
and a scrollar widget. There is a -yscrollcommand
similler to this one.
-font 8;NTN5&9
4pecifies the font to use when drawing te(t inside the
widget.
-width N/&.9E 4pecifies the width of the widget
-height N/&.9E 4pecifies the, you guessed it, height of the widget
Synta !es"ription Eample
path get index1 ?index ...? Eeturn a range of
characters from the te(t.
The return value will e all
the characters in the te(t
starting with the one
whose inde( is index1 and
ending 6ust efore the one
whose inde( is index Mthe
character at index will
set contents G.t(t get >.2 endH
not e returnedN. If
index is omitted then the
single character at index1
is returned.
Note that the inde( of te(t
is different from that of
the entry widget. The
inde( of te(t widget is in
the form
*IN9BN;.C!5E9CT9EBN;.
This means that >.2
means the first character
in the first line.
path insert index +5T5
Inserts all of the chars
arguments 6ust efore the
character at inde(. If
inde( refers to the end of
the te(t Mthe character
after the last newlineN
then the new te(t is
inserted 6ust efore the
last newline instead.
.t(t inset end '!ello "orld'
Eample
proc pushButton CD C
set name G.ent getH
.t(t insert end '!ello, 1name.'
D
frame .frm -relief groove
lael .la -te(t '9nter nameJ'
entry .ent
utton .ut -te(t '@ush &e' -command 'pushButton'
te(t .t(t -width ?2 -height >2
pack .la -in .frm
pack .ent -in .frm
pack .frm
pack .ut
pack .t(t
Scrollbar
5 scrollar is a widget that displays two arrows, one at each end of the scrollar, and
a slider in the middle portion of the scrollar. It provides information aout what is
visile in an associated window that displays an document of some sort Msuch as a
file eing edited or a drawingN. The position and siOe of the slider indicate which
portion of the document is visile in the associated window. 8or e(ample, if the slider
in a vertical scrollar covers the top third of the area etween the two arrows, it
means that the associated window displays the top third of its document. It is made
to work with other widgets like te(t. Some Options
-orient +IE9CTI;N
8or widgets that can lay themselves out with either a horiOontal
or vertical orientation, such as scrollars, this option specifies
which orientation should e used. +IE9CTI;N must e either
horiOontal or vertical or an areviation of one of these.
-command C;&&5N+
This command gets e(ecuted when the scrollar is moved. This
option almost always has a value such as .t (view or .t yview,
consisting of the name of a widget and either (view Mif the
scrollar is for horiOontal scrollingN or yview Mfor vertical
scrollingN. 5ll scrollale widgets have (view and yview
commands that take e(actly the additional arguments
appended y the scrollar.
Eample
proc pushButton CD C
set name G.ent getH
.t(t insert end '!ello, 1name.'
D
frame .frm -relief groove
lael .la -te(t '9nter nameJ'
entry .ent
utton .ut -te(t '@ush &e' -command 'pushButton'
frame .te(tarea
te(t .t(t -width ?2 -height >2 0
-yscrollcommand '.srlBy set' -(scrollcommand '.srlB( set'
scrollar .srlBy -command '.t(t yview' -orient v
scrollar .srlB( -command '.t(t (view' -orient h
pack .la -in .frm
pack .ent -in .frm
pack .frm
pack .ut
grid .t(t -in .te(tarea -row > -column >
grid .srlBy -in .te(tarea -row > -column ? -sticky ns
grid .srlB( -in .te(tarea -row ? -column > -sticky ew
pack .te(tarea
Grid
5s you can see I have used )grid) here. Irid is N;T a widget. It is a geometry
manager like pack ut more advanced. *ets take a closer look at the commands -
grid .t(t -in .te(tarea -row > -column >
This line will tell the interpreter to put the widget called ).t(t) in the widget called
).te(tarea)MThat is a frame, rememer,N. It will e put in the first column of the first
row. The elow digram will help you understand.
Column > Column ?
Eow > ).t(t) widget will e here ).srlBy) widget)s place
Eow ? ).srlB() widget)s position.
Some Options
-sticky 4TY*9
This option may e used to position Mor stretchN the widget within its
cell. 4TY*9 is a string that contains Oero or more of the characters n,
s, e or w. 9ach letter refers to a side Mnorth, south, east, or westN
that the slave will 'stick' to. If oth n and s Mor e and wN are
specified, the slave will e stretched to fill the entire height Mor widthN
of its cavity.
-in &54T9E The widget will e put in the &54T9E widget.
-ipad( 5&;/NT
The 5&;/NT specifies how much horiOontal internal padding to leave
on each side of the slaveMsN. This is space is added inside the slaveMsN
order.
-ipady 5&;/NT
The 5&;/NT specifies how much vertical internal padding to leave on
each side of the slaveMsN. ;ptions same as -ipad(
-pad( 5&;/NT
The amount specifies how much horiOontal e(ternal padding to leave
on each side of the slaveMsN, in screen units. 5&;/NT may e a list
of two values to specify padding for left and right separately.
-pady 5&;/NT
The amount specifies how much vertical e(ternal padding to leave on
the top and ottom of the slaveMsN, in screen units. ;ptions same as
-pad(.
-row N
Insert the slave so that it occupies the Nth row in the grid. Eow
numers start with 2. If this option is not supplied, then the slave is
arranged on the same row as the previous slave specified on this call
to grid, or the first unoccupied row if this is the first slave.
-column N
Insert the slave so that it occupies the N)th column in the grid.
;ptions same as -row
-rowspan N
Insert the slave so that it occupies N rows in the grid. The default is
one row.
-columnspan N Insert the slave so that it occupies N columns in the grid.
/sing grid reAuires a it of e(perience - ut if you know !T&* it would help a lot.
The rows and columns are 6ust like those in !T&* tales - although the codes are
very different.
Scale
&akes a slider that can e ad6usted y the user to input a variale.
Some Options
-from N/&.9E 4tarting Numer
-to N/&.9E 9nding Numer
-tickinterval N/&.9E
+etermines the spacing etween numerical tick marks displayed
elow or to the left of the slider.
-varale N5&9
4pecifies the name of a gloal variale to link to the scale.
"henever the value of the variale changes, the scale will
update to reflect this value. "henever the scale is manipulated
interactively, the variale will e modified to reflect the scale)s
new value.
Synta !es"ription Eample
path get Iet the current value of the scale set age G.scl getH
path set !alue Iive the scale a new value. .scl set ?2
Eample
#This function will e e(ecuted when the utton is pushed
proc pushButton CD C
gloal age
set name G.ent getH
.t(t insert end '!ello, 1name.0nYou are 1age years old.'
D
#Iloal Fariales
set age >2
#I/I uilding
frame .frm -relief groove
lael .la -te(t '9nter nameJ'
entry .ent
utton .ut -te(t '@ush &e' -command 'pushButton'
#5ge
scale .scl -lael '5ge J' -orient h -digit > -from >2 -to P2 0
-variale age -tickinterval >2
#Te(t 5rea
frame .te(tarea
te(t .t(t -yscrollcommand '.srlBy set' -(scrollcommand '.srlB( set' 0
-width ?2 -height >2
scrollar .srlBy -command '.t(t yview' -orient v
scrollar .srlB( -command '.t(t (view' -orient h
#Ieometry &anagement
pack .la -in .frm
pack .ent -in .frm
pack .frm
pack .scl
pack .ut
grid .t(t -in .te(tarea -row > -column >
grid .srlBy -in .te(tarea -row > -column ? -sticky ns
grid .srlB( -in .te(tarea -row ? -column > -sticky ew
pack .te(tarea
Now our little e(ample is ecoming more and more like a program. "e have added
the comments to it as it has grown ig and is difficult to understand. Now we have
added a slider with which age can e inputed. You may have also noticed that an
new line )gloal age) is added. It is not the aged of the earth or the age of every
human eing comined. It says that the variale )age) should e taken from the
gloal scope to the scope of the function.
Widgets %: &adio button, Che"' button
Radio button
Eadio utton is an input where any one of many choices &/4T e chosen. If one is
chosen and another utton is clicked, the last chosen will lose its state and the
clicked utton will e chosen. 5 graphic e(ample Min !T&*N is given elow.
Some Options
-command C;&&5N+
4pecifies a Tcl command to associate with the utton. This
command is typically invoked when mouse utton > is released
over the utton window.
-variale F5EI5.*9
4pecifies name of gloal variale to set to indicate whether or
not this utton is selected.
-value F5*/9
4pecifies value to store in the utton)s associated variale
whenever this utton is selected.
Synta !es"ription Eample
path deselect
+eselects the checkutton and sets the associated
variale to its QQoff)) value.
.rdBm
deselect
path select
4elects the checkutton and sets the associated variale
to its QQon)) value.
.rdBm select
Eample
#This function will e e(ected when the utton is pushed
proc pushButton CD C
gloal age gender
set name G.ent getH
.t(t insert end '1name0M1gender0N is 1age years old.'
D
#Iloal Fariales
set age >2
set gender '&ale'
#I/I uilding
frame .frmBname
lael .la -te(t 'NameJ'
entry .ent
#5ge
scale .scl -lael '5ge J' -orient h -digit > -from >2 -to P2 0
-variale age -tickinterval >2
#Iender
frame .gender
lael .llBgender -te(t '4e( '
radioutton .gender.rdBm -te(t '&ale' -variale gender -value '&ale'
radioutton .gender.rdBf -te(t '8emale' -variale gender -value '8emale'
.gender.rdBm select
utton .ut -te(t '@ush &e' -command 'pushButton'
#Te(t 5rea
frame .te(tarea
te(t .t(t -yscrollcommand '.srlBy set' -(scrollcommand '.srlB( set' 0
-width R2 -height >2
scrollar .srlBy -command '.t(t yview' -orient v
scrollar .srlB( -command '.t(t (view' -orient h
#Ieometry &anagement
grid .frmBname -in . -row > -column > -columnspan ?
grid .la -in .frmBname -row > -column >
grid .ent -in .frmBname -row > -column ?
grid .scl -in . -row ? -column > -columnspan ?
grid .gender -in . -row K -column ?
grid .llBgender -in .gender -row > -column >
grid .gender.rdBm -in .gender -row > -column ?
grid .gender.rdBf -in .gender -row > -column K
grid .ut -in . -row R -column > -columnspan ?
grid .t(t -in .te(tarea -row > -column >
grid .srlBy -in .te(tarea -row > -column ? -sticky ns
grid .srlB( -in .te(tarea -row ? -column > -sticky ew
grid .te(tarea -in . -row P -column > -columnspan ?
This time the program is su6ected to even more change - the geometry manager is
fully grid now. There is no instances of pack. You will find this necessary when the
layout ecomes more complicated. I hope you can stay with me in such trying times.
checkbutton
Checkotton is a input with two options - ;ff or ;n - it has to e either one. The
state can e changed y clicking on it. 5n e(ample is shown elow.
check o(
Some Options
-offvalue F5*/9
4pecifies value to store in the utton)s associated variale
whenever this utton is deselected. +efaults to QQ2)).
-onvalue F5*/9
4pecifies value to store in the utton)s associated variale
whenever this utton is selected. +efaults to QQ>)).
-command C;&&5N+
4pecifies a Tcl command to associate with the utton. This
command is typically invoked when mouse utton > is released
over the utton window.
-variale F5E5.*9
4pecifies name of gloal variale to set to indicate whether or
not this utton is selected.
Synta !es"ription Eample
path deselect
+eselects the checkutton and sets the associated variale
to its QQoff)) value.
.chk
deselect
path select
4elects the checkutton and sets the associated variale to
its QQon)) value.
.chk select
path toggle
Toggles the selection state of the utton, redisplaying it and
modifying its associated variale to reflect the new state.
.chk toggle
Eample
#This function will e e(ecuted when the utton is pushed
proc pushButton CD C
gloal age occupied gender
set name G.ent getH
.t(t insert end '1name0M1gender0N is 1age years old and is '
if C 1occupied SS > D C
.t(t insert end 'occupied.'
D else C
.t(t insert end 'unemployed.'
D
D
#Iloal Fariales
set age >2
set occupied >
set gender '&ale'
#I/I uilding
frame .frmBname
lael .la -te(t 'NameJ'
entry .ent
#5ge
scale .scl -lael '5ge J' -orient h -digit > -from >2 -to P2 0
-variale age -tickinterval >2
checkutton .chk -te(t ';ccupied' -variale occupied
#Iender
frame .gender
lael .llBgender -te(t '4e( '
radioutton .gender.rdBm -te(t '&ale' -variale gender -value '&ale'
radioutton .gender.rdBf -te(t '8emale' -variale gender -value '8emale'
.gender.rdBm select
utton .ut -te(t '@ush &e' -command 'pushButton'
#Te(t 5rea
frame .te(tarea
te(t .t(t -yscrollcommand '.srlBy set' -(scrollcommand '.srlB( set' 0
-width R2 -height >2
scrollar .srlBy -command '.t(t yview' -orient v
scrollar .srlB( -command '.t(t (view' -orient h
#Ieometry &anagement
grid .frmBname -in . -row > -column > -columnspan ?
grid .la -in .frmBname -row > -column >
grid .ent -in .frmBname -row > -column ?
grid .scl -in . -row ? -column >
grid .chk -in . -row ? -column ?
grid .gender -in . -row K -column ?
grid .llBgender -in .gender -row > -column >
grid .gender.rdBm -in .gender -row > -column ?
grid .gender.rdBf -in .gender -row > -column K
grid .ut -in . -row R -column > -columnspan ?
grid .t(t -in .te(tarea -row > -column >
grid .srlBy -in .te(tarea -row > -column ? -sticky ns
grid .srlB( -in .te(tarea -row ? -column > -sticky ew
grid .te(tarea -in . -row P -column > -columnspan ?
Widgets (: List bo
list box
5 listo( is a widget that displays a list of strings, one per line. "hen first created, a
new listo( has no elements. 9lements may e added or deleted using widget
commands descried elow.
Some Options
-listvariale
F5EI5.*9
4pecifies the name of a variale. The value of the F5EI5.*9 is a list to
e displayed inside the widgetL if the F5EI5.*9 value changes then
the widget will automatically update itself to reflect the new value.
-selectmode
&;+9
4pecifies one of several styles for manipulating the selection. The
&;+9 may e aritrary, ut the default indings e(pect it to e either
single, rowse, multiple, or e(tendedL the default value is rowse.
Some Commands
Synta !es"ription Eample
path curselection
Eeturns a list containing the
numerical indices of all of the
elements in the listo( that are
currently selected. If there are no
elements selected in the listo( then
an empty string is returned.
set sel G.lst curselectionH
path delete first ?
last?
+eletes one or more elements of the
listo(. 8irst and last are indices
specifying the first and last elements
in the range to delete. If last isn)t
specified it defaults to first, i.e. a
single element is deleted.
.lst delete P
path get first ?last?
If last is omitted, returns the contents
of the listo( element indicated y
first, or an empty string if first refers
to a non-e(istent element. If last is
specified, the command returns a list
whose elements are all of the listo(
elements etween first and last,
inclusive.
.lst get P end
path inde( index
Eeturns the integer inde( value that
corresponds to index. If index is end
the return value is a count of the
numer of elements in the listo(
Mnot the inde( of the last elementN.
.lst inde( P
path insert index ?
element element ...?
Inserts Oero or more new elements in
the list 6ust efore the element given
y index. If index is specified as end
then the new elements are added to
the end of the list. Eeturns an empty
string.
.lst insert end '&e'
path siOe
Eeturns a decimal string indicating
the total numer of elements in the
listo(.
set count G.lst siOeH
path (view
This command is used to Auery and
change the horiOontal position of the
information in the widget)s window.
5nother similar command is yview
.lst (view
Eample
#This function will e e(ected when the utton is pushed
proc pushButton CD C
gloal age occupied gender
set name G.ent getH
.t(t insert end '1name0M1gender0N is 1age years old and is '
if C 1occupied SS > D C L#4ee whether he is employed
set 6oBid G.lst curselectionH L#Iet the no of selected 6os
if C 16oBidSSCD D C L#If there is no 6o
set 6o 'a Non worker.'
D else C
set 6o G.lst get 16oBidH L#Iet the name of the 6o
.t(t insert end 'a 16o.'
D
D else C
.t(t insert end 'unemployed.'
D
D
#-os will e activated only if occupation is enaled
proc activateB6os CD C
gloal occupied
if C 1occupied SS > D C
.lst configure -state normal
D else C
.lst configure -state disale
D
D
#Iloal Fariales
set age >2
set occupied >
set gender '&ale'
#I/I uilding
frame .frmBname
lael .la -te(t 'NameJ'
entry .ent
#5ge
scale .scl -lael '5ge J' -orient v -digit > -from >2 -to P2 0
-variale age -tickinterval >2
#-os
frame .frmB6o
checkutton .chk -te(t ';ccupied' -variale occupied -command 'activateB6os'
.chk deselect
listo( .lst -selectmode single
#5dding 6os
.lst insert end '4tudent' 'Teacher' 'Clerk' '.usiness &an' 0
'&ilitry @ersonal' 'Computer 9(pert' ';thers'
.lst configure -state disale L#+isale 6os
#Iender
frame .gender
lael .llBgender -te(t '4e( '
radioutton .gender.rdBm -te(t '&ale' -variale gender -value '&ale'
radioutton .gender.rdBf -te(t '8emale' -variale gender -value '8emale'
.gender.rdBm select
utton .ut -te(t '@ush &e' -command 'pushButton'
#Te(t 5rea
frame .te(tarea
te(t .t(t -yscrollcommand '.srlBy set' -(scrollcommand '.srlB( set' 0
-width R2 -height >2
scrollar .srlBy -command '.t(t yview' -orient v
scrollar .srlB( -command '.t(t (view' -orient h
#Ieometry &anagement
grid .frmBname -in . -row > -column > -columnspan ?
grid .la -in .frmBname -row > -column >
grid .ent -in .frmBname -row > -column ?
grid .scl -in . -row ? -column >
grid .frmB6o -in . -row ? -column ?
grid .chk -in .frmB6o -row > -column > -sticky w
grid .lst -in .frmB6o -row ? -column >
grid .gender -in . -row K -column > -columnspan ?
grid .llBgender -in .gender -row > -column >
grid .gender.rdBm -in .gender -row > -column ?
grid .gender.rdBf -in .gender -row > -column K
grid .ut -in . -row R -column > -columnspan ?
grid .t(t -in .te(tarea -row > -column >
grid .srlBy -in .te(tarea -row > -column ? -sticky ns
grid .srlB( -in .te(tarea -row ? -column > -sticky ew
grid .te(tarea -in . -row P -column > -columnspan ?
"ow$ ;ur )little) e(ample is a ig Mand utterly pointlessN program now. I am going to
stop )e(ampling) from now on.
This is Auite complicated isn)t it, "hy don)t you run the script and see what a
eautiful script we made. Copy the aove script and paste it in a file called 'info.tcl'
and doule click the file. Foila$ "e are TC*%TT programmers.
4ome e(plaining is necessary. The 6os list will e deactivated until the ;ccupied
activated is off. If it is turned on, the list will e activated. "hen the utton is pushed
the )pushButton) function is called and details of every input field is collected and a
summary is written to the )t(t) field.
+id you notice some comment started with # while others started with L#, /se #
while starting comments in new lines and use L# if a comment is put at the end of a
line. The )L) character tells that the command is finished otherwise the compiler will
think that the comment is a part of the program. This is one of the most common
errors made y Tcl programmers.
Widgets ) : *enubutton, *enu, t'+option*enu
menubutton
5 menuutton is a widget that displays a te(tual string, itmap, or image and is
associated with a menu widget.In normal usage, pressing left-clicking the
menuutton causes the associated menu to e posted 6ust underneath the
menuutton.
Some Options
-direction
+IE9CTI;N
4pecifies where the menu is going to e popup up. aove tries to pop
the menu aove the menuutton. elow tries to pop the menu elow
the menuutton. left tries to pop the menu to the left of the
menuutton. right tries to pop the menu to the right of the menu
utton. flush pops the menu directly over the menuutton.
-menu N5&9
4pecifies the path name of the menu associated with this menuutton.
The menu must e a child of the menuutton.
menu
5 menu is a widget that displays a collection of one-line entries arranged in one or
more columns. There e(ist several different types of entries, each with different
properties. 9ntries of different types may e comined in a single menu. &enu
entries are not the same as entry widgets. In fact, menu entries are not even distinct
widgetsL the entire menu is one widget.
Some Options
-tearoff .;;*95N
This option must have a proper oolean value, which specifies
whether or not the menu should include a tear-off entry at the top.
If so, it will e(ist as entry 2 of the menu and the other entries will
numer starting at >. The default menu indings arrange for the
menu to e torn off when the tear-off entry is invoked.
-title 4TEINI
The string will e used to title the window created when this menu
is torn off. If the title is N/**, then the window will have the title of
the menuutton or the te(t of the cascade item from which this
menu was invoked.
-type ;@TI;N
This option can e one of menuar, tearoff, or normal, and is set
when the menu is created. "hile the string returned y the
configuration dataase will change if this option is changed, this
does not affect the menu widget)s ehavior.
Some Commands
Synta !es"ription
path add t"pe ?option
!alue option !alue ...?
5dd a new entry to the ottom of the menu. The new entry)s
type is given y type and must e one of "as"ade,
"he"'button, "ommand, radiobutton, or separator, or a
uniAue areviation of one of the aove. If additional
arguments are present, they specify any of the following
optionsJ
-accelerator F5*/9 4pecifies a string to display at the right
side of the menu entry. Normally
descries an accelerator keystroke
seAuence that may e typed to invoke
the same function as the menu entry.
This option is not availale for separator
or tear-off entries.
-columnreak F5*/9
"hen this option is Oero, the appears
elow the previous entry. "hen this
option is one, the menu appears at the
top of a new column in the menu
-lael F5*/9
4pecifies a string to display as an
identifying lael in the menu entry. Not
availale for separator or tear-off
entries.
-compound F5*/9
4pecifies whether the menu entry
should display oth an image and te(t,
and if so, where the image should e
placed relative to the te(t. Falid values
for this option are ottom, center, left,
none, right and top.
-image F5*/9
4pecifies an image to display in the
menu instead of a te(t string or itmap
The image must have een created y
some previous invocation of image
create. This option overrides the -lael
and -itmap options ut may e reset
to an empty string to enale a te(tual
or itmap lael to e displayed. This
option is not availale for separator or
tear-off entries.
-underline F5*/9
4pecifies the integer inde( of a
character to underline in the entry. This
option is used to make keyoard
shortcuts. 2 corresponds to the first
character of the te(t displayed in the
entry, > to the ne(t character, and so
on.
path clone
new#athname
&akes a clone of the current menu named new@athName. This
clone is a menu in its own right, ut any changes to the clone
are propagated to the original menu and vice versa.
path delete index1
?index?
+elete all of the menu entries etween inde(> and inde(?
inclusive. If inde(? is omitted then it defaults to inde(>.
5ttempts to delete a tear-off menu entry are ignored Minstead,
you should change the tear;ff option to remove the tear-off
entryN.
path insert inde( t"pe
?option !alue ...?
4ame as the add widget command e(cept that it inserts the
new entry 6ust efore the entry given y inde(, instead of
appending to the end of the menu. The type, option, and value
arguments have the same interpretation as for the add widget
command. It is not possile to insert new menu entries efore
the tear-off entry, if the menu has one.
Eample
proc menuBclicked C no opt D C
tkBmessage.o( -message 0
'You have clicked 1opt.0nThis function is not implanted yet.'
D
#+eclare that there is a menu
menu .mar
. config -menu .mar
#The &ain .uttons
.mar add cascade -lael '8ile' -underline 2 0
-menu Gmenu .mar.file -tearoff 2H
.mar add cascade -lael ';thers' 0
-underline 2 -menu Gmenu .mar.oth -tearoff >H
.mar add cascade -lael '!elp' -underline 2 0
-menu Gmenu .mar.help -tearoff 2H
## 8ile &enu ##
set m .mar.file
1m add command -lael 'New' -underline 2 0
-command C .t(t delete >.2 end D L# 5 new item called New is added.
1m add checkutton -lael ';pen' -underline 2 -command C menuBclicked >
';pen' D
1m add command -lael '4ave' -underline 2 -command C menuBclicked > '4ave' D
1m add separator
1m add command -lael '9(it' -underline > -command e(it
## ;thers &enu ##
set m .mar.oth
1m add cascade -lael 'Insert' -underline 2 -menu Gmenu 1m.mnu -title 'Insert'H
1m.mnu add command -lael 'Name' 0
-command C .t(t insert end 'Name J .inny F 50n'D
1m.mnu add command -lael '"esite' -command C 0
.t(t insert end '"esiteJ httpJ%%www.in-co.com%0n'D
1m.mnu add command -lael '9mail' 0
-command C .t(t insert end '9-&ail J innyva3hotmail.com0n'D
1m add command -lael 'Insert 5ll' -underline U 0
-command C .t(t insert end CName J .inny F 5
"esite J httpJ%%www.in-co.com%
9-&ail J innyva3hotmail.comD
D
## !elp ##
set m .mar.help
1m add command -lael '5out' -command C
.t(t delete >.2 end
.t(t insert end C
5out
----------
This script created to make a menu for a tcl%tk tutorial.
&ade y .inny F 5
"esite J httpJ%%www.in-co.com%
9-&ail J innyva3hotmail.com
D
D
#&aking a te(t area
te(t .t(t -width P2
pack .t(t
Create the main uttons as cascade menus and create the menus as their slaves. 8or
more information see the manual.
tk_optionMenu
&akes a utton, which when clicked on shows a list with availale options. /seful
when user has to make one choice when multiple choices are given. .elow is a
options menu in !T&*. 5 word of caution though - Tcl%Tk)s option menu has a very
different appearance.
$"ntax
tkBoption&enu path !ar%ame !alue ?!alue !alue ...?
Eample,,,
set opt 'Two'
tkBoption&enu .omn opt ';ne' 'Two' 'Three' '8our' '8ive

Potrebbero piacerti anche