Canvas geometry

Fredrik Lundh fredrik at effbot.org
Thu Jan 18 16:37:59 EST 2001


Hans Kristian Ruud wrote:
> I try to retrieve the dimensions of a canvas in the following
> manner:
>
>       h = eval( self.canvas.cget('height')  )
>       w = eval( self.canvas).cget('width')  )

Typos aside, this gives you the configured size.  Whether or
not this is the same as the actual size depends on the widget
and the geometry manager (it usually isn't the same).

(btw, note that "eval" isn't exactly the most efficient way to
convert a string containing an integer to an integer value...
try using "int" instead)

To get the actual size, use winfo_height/width:

    h = self.canvas.winfo_height()
    w = self.canvas.winfo_width()

For a couple of caveats, see
http://www.pythonware.com/library/tkinter/introduction/basic-widget-methods.htm
=> Window Related Information => winfo_width, winfo_height

To track changes to the canvas size, bind the <Configure>
event (see Steven's post for an example).

Hope this helps!

Cheers /F

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->






More information about the Python-list mailing list