Star123 Star123 - 1 year ago 152
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 Source

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()


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 = 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).