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. Alternatively, you can download the code from https://github.com/PacktPublishing/Python-GUI-Programming-Cookbook-Third-Edition/.
How to do it...
Here are the steps to prevent the GUI from being resized:
- Start with the module from the previous recipe and save it as Gui_not_resizable.py.
- Use the Tk instance variable, win, to call the resizable method:
win.resizable(False, False)
Here is the code to prevent the GUI from being resized (GUI_not_resizable.py):
- Run the code. Running the code creates this GUI:
Let's go behind the scenes to understand the code better.
How it works...
Line 18 prevents the Python GUI from being resized.
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.
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 our GUI can make it not look the way we want it to look. We will add widgets to our GUI in the next recipes, starting with Adding a label to the GUI form.
We also added comments to our code in preparation for the recipes contained in this book.
We've successfully learned how to prevent the GUI from being resized. Now, let's move on to the next recipe.