Tkinter\'s canvas widget has built-in features to:
move/pan the canvas (for example with Click + Drag) with canvas.scan_mark and canvas.s
(The question TITLE doesn't indicate that it's focused on bitmaps. I add an answer here for those who were interested in basic zoom/pan support for canvas, and got here by a search engine)
The fundamental mechanism to support zoom (with wheel) and move/pan (with left-button drag) is as follows:
from tkinter import ALL, EventType
canvas.bind("", do_zoom)
canvas.bind('', lambda event: canvas.scan_mark(event.x, event.y))
canvas.bind("", lambda event: canvas.scan_dragto(event.x, event.y, gain=1))
def do_zoom(event):
factor = 1.001 ** event.delta
canvas.scale(ALL, event.x, event.y, factor, factor)
Simple extension: support zooming of each axis individually, by looking at the state of Ctrl and Shift, as follows:
def do_zoom(event):
factor = 1.001 ** event.delta
is_shift = event.state & (1 << 0) != 0
is_ctrl = event.state & (1 << 2) != 0
canvas.scale(ALL, event.x, event.y,
factor if not is_shift else 1.0,
factor if not is_ctrl else 1.0)