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)

Creating the GUI Form and Adding Widgets

In this chapter, we will develop our first GUI in Python. We will start with the minimum code required to build a running GUI application. Each recipe then adds different widgets to the GUI form.

We will start by using the tkinter GUI toolkit.

tkinter ships with Python. There is no need to install it once you have installed Python version 3.7 or later. The tkinter GUI toolkit enables us to write GUIs with Python.

The old world of the DOS Command Prompt has long been outdated. Some developers still like it for development work. The end user of your program expects a more modern, good-looking GUI.

In this book, you will learn how to develop GUIs using the Python programming language.

By starting with the minimum amount of code, we can see the pattern every GUI written with tkinter and Python follows. First come the import statements, followed by the creation of a tkinter class. We then can call methods and change attributes. At the end, we always call the Windows event loop. Now we can run the code.
We progress from the most simple code, adding more and more functionality with each following recipe, introducing different widget controls and how to change and retrieve attributes.

In the first two recipes, we will show the entire code, consisting of only a few lines of code. In the following recipes, we will only show the code to be added to the previous recipes because, otherwise, the book would get too long, and seeing the same code over and over again is rather boring.

If you don't have the time to type the code yourself, you can download all of the code for the entire book from https://github.com/PacktPublishing/Python-GUI-Programming-Cookbook-Third-Edition.

At the beginning of each chapter, I will show the Python modules that belong to each chapter. I will then reference the different modules that belong to the code shown, studied, and run.

By the end of this chapter, we will have created a working GUI application that consists of labels, buttons, textboxes, comboboxes, check buttons in various states, and radio buttons that change the background color of the GUI.

Here is an overview of the Python modules (ending in a .py extension) for this chapter:

In this chapter, we start creating amazing GUIs using Python 3.7 or later. We will cover the following topics:

  • Creating our first Python GUI
  • Preventing the GUI from being resized
  • Adding a label to the GUI form
  • Creating buttons and changing their text attributes
  • Creating textbox widgets
  • Setting the focus to a widget and disabling widgets
  • Creating combobox widgets
  • Creating a check button with different initial states
  • Using radio button widgets
  • Using scrolled text widgets
  • Adding several widgets in a loop