Is this canvas inside a Size Box?
Your script was not working because you asked the size box to adjust its padding and you do not even have a size box.
There are too many ways to adjust widget size and all of them work run-time. It really depends on what you want to achieve.
For starters you can literally tell the widget to:
But again, there are specific tools for specific jobs.