Problem in scalling............

Chad Netzer cnetzer at
Wed Apr 30 03:23:49 CEST 2003

On Tue, 2003-04-29 at 17:15, perl lover wrote:

> When i scallled
> the canvas stil i got the same value (762*477) if the
> mouse is placed at bottom right of the canvas. Since i
> used 2 as scalling factor for both x,y, I expect
> bottom right value is as half as normal canvas (ie,
> 381,248). How can i get it?

When you scale things in the canvas, you are scaling the objects, not
the canvas itself.  So the mouse coordinates in the canvas do not change
with scaling.  From the Tk documentation for the canvas object:

"Canvases do not support scaling or rotation of the canvas coordinate
system relative to the window coordinate system."

If you want to treat the canvas a a zoomable window, such that you can
transform the coordinate system of the canvas itself, I suppose you
could intercept the canvas scale method, and keep track of all the
scaling for 'ALL' objects, and use that to help compute the zoom factor
for converting mouse coordinates.

Lots of people, inclusing myself, have learned that the Tk Canvas sucks
for doing things like you want to (anything requiring lots of scaling,
rotating, etc.).  In particular, the kinds of transformations you can do
to objects is limited, and those the canvas almost non-existant
(translations only).

You can try using other toolkit canvases (Gtk, Qt, WxWindows?).  You can
also try using OpenGL (which can very easily handle transformations of
it's space, but is a heavyweight rendering library).  The Sketch
software project, written mostly in Python, needed a very powerful and
flexible canvas device, and uses an interface to X-Windows drawing (they
call it Pax) and implements its own primitives; you could take a look at
it, although it is daunting code for a beginner.  It all depends on what
you want to do.

On the other hand, doing things with the Tk canvas, and learning its
limitations and how to deal with them, can be very instructive (and
sometimes very frustrating).


Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)

More information about the Python-list mailing list