How it works…

The first label is placed with the relwidth and relheight options set to 0.25, which means that its width and height are 25% of its parent container. By default, widgets are placed at the x=0 and y=0 positions and aligned to north-west, that is, the top-left corner of the screen.

The second label is placed at the absolute position—x=100and aligned to the top border with the anchor option set to the tk.N (north) constant. Here, we also specified an absolute size with width and height.

The third label is centered on the window using the relative positioning and setting the anchor to tk.CENTER. Remember that a value of 0.5 for relx and relwidth means half of the parent's width and a value of 0.5 for rely, and relheight means half of the parent's height.

The fourth label is placed on top of label_c by passing it as the in_ argument (note that Tkinter suffixes it with an underscore because in is a reserved keyword). When using in_, you might notice that the alignment is not geometrically exact. In our example, we had to add an offset of 2 pixels in each direction to perfectly overlap the right-bottom corner of label_c.

Finally, the fifth label uses absolute positioning and relative size. As you may have already noticed, these dimensions can be easily switched since we assume a parent container of 200 x 200 pixels; however, only relative weights will work as expected if the main window is resized. You can test this behavior by resizing the window.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset