Mar-24-2024, 07:40 PM
CPython uses a reference count to determine when objects are no longer used, and their memory can be recycled. If nothing references an object. there is nothing you can do with the object, so it must be unused.
In your program you used a local variable to reference the image returned by tkinter.PhotoImage(). When you left the function (Guess.__init__() for example) all the local variables cease to exist. There is nothing left to reference the image, so the image is deleted and the memory recycled. By changing image from a local function variable to an instance variable your program keeps a reference to the image.
In your program you used a local variable to reference the image returned by tkinter.PhotoImage(). When you left the function (Guess.__init__() for example) all the local variables cease to exist. There is nothing left to reference the image, so the image is deleted and the memory recycled. By changing image from a local function variable to an instance variable your program keeps a reference to the image.