[Tkinter-discuss] tkinter coordinates, conversion

Michael Lange klappnase at web.de
Sun Mar 30 19:22:07 CEST 2008

On Sun, 30 Mar 2008 15:37:35 +0200
Fredrik Lundh <fredrik at pythonware.com> wrote:

> Ron Provost wrote:
> > Is there any way in tkinter to convert between coordinate systems?  
> > Specifically, I'm refering to the canvas.  I'm getting x and y's back in 
> > mouse events and I would like to convert them back to inches 'i', 
> > centemeters 'c', millimeters 'm' or points 'p'.
> use winfo_screenheight() and winfo_screenmmheight() to determine the 
> pixel size, and use that to calculate "real-life" values.  e.g.
> 	pixel_size =   w.winfo_screenmmheight() / w.winfo_screenheight()
> 	...
> 	x = x * pixel_size
> 	print x, "mm"
> 	print x/10.0, "cm"
> 	print x/25.4, "inches"
> 	print 72*x/25.4, "points"

You should be aware however that the values reported by winfo_screenmmheight()
and winfo_screenmmwidth() may be incorrect; I think this is because tk assumes
a screen dpi value of 72 which may be different from what the system actually
uses. For example my debian etch system uses a dpi value of 96 by default, so when I call


tk returns 203 where the actual value should be 203 * (96 / 72) = 270 .

You can query the dpi value in use with 


which returns on my box: 95.976383763837632

You can even force tk to use a dpi value different from the system default:

    w.tk.call('tk', 'scaling', '-displayof', '.', your_dpi_value / 72.0)

which may be useful if you want to make sure that for example some Canvas items
are drawn in an exact mm-size onto the user's screen (however be careful, doing
so might mess up the application's visual appearance on some systems, especially
using point-sized fonts should be avoided).

Hope this helps


More information about the Tkinter-discuss mailing list