Previous
Menubutton, Menu 
Next
Dialogs 
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
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.

Example

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 DIRECTIONThis 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 BOOLEANSpecifies whether panes should be resized as a sash is moved (true), or if resizing should be deferred until the sash is placed (false).
-increment NUMBERWhen 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 NUMBERThe starting number. The value in the entry field cannot be reduced below this number.
-to NUMBERNUMBER is the upper limit. The value in the entry field cannot be increased above this number.
-increment NUMBERWhen 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
Previous
Menubutton, Menu 
Next
Dialogs 

Comments

Anonymous at 09 Aug, 2007 08:12
Hi Bin-Co,

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
Reply to this.
yamex5 at 18 Aug, 2007 09:37
I am new to Tcl and Tk, and this is a sort of hack, but you could use a global variable shared between the two spin boxes.
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 ...
Reply to this.
yamex5 at 18 Aug, 2007 09:56
Hi, I also agree the tutorial is great. Although there are other lessons for Tcl, no one seems to handle Tk at all, so your examples are very appreciated.
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.
Reply to this.
Binny V A at 18 Aug, 2007 10:46
How about drawing another line with the exact specification of the line you want to erase with the background colour? That is, draw a white line over the line you want to erase. This is just a suggestion - I have not used canvas that much - so I don't know if there is a better way.
Reply to this.
at 06 Oct, 2008 04:28
i think this is based on the stacking order, it will be better if u refer the tcl/tk programming book by Brent Welch.
Reply to this.
yamex5 at 18 Aug, 2007 11:58
Hell Binny,
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.
Reply to this.
yamex5 at 18 Aug, 2007 12:10
Oops, Sorry Binny, that's a typo above, it was supposed to be "Hello" not "Hell" :-(
Mike
Reply to this.
yamex5 at 18 Aug, 2007 10:38
Hello Binny,
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.
Reply to this.
Binny V A at 19 Aug, 2007 02:07
Great! By the way, which books did you buy? Also if you have created any small programs that you think are useful for beginners - give them to me - I will be happy to include them at the end.
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed