- What is Tk
- What makes it unique
Hints for beginners (things that may not be obvious if you come to Tk from the wrong community):The window concept
- Every rectangular unit (in particular: the various widgets) on the screen is called a window. Windows as in "document window" is a toplevel.
- Windows (toplevels excepted) do not show on screen until they have become mapped. This happen when they are assigned a geometry manager, such as pack or grid.
- Every window has a unique name, which is also the name of a command. This command -- the "widget command" -- is used to control the window: change contents, set options, change view, etc.
- Windows are created and destroyed in a hierarchy. This hierarchy is visible in the window names: removing the last period (.) and all that follows it from a window name gets you the name of the parent window. (Exception: the root window is called "." (one period) rather than "" (empty string) as this rule would imply.) All window names start with a period.
% canvas my_canvas -width 100 -height 100 bad window path name "my_canvas"my_canvas can't be the name of a window, since it doesn't begin with a period (.).
% canvas .my_canvas -width 100 -height 100 .my_canvasMuch better. Now that we have a 100x100 pixels canvas, let's put something on it, e.g. a thick line from (10,10) to (90,90).
% .my_canvas create line {10 10 90 90} -width 10 1The line is item 1 on this canvas, but strangely enough nothing shows up on the screen (Wish's window is as empty as when we began). Even worse, there seems to be some confusion about how large the canvas is:
% winfo width .my_canvas 1 % .my_canvas cget -width 100The reason for this is found in the following:
% winfo ismapped .my_canvas 0The canvas doesn't appear on screen because it isn't mapped yet. pack can do that for us:
% pack .my_canvasBam! A thick line appears in wish's window (that might also resize because of this). winfo returns data more in line with what one might expect:
% winfo ismapped .my_canvas 1 % winfo width .my_canvas 106
Some example programs: