0

I'm trying to zoom in & out, in a canvas. But when I zoom, the canvas still empty. Here is my code

teste.py

from tkinter import *
from observable import *

class Model(Observable):

    def __init__(self):
        Observable.__init__(self)

        self.scale = 1

        File = "image.png"

        self.orig_img = PhotoImage(file = File)

    def zoom(self,event): # window binded width "<MouseWheel>"
        if event.delta > 0:
            self.scale += 1
        elif event.delta < 0:
            self.scale -= 1

        self._docallUpdate(self)

class View(Canvas):

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

    def update(self, model):
        self.delete("all")
        if model.scale > 0:
            img = model.orig_img.zoom(model.scale)
        elif model.scale < 0:
            img = model.orig_img.subsample(abs(model.scale))
        else: img = model.orig_img
        print(img)

        self.create_image(0, 0, image= img, anchor = "nw")

Observers.py

class Observable:

    def __init__(self):
        self.observers = {}

    def addObserver(self, ob):
        self.observers[ob] = 1

    def delObserver(self, element):
        del self.observers[element]

    def _docallUpdate(self, mod):
        for ob in self.observers:
             ob.update(mod)
aL_eX
  • 1,453
  • 2
  • 15
  • 30
icetom 54
  • 63
  • 7
  • 2
    Please edit your question to copy/paste your code and share with us the error messages you get from it – Billal Begueradj Dec 10 '17 at 11:18
  • @BillalBEGUERADJ The code is a screenshot. And the problem is that there are no error messages, just when i use canvas.create_image, the canvas still empty – icetom 54 Dec 10 '17 at 11:31
  • @BillalBEGUERADJ i found the problem : in the class View in update replace every "img" by "self.img" but could you explain me why ? – icetom 54 Dec 10 '17 at 13:18
  • You need a _global_ reference to images or they get garbage collected, aka removed from memory. Learn more in [this answer](https://stackoverflow.com/q/16424091/7032856). `self.img` is a global reference whereas `img` is a _local_ variable, aka only available under `view` method. – Nae Dec 10 '17 at 13:57

0 Answers0