Star123 Star123 - 2 months ago 27
Python Question

Canvas Update Tkinter Python

I wrote a method to create a canvas

But I see that Canvas is not updated and the Gui elements are not updated

Please guide here as i want to implement the New Game functionality

Answer

Removing all references to a Tkinter widget (like a Canvas) doesn't necessarily delete that object (or its child widgets) from a Tkinter application. You can test this with a small script:

import tkinter as tk
root = tk.Tk()
def f():
    tk.Label(root, text='hi').pack()

f()

No reference to that Label is ever saved, and it's local to a function scope anyway, and yet it persists. If you want to get rid of a Tkinter widget, use its destroy method.

self.canvas.destroy()
self.canvas = tk.Canvas(self, width=2000, height=800)
...

Note that it's still good practice to keep references to your Tkinter objects so that you can work with them (like calling destroy() on them, for example), and some Tkinter objects actually are discarded when there are no more references to them, like image objects (e.g. as used by Canvas.create_image and Label).