1

I'm attempting to adapt martineau's excellent answer on how to use tkinter to highlight and select an area of an image with a mouse. My goal is to modify the code to support huge images, such as 10000x6000 pixels or more!

This is my first attempt at using tkinter, and it's much more challenging than expected.

The three options which I've considered and experimented with are:

  1. Load the image, then scale the image to the size available in the window before placing it on the canvas. This in turn requires that I know the scale factor, so I can reverse the scaling when operating on the original image.
  2. Add scrollbars to the side/button on the visible part of the image in the canvas. (My preferred option).
  3. Don't add scrollbars, but use the cursor keys to scroll the visible part of the image in the canvas.

I've tried to do all of them, but ran into various kinds of trouble:

  1. I used root.state('zoomed') to maximize the window, then placed a button to top of the window using tk.Button(...).pack(fill=tk.X) before placing the canvas as in the linked code using self.canvas = tk.Canvas(...). At this time I discovered two issues: It turns out that I can't maximize the canvas to the remaining space in the window using self.canvas.pack(fill=tk.BOTH) (it only expands the width). Another more crucial issue was that I could not read the actual width/height value that the canvas was expanded to, (or get there available space in the window before placing the canvas), and without these values, I can't calculate the scaling factor. :-(
  2. I tried to follow various methods of adding scrollbars to the packed canvas but they all, with one exception, required to use the grid method and I quickly found that pack and grid methods were conflicting methods. After several attempts at rewriting the code to use the grid method, I gave up. The place method seemed promising, except that it will require me to size the canvas manually and then place the scrollbars at calculated offsets to the canvas... and all of this required that I have the same values that were needed to calculate the scaling factor above. :-(
  3. I didn't get very far with this, as it seems I can't get (keyboard?) binds to work at all (On windows with Python 3.10.6). Shouldn't something like self.canvas.bind("<Right>", lambda event: print("Ping")) followed by self.canvas.focus_set() (perhaps with root instead of the canvas) work?

I'm sure I'm overlooking something basic, but after a few evenings looking at this I've gotten nowhere. :-(

I hope some of you can provide help and/or pointers allowing me to get further.

Ohh, for completeness, I'm trying to hack together a small tool that does the following:

  1. On startup requests and load an image file. (Adding a button to perform the load/re-load is nice to have, but not strictly needed. )
  2. Display the image file and allow an area of the image to be selected with the mouse.
  3. Perform some software analysis on the selected part of the image when a button is pressed.
fsteff
  • 543
  • 5
  • 19
  • 1
    If you want to access the entire image, try including `scrollregion` in your `Canvas` declaration then add your scrollbars. For example, `Canvas(root, scrollregion = "0 0 10000 6000")' would produce a sufficiently large area. – Derek Aug 07 '22 at 23:04

0 Answers0