Book Image

Python GUI Programming Cookbook - Third Edition

By : Burkhard Meier
Book Image

Python GUI Programming Cookbook - Third Edition

By: Burkhard Meier

Overview of this book

Python is a multi-domain, interpreted programming language that is easy to learn and implement. With its wide support for frameworks to develop GUIs, you can build interactive and beautiful GUI-based applications easily using Python. This third edition of Python GUI Programming Cookbook follows a task-based approach to help you create effective GUIs with the smallest amount of code. Every recipe in this book builds upon the last to create an entire, real-life GUI application. These recipes also help you solve problems that you might encounter while developing GUIs. This book mainly focuses on using Python’s built-in tkinter GUI framework. You'll learn how to create GUIs in Python using simple programming styles and object-oriented programming (OOP). As you add more widgets and expand your GUI, you will learn how to connect to networks, databases, and graphical libraries that greatly enhance the functionality of your GUI. You’ll also learn how to use threading to ensure that your GUI doesn't become unresponsive. Toward the end, you’ll learn about the versatile PyQt GUI framework, which comes along with its own visual editor that allows you to design GUIs using drag and drop features. By the end of the book, you’ll be an expert in designing Python GUIs and be able to develop a variety of GUI applications with ease.
Table of Contents (13 chapters)

Preventing the GUI from being resized

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.

Getting ready

How to do it...

Here are the steps to prevent the GUI from being resized:

  1. Start with the module from the previous recipe and save it as Gui_not_resizable.py.
  2. 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):

  1. 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.

In visual programming IDEs such as Visual Studio .NET, C# programmers often do not think of preventing the user from resizing the GUI they developed in this language. This creates inferior GUIs. Adding this one line of Python code can make our users appreciate our GUI.

We've successfully learned how to prevent the GUI from being resized. Now, let's move on to the next recipe.