Menubutton, Menu
Dialogs
Canvas, Message
Some more Widgets - Canvas, Message, Panedwindow, Spinbox
canvas
The canvas widget is a very important widget as all points are addressable graphical drawing area. Canvas widgets implement structured graphics. A canvas displays any number of items, which may be things like rectangles, circles, lines, and text. Items may be manipulated (e.g. moved or re-colored) and commands may be associated with items. So if you don't like the paint program in windows, you can make your own program using this widget.
The command path create type options is used to make different
structures. A few examples are given below. For more information read the excellent manual that comes with Tcl/Tk.
canvas .cns -relief sunken -background "blue"
.cns create polygon 5 100 50 5 150 5 200 100 5 100 \
-joinstyle bevel -fill "red" -outline "white" -width 5
.cns create oval 200 100 300 200 -fill "green"
.cns create oval 100 150 300 100 -fill "white" -width 0
.cns create rectangle 10 150 100 250 -dash {6 4 2 4 2 4}
pack .cns
message
A message is a widget that displays a textual string. Much like the label widget but this can be used to make a multi-line text.
The -justify option specifies how to justify lines of text. Must be one of left, center, or right. Defaults to left. This option works together with the anchor, aspect, padX, padY, and width options to provide a variety of arrangements of the text within the window.
panedwindow
A panedwindow acts like the frame widget - with one notable exception. The borders can be dragged and expended. This widget contains any number of panes, arranged horizontally or vertically, according to the value of the -orient option. Each pane contains one widget, and each pair of panes is separated by a movable sash. Moving a sash can be done by dragging it. This causes the widgets on either side of the sash to be resized.
Some Options| -orient DIRECTION | This option specifies which orientation should be used. DIRECTION must be either horizontal or vertical or an abbreviation of one of these(h or v). |
| -opaqueresize BOOLEAN | Specifies whether panes should be resized as a sash is moved (true), or if resizing should be deferred until the sash is placed (false). |
| -increment NUMBER | When used with -from and -to, the value in the widget will be adjusted by -increment when a spin button is pressed (up adds the value, down subtracts the value). Default 1. |
Example
panedwindow .pnd -orient v -opaqueresize 0
listbox .lst
.lst insert end "Item 1"
.lst insert end "Item 2"
.lst insert end "Item 3"
.lst insert end "Item 4"
.lst insert end "Item 5"
text .txt
.txt insert end {To Hack With It
To Compute...
Or Not To Compute...
That Is The Question...
Whether 'Tis Nobler In The Memory Bank..
To Suffer The Slings And Circuits Of Outrageous Functions...
...Or To Take Up Arms Against A Sea Of..Transistors,
Or Rather Transponders...
Transcondu--
Trans...
Er... Oh, To Hack With It.
}
.pnd add .lst
.pnd add .txt
pack .pnd -fill both -expand 1
spinbox
This is widget is like an entry widget with two buttons to the right of it to increase or decrease the number that is displayed in the entry area. See the below example - in which I tried to create a functional entry widget in HTML. Not an exact picture, but the best I can do in HTML. Click on either of the arrows to the right to increase or decrease the number.
| ^ | |
| v |
Some Options
| -from NUMBER | The starting number. The value in the entry field cannot be reduced below this number. |
| -to NUMBER | NUMBER is the upper limit. The value in the entry field cannot be increased above this number. |
| -increment NUMBER | When used with -from and -to, the value in the widget will be adjusted by -increment when a spin button is pressed (up adds the value, down subtracts the value). Default 1. |
Example:
set number 5
spinbox .spn -from 1 -to 20 -increment 2 -textvariable number
pack .spn

Comments
Thanks for the nice tutorial. I have a question about spinbox. I created two spinboxes in my program and wanted the value in one of the spinboxes to be 1 plus the value in the other. I wonder if there is any way to get them coupled like this so that when one spinbox changes its value, the other spinbox updates its value automatically??
Thanks again
The downside is that although the boxes would be tied together, either one will affect the other. I suppose you could disable one if you needed to ...
I do have a question though, I am writing an application that draws line segments on a Canvas based on the output of several Scales after a button is pressed. New lines are drawn each time the button is pressed, but my problem is that I don't know how to erase the old line segments displayed. I tried using the following:
.cns config -fill "blue"
in the proc that draws the line segments, but if I place it above the line statements, then all line drawing is erased, even the newly drawn lines. If I place the fill statement below the line statements, then the old lines are not repainted.
Does anyone have any ideas?
I think I just have to get out a spend a couple of bucks on a book ...
Once again, Tk is *powerful* and this tutorial got me far enough to realize that Tcl/Tk is the way to go for rapid GUI development!
Kudos and many thanks!
Mike L.
Thank you for the quick reply. I had thought of that, but wanted to avoid it because that will require that I maintain backups for all the old scale variables in order to erase, and for complicated graphics, becomes almost impossible to maintain.
No problem, as I said, this tutorial has already demonstrated the strength of Tk's graphics, and I'm sure that after getting a book (I'm going to the bookstore this afternoon) I *will* know how to accomplish a canvas refresh.
By the way, if you don't don't mind, I would like to give back with some examples, if you'd like. I am a C developer and was learning Java for GUI development, but Tk is much more elegant and concise.
Sincerely,
Mike L.
Mike
My local library and Barnes & Noble did not have even one book on Tcl/Tk, so I have ordered 2 off the web, though I would like to have checked them out first.
Anyhow, I found a site www.dci.clrc.ac.uk/Publications/Cookbook/eu1.html ,
that explained how to clear the graphics. Apparently you had the right idea after all. I believe that Tk stores all the graphic primitives as objects, and executing a 'fill' will not erase them. The trick is to loop through all the children of the canvas and delete them. It ends up being one line of code:
foreach id [ .canvas find all] { .canvas delete $id };
After that, you simply redraw the updated graphics.
a, strong, em, b, i, code, pre, pandbrallowed. Other tags will be shown as code(< will become <). Urls, Line breaks will be auto-formated.