How it works...

In tkinter, adding space horizontally and vertically is done by using built-in properties named padx and pady. These can be used to add space around many widgets, improving horizontal and vertical alignments, respectively. We hardcoded 20 pixels of space to the left and right of LabelFrame, and we added 40 pixels to the top and bottom of the frame. Now our LabelFrame stands out better than it did before.

The preceding screenshot only shows the relevant change.

We can use a loop to add space around the labels contained within LabelFrame:

GUI_add_padding_loop.py

Now, the labels within the LabelFrame widget have some space around them too:

The grid_configure() function enables us to modify the UI elements before the main loop displays them. So, instead of hardcoding values when we first create a widget, we can work on our layout and then arrange spacing towards the end of our file, just before the GUI is created. This is a neat technique to know.

The winfo_children() function returns a list of all the children belonging to the buttons_frame variable. This enables us to loop through them and assign the padding to each label.

One thing to notice is that the spacing to the right of the labels is not really visible. This is because the title of LabelFrame is longer than the names of the labels. We can experiment with this by making the label names longer.

Consider the following changes in code as in GUI_long_label.py:

Now, our GUI looks as shown in the following screenshot. Note how there is now some space added to the right of the long label next to the dots. The last dot does not touch LabelFrame, which it otherwise would have, without the added space:

We can also remove the name of LabelFrame to see the effect padx has on the position of our labels:

GUI_LabelFrame_no_name.py

By setting the text property to an empty string, we remove the name that was previously displayed for LabelFrame:

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

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