Python Tkinter - change the canvas size after inital declaration

You can easily resize to canvas with the .config() command like so:

w.config(width=x height=y)

The x and y represent integers (whole numbers). You can also add on some other customisation attributes like bg (background) to further customise the canvas.

Also, you have created a brand new canvas at the end there so that is why your attributes disappeared. You can fix that by deleting the last 2 lines of code starting with w =.


What you are looking for is the configure option, as is documentered here. Basically, something like this should help, in place of creating a new canvas:

w.config(width=200, height=200)

For reference, the reason why everything got deleted off of the Canvas is because you created a brand new Canvas, with a different size and the same name. If you are going to change properties of an existing object, you must change the existing object, and not overwrite it. Basically you overwrite something if you declare it equal to something else (w=Canvas(...)).

Tags:

Python

Tkinter