By default, a GUI created using tkinter can be resized. This is not always ideal. The widgets we place onto our GUI forms might end up being resized in an improper way, so in this recipe, we will learn how to prevent our GUI from being resized by the user of our GUI application.
Preventing the GUI from being resized
Getting ready
This recipe extends the previous one, Creating our first Python GUI, so one requirement is to have typed the first recipe yourself into a project of your own, or download the code from https://github.com/PacktPublishing/Python-GUI-Programming-Cookbook-Second-Edition/.
How to do it...
We are preventing the GUI from being resized, look at:
GUI_not_resizable.py
Running the code creates this GUI:
How it works...
Line 18 prevents the Python GUI from being resized.
Running this code will result in a GUI similar to the one we created in the first recipe. However, the user can no longer resize it. Also, note how the maximize button in the toolbar of the window is grayed out.
Why is this important? Because once we add widgets to our form, resizing can make our GUI look not as good as we want it to be. We will add widgets to our GUI in the next recipes.
The resizable() method is of the Tk() class, and by passing in (False, False), we prevent the GUI from being resized. We can disable both the x and y dimensions of the GUI from being resized, or we can enable one or both dimensions by passing in True or any number other than zero. (True, False) would enable the x-dimension but prevent the y-dimension from being resized.
We also added comments to our code in preparation for the recipes contained in this book.