pythonware.com products ::: library ::: search ::: daily Python-URL!
   

Concepts

Back   Next   

 Concepts

To be added.

 Items

The Canvas widget supports the following standard items:

  • arc (arc, chord, or pieslice)

  • bitmap (built-in or read from XBM file)

  • image (a BitmapImage or PhotoImage instance)

  • line

  • oval (a circle or an ellipse)

  • polygon

  • rectangle

  • text

  • window

Chords, pieslices, ovals, polygons, and rectangles are drawn as both an outline and an interior, either of which can be made transparent (if you insist, you can make both transparent).

Window items are used to place other Tkinter widgets on top of the canvas; for these items, the Canvas widget simply acts like a geometry manager.

You can also write your own item types in C or C++ and plug them into Tkinter via Python extension modules.

 Coordinate Systems

The Canvas widget uses two coordinate systems; the window coordinate system (with (0, 0) in the upper left corner), and a canvas coordinate system in which the items are drawn. By scrolling the canvas, you can specify which part of the canvas coordinate system to show in the window.

The scrollregion option is used to limit scrolling operations for the canvas. To set this, you can usually use something like:

    canvas.config(scrollregion=canvas.bbox(ALL))

To convert from window coordinates to canvas coordinates, use the canvasx and canvasy methods:

    def callback(event):
        canvas = event.widget
        x = canvas.canvasx(event.x)
        y = canvas.canvasx(event.y)
        print canvas.find_closest(x, y)

 Item Specifiers

The Canvas widget allows you to identify items in several ways. Everywhere a method expects an item specifier, you can use one of the following:

  • item handles

  • tags

  • ALL

  • CURRENT

Item handles are integer values that are used to identify a specific item on the canvas. Tkinter automatically assigns a new handle to each new item created on the canvas. Item handles can be passed to the various canvas methods either as integers or as strings.

Tags are symbolic names attached to items. Tags are ordinary strings, and they can contain anything except whitespace.

An item can have zero or more tags associated with it, and the same tag can be used for more than one item. However, unlike the Text widget, the Canvas widget doesn't allow you to create bindings or otherwise configure tags for which there are no existing items. All such operations are ignored.

You can either specify the tags via an option to the item create method, set them via the itemconfig method, or add them using the addtag_withtag method. The tags option take either a single string, or a tuple of strings.

    item = canvas.create_line(0, 0, 100, 100, tags="uno")
    canvas.itemconfig(item, tags=("one", "two"))
    canvas.addtag_withtag("three", "one")

To get all tags associated with a specific item, use gettags. To get all items having a given tag, use find_withtag.

    >>> print canvas.gettags(item)
    ('one', 'two', 'three')
    >>> print canvas.find_withtag("one")
    (1,)

The Canvas widget also provides two predefined tags:

ALL (or "all") matches all items on the canvas.

CURRENT (or "current") matches the item under the mouse pointer, if any. This can be used inside mouse event bindings to refer to the item that trigged the callback.

 Printing

To be added.

Back   Next