Trouble centering tcl/tk program when using grid

tcl, tk-toolkit

Solution

`[. cget -width]` and `[. cget -height]` return the explicitly configured width/height of the window. For most widgets in Tk you don't explicitly configure the dimensions. Instead, the widgets have a "natural" width/height based on the contents of the widget. A label, for example, is just wide/tall enough to accommodate the text in it. A toplevel window is just big enough to fit all the widgets inside of it.

To get the actual dimensions of the window, you should use `[winfo width .]` and `[winfo height .]`. Make sure you do that after the window and all of the widgets inside it have been mapped (usually that means doing an `[update]` call before the `[winfo ...]` calls). Otherwise the geometry manager may not have updated the dimensions of the widgets/window yet.

Problem

The reason I'm doing this is because I'm trying to set the window in the centre of the screen. I'm using grid not pack and I have read about using `wm grid` but don't understand how to set it. I also don't understand why `. cget -width` is returning `0` I thought grid was supposed to set the size when not given an option? ``` set width [. cget -width] set height [. cget -height] puts $height puts $width set x [expr { ( [winfo vrootwidth .] - $width ) / 2 }] set y [expr { ( [winfo vrootheight .] - $height ) / 2 }] wm title . "a3q2" wm geometry . ${width}x${height}+${x}+${y} ``` What am I doing wrong this time? P.S homework I don't just want a code post. Thanks

Original source