Tcl/Tk: 'moveto' position is off?

Viewed 54

This is with Tcl/Tk-8.6.12+dfsg-1 (as packaged in Debian/sid).

I'm trying to position some element on a Tcl/Tk canvas.

Traditionally, there was the canvas move command, which would move tagged items relative to their current position. As of Tcl/Tk-8.6, there's also a canvas moveto command, which should move tagged items to an absolute position.

However, for whatever reasons the two do not behave the same.

Consider the following minimal script (where I've put the output of puts in comments):

#!/usr/bin/env wish
canvas .c -width 100 -height 100 -background white
pack .c

.c create line 0 0 0 0 -tags tag
puts [.c coord tag]
# 0.0 0.0 0.0 0.0

.c move tag 100 100
puts [.c coord tag]
# 100.0 100.0 100.0 100.0

.c move tag -100 -100
puts [.c coord tag]
# 0.0 0.0 0.0 0.0

.c moveto tag 100 100
puts [.c coord tag]
# 102.0 102.0 102.0 102.0

As you can see, if I position an item at +0+0, its bounding box will be where i expect it. If I then do a relative move to a new position, e.g. +100+100, the item is there as it should be. I can also do a relative move back, and the item appears at the origin.

However, if I do an absolute move to the position +100+100, the item will really show up at +102+102. For my use-case this offset by 2 pixels in each dimension is not tolerable (as items are going to be positioned pixel-accurately)

What's going on here?

What's the use-case of moveto if it doesn't actually move the item to the requested position?

(sidenote: what is the correct tag for Tcl/Tk questions? why is tk an alias for tkinter, and tcltk for questions about an R-module?)

EDIT: i only observe this behaviour with a line item. If i instead use .c create rectangle 0 0 0 0 -tags tag the corrdinates are correct...

1 Answers

Too long for a comment. The Tcl Tk documentation states that:

All coordinates related to canvases are stored as floating-point numbers. Coordinates and distances are specified in screen units, which are floating-point numbers optionally followed by one of several letters. If no letter is supplied then the distance is in pixels. If the letter is m then the distance is in millimeters on the screen; if it is c then the distance is in centimeters; i means inches, and p means printers points (1/72 inch). Larger y-coordinates refer to points lower on the screen; larger x-coordinates refer to points farther to the right. Coordinates can be specified either as an even number of parameters, or as a single list parameter containing an even number of x and y coordinate values.

I'm not entirly sure about this, but you may consider this. Tcl/Tk states on its own that it is +-1 pixel accurate. I believe this is due dpi scaling and results are threat as whole numbers while it actually is a float and it rounds up sometime.

Related