I would like to move a whole tkinter Canvas
with Mouse Click (hold) + Motion of the mouse. I tried with canvas.move
but it doesn't work unfortunately.
How can I scroll in the whole canvas ? (not move each element of the canvas, but rather scroll the displayed area of the canvas)
import Tkinter as Tk
oldx = 0
oldy = 0
def oldxyset(event):
global oldx, oldy
oldx = event.x
oldy = event.y
def callback(event):
# How to move the whole canvas here?
print oldx - event.x, oldy - event.y
root = Tk.Tk()
c = Tk.Canvas(root, width=400, height=400, bg='white')
o = c.create_oval(150, 10, 100, 60, fill='red')
c.pack()
c.bind("<ButtonPress-1>", oldxyset)
c.bind("<B1-Motion>", callback)
root.mainloop()
The canvas has built-in support for scrolling with the mouse, via the
scan_mark
andscan_dragto
methods. The former remembers where you clicked the mouse, and the latter scrolls the window an appropriate amount of pixels.Note: the
gain
attribute tellsscan_moveto
how many pixels to move for each pixel the mouse moves. By default it is 10, so if you want 1:1 correlation between the cursor and the canvas you will need to set this value to 1 (as shown in the example).Here's an example: