Tk Commands

Ok, this is a list of those TK commands I know. I'm sure there are lots more, but this should be enough to get you started. If you notice any errors or omissions, please e-mail me. Remember that you can get a list of arguments for most commands by simply typing in the command name (ex: button) with no arguments. ?argument? means it's optional. I highly recommend you get the man pages and use them as they have the complete list of options for each command. I'm just going to give a few of the commands I know which are commonly useful, and some of their options. If you think a command should have an option I don't list, chances are it probably does.

Basic Commands: (try these first)
bell
bind
button
canvas
checkbutton
destroy
entry
frame
label
listbox
menu
message
pack
radiobutton
scrollbar
text

bell activates the default sound (bell). Note that this can be used to turn off most screen savers.

bind widget_name event {script} used to activate a Tcl script when the widget_name is activated. See the bind information in Core Tk Rules for more information.

button widget_name option(s) creates a button. Options include -text your_text which will be placed on the face of the button and the standard -width, -height, -back/foreground commands.

canvas widget_name option(s) one of the more interesting features, this creates a canvas you can manipulate graphical objects on. You can specify -height and -width along with -background, -relief (try raised). Once a canvas is packed you can put objects on it using widget_name create shape corners(4) option(s). Try making a canvas and adding % set MyOval [.widget_name create oval 20 20 40 40 -tag node -fill blue]. I may add a section on canvas later, if you're interested in charts, etc. then I'd suggest you play around with it.

checkbutton widget_name option(s) is similar to the radiobutton but it uses a checkbox and can be turned on and off.

destroy widget_name(s) this deletes widget_name(s). You'll have to re-create them from scratch if you make a mistake so be careful. It also kills the children in order so if there's an error partway through, some will still be deleted.

entry widget_name option(s) creates a line of editable (if you want) text. Common options are: -textvariable variable_name which assigns a variable to hold the text being displayed. This not only allows a user to enter their own text but also for you to set a new value to the variable_name which will then be displayed. You'll probably also want to set the -width.

frame widget_name option(s) a container for other widgets. The default is "invisible" but you can specifiy -background and -relief, etc. if you want. frame is key to making a nice window as it lets you arrange things just as you'd like.

label widget_name option(s) displays a string (amount of text) or image which is not editable by the user.

listbox widget_name option(s) list strings, one on each line. Once the listbox is created you can insert or delete entries (ex: widget_name insert end your_string_of_text). Options include the usual plus -selectmode (single, browse (the default), multiple or extended).

menu widget_name option(s) I'm actually not going to try and explain this one, just know that you can make menus and while they're pretty complex, they can do a lot. Pull-down menus, pop-up menus or whatever else you want to do, Tk can do it. If you're interested just look up the man pages at http://www.scriptics.com/man/

message widget_name option(s) is similar to label except that message will extend a textual string across more than one line. Newlines will be added according to words (unless you've got a really huge word or a really small message width).

pack argument(s) widget_name(s) option(s) this is explained in the previous page, just go here. Learn enough to use it, play around and then come back and master how to use frame and pack.

radiobutton widget_name option(s) simply makes a button that the user can select. Usually you'll have a -text label. -variable variable_name -value a_value will assign a_value to the variable variable_name when the widget_name is clicked, otherwise variable_name will be blank. For kicks try typing widget_name flash

scrollbar widget_name option(s) these are those scrolling bars you have at the edge of your screen when the window you're using is too small to contain all the data (like right now). These are sort of involved, so I'm not going to explain them now, just be aware that they exist and if you find some code that uses them you'll probably be able to figure it out. Again, the man pages have the information in detail.

text widget_name option(s) this is like message except that you can create multiple (or a single) lines of editable text. Once created, you can insert text to the widget_name by typing widget_name insert end the_text_to_be_inserted.

 

That's enough commands to get you started (plus a few hints of things you might want to look into later). Once you get done programming and want to write a program you'll probably want to keep a copy of your code so you can run it again after you close Tcl/Tk. Just copy and paste the code into Notepad or a word processor which can save things to plain ASCII. I'm not sure how it works on Macintosh, but on Windows95 if you save it in ASCII form as MyFile.tcl then Tcl/Tk will automatically compile and run it (assuming there are no bugs and it all works) if you just double click on it! This will only work on machines that have Tcl/Tk installed on them, but if you're running Tclsh and/or Wish then you should be fine. Every time I start a new program I'll start writing it directly in Notepad, and just copy and paste the procedures into the wish Console as I go to test for bugs. This is a little scattered, and there are probably programs out there which will let you do this more efficiently, but it does the trick.

What's this? We're done?? That's right, you've graduated! You can officially go out and make all sorts of exciting programs. The rest of this tutorial will be exercises to help you get the commands down and some examples of programs I have written. I may eventually try to add some notes about more advanced commands and ideas (pattern matching, for example), but you can always learn those on your own and this is really just supposed to get you moving. So get moving already!

 

next page
contents
previous page


Copyright 1998 by Chris Palmer
Mail all comments to: Ardenstone@Ardenstone.com
Visit the rest of my Senior Seminar
or my homepage.