Skip to content
Advertisement

How to scroll a tkinter canvas to an absolute position?

I’m using Python and tkinter. I have a Canvas widget that will display just one image. Most times the image will be larger than the canvas dimensions, but sometimes it will be smaller. Let’s just focus on the first case (image larger than canvas).

I want to scroll the canvas to an absolute position that I have already calculated (in pixels). How can I do that?

Advertisement

Answer

After trying for around half hour, I got another solution that seems better:

self.canvas.xview_moveto(float(scroll_x+1)/img_width)
self.canvas.yview_moveto(float(scroll_y+1)/img_height)
  • img_width and img_height are the dimensions of the image. In other words, they are the full scrollable area.

  • scroll_x and scroll_y are the coordinates of the desired top-left corner.

  • +1 is a magic value to make it work precisely (but should be applied only if scroll_x/y is non-negative)

  • Note that the current widget dimension is not needed, only the dimension of the contents.

This solution works very well even if the image is smaller than the widget size (and thus the scroll_x/y can be negative).

EDIT: improved version:

offset_x = +1 if scroll_x >= 0 else 0
offset_y = +1 if scroll_y >= 0 else 0
self.canvas.xview_moveto(float(scroll_x + offset_x)/new_width)
self.canvas.yview_moveto(float(scroll_y + offset_y)/new_height)
Advertisement