'Python tkinker - scrolling problem with scrollbars

  1. I would like to do scrolling in my photo editing program. But something is not working for me. The code was in theory based on one of the stackoverflow entries and I can't identify the problem. I have tried to remake it in various ways. _inbound_to_mousewheel prints nicely and so does _un_to_mousewheel. The one responsible directly for scrolling doesn't even print.
  2. Is it possible to do a zoom in such a project, and if not, does it need to be rebuilt?

Main window file:

import sys
import tkinter as tk
from tkinter import filedialog, Frame
from PIL import ImageTk, Image
from gui.menu_bar import MenuBar
from gui.workspace import Workspace


def close_all_windows():
    sys.exit()


class MainForm(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("myPhotoEditor by Sebastian Kąkolewski")
        self.geometry("1000x850")
        self.protocol("WM_DELETE_WINDOW", close_all_windows)

        self.menu_bar = MenuBar(self)
        self.config(menu=self.menu_bar)

        # region Workspace
        self.workspace_frame = Frame(self)
        self.workspace_frame.pack(expand=True, fill="both", side="left")

        self.workspace = Workspace(self.workspace_frame)
        self.workspace.pack(fill="both", expand=True)

        file_url = filedialog.askopenfilename()
        img = Image.open(file_url)
        self.workspace.image = ImageTk.PhotoImage(img)
        self.workspace.create_image(0, 0, image=self.workspace.image, anchor='nw')

        self.workspace_frame.bind('<Enter>', self._bound_to_mousewheel)
        self.workspace_frame.bind('<Leave>', self._unbound_to_mousewheel)
        # endregion

    def _bound_to_mousewheel(self, event):
        print("_bound_to_mousewheel")
        self.workspace_frame.bind_all("<MouseWheel>", self._on_mousewheel)

    def _unbound_to_mousewheel(self, event):
        print("_unbound_to_mousewheel")
        self.workspace_frame.unbind_all("<MouseWheel>")

    def _on_mousewheel(self, event):
        print("_on_mousewheel")
        self.workspace_frame.yview_scroll(int(-1 * (event.delta / 120)), "units")

Workspace file:

from tkinter import Canvas, Scrollbar


class Workspace(Canvas):

def __init__(self, master=None):
    Canvas.__init__(self, master)
    self.master = master

    workspace_hbar = Scrollbar(self.master, orient="horizontal")
    workspace_hbar.pack(side="bottom", fill="x")
    workspace_hbar.config(command=self.xview)
    workspace_vbar = Scrollbar(self.master, orient="vertical")
    workspace_vbar.pack(side="right", fill="y")
    workspace_vbar.config(command=self.yview)

    self.config(xscrollcommand=workspace_hbar.set, yscrollcommand=workspace_vbar.set)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source