1

I'd like to know if there is a way to set a limit to the .scan_dragto method in tkinter.

In my current project, using .scan_dragto correctly drags my canvas widget. But I can drag it endlessly. Is there any way to limit this drag?

I've already tried to set a max size to my canvas using maxsize, but it didn't work.

This part of my code is something like this one below.

import tkinter as tk

root = tk.Tk()

img = tk.Canvas(root, bg = "white", width = 1100, height = 600)
img.grid()

#Some canvas objects
#...

img.bind('<ButtonPress-1>', lambda event: img.scan_mark(event.x, event.y))
img.bind("<B1-Motion>", lambda event: img.scan_dragto(event.x, event.y, gain=1))

root.mainloop()
  • Please [edit] your question to include a [mcve] that illustrates what you've tried. Given that you're the one providing arguments to `scan_dragto`, it stands to reason you can limit those arguments. Plus, the `scrollregion` method lets you define the extent to which a canvas can scroll. – Bryan Oakley Sep 01 '20 at 21:37
  • Here's an example of a canvas that can be scrolled, but doesn't allow unlimited scrolling: https://stackoverflow.com/a/20646727/7432 – Bryan Oakley Sep 01 '20 at 21:40
  • Sorry, this was my first questions. Done it – Rafael Hideki Cardoso Ishida Sep 03 '20 at 02:41

1 Answers1

1

As @Bryan Oakley pointed in his comment, using scrollregion solved my problem. The syntax is shown below:

import tkinter as tk

root = tk.Tk()

img = tk.Canvas(root, bg = "white", width = 1100, height = 600)
img.grid()
img.configure(scrollregion=(0, 0, 1000, 800))

#Some canvas objects
#...

img.bind('<ButtonPress-1>', lambda event: img.scan_mark(event.x, event.y))
img.bind("<B1-Motion>", lambda event: img.scan_dragto(event.x, event.y, gain=1))

root.mainloop()

The line img.configure(scrollregion=(0, 0, 1000, 800)) limits the horizontal scroll from 0 to 1000 (in canvas coordinates) and the vertical scroll from 0 to 800.