Reader small image

You're reading from  Python GUI Programming Cookbook. - Third Edition

Product typeBook
Published inOct 2019
Reading LevelIntermediate
Publisher
ISBN-139781838827540
Edition3rd Edition
Languages
Right arrow
Author (1)
Burkhard Meier
Burkhard Meier
author image
Burkhard Meier

Burkhard Meier is a professional software test automation designer, developer, and analyst. He has more than 17 years' professional experience working for several software companies in California, USA. He is the author of Python GUI Programming Cookbook, First and Second Edition. This book is also available as a Packt video course. He is also the author of the Python Projects Packt video course. In his professional career, he developed advanced in-house testing frameworks written in Python 3. He also developed advanced test automation GUIs in Python, which highly increased the productivity of the software development testing team. When not dreaming in Python code, he reads programming books about design, likes to go for long walks, and reads classical poetry.
Read more about Burkhard Meier

Right arrow

Preface

In the third edition of this book, we will explore the beautiful world of graphical user interfaces (GUIs) using the Python programming language. We will be using the latest version of Python 3. All of the recipes from the first and second editions are included in this edition, except for the outdated OpenGL library, which is not very Pythonic, after all. We have added an entirely new chapter to the third edition, and we have dramatically changed the style of this third edition to give it more of a cookbook format. By doing so, hopefully, it is easier to apply the recipes to real-world programming situations, providing tested and working solutions.

This is a programming cookbook. Every chapter is self-contained and explains a certain programming solution. We will start very simply, yet throughout the course of this book, we will build a working application written in Python 3. Each recipe will extend the building of this application. Along the way, we will talk about networks, queues, databases, the PyQt5 graphical library, and many more technologies. We will apply design patterns and use best practices.

The book assumes that you have some experience of using the Python programming language, but that is not really required to successfully use this book. This book can also be used as an introduction to the Python programming language, if, and only if, you are dedicated in your desire to become a Pythonic programmer.

If you are an experienced developer in any other language, you will have a fun time extending your professional toolbox by adding the ability to write GUIs using Python to your toolbox.

Who this book is for

This book is for programmers who wish to create a GUI. You might be surprised by what we can achieve by creating beautiful, functional, and powerful GUIs using the Python programming language. Python is a wonderful, intuitive programming language, and is very easy to learn.

I invite you to start on this journey now. It will be a lot of fun!

What this book covers

Chapter 1, Creating the GUI Form and Adding Widgets, explains how to 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.

Chapter 2, Layout Management, explores how to arrange widgets to create our Python GUI. The grid layout manager is one of the most important layout tools built into tkinter that we will be using.

Chapter 3, Look and Feel Customization, offers several examples of how to create a GUI with good look and feel. On a practical level, we will add functionality to the Help | About menu item that we created in one of the recipes.

Chapter 4, Data and Classes, discusses saving the data our GUI displays. We will start using object-oriented programming (OOP) in order to extend Python's built-in functionality.

Chapter 5, Matplotlib Charts, explains how to create beautiful charts that visually represent data. Depending on the format of the data source, we can plot one or several columns of data within the same chart.

Chapter 6, Threads and Networking, explains how to extend the functionality of our Python GUI using threads, queues, and network connections. This will show us that our GUI is not limited at all to the local scope of our PC.

Chapter 7, Storing Data in Our MySQL Database via Our GUI, shows us how to connect to a MySQL database server. The first recipe in this chapter will show you how to install the free MySQL Server Community Edition, while, in the following recipes, we will create databases and tables, and then load data into those tables and modify it. We will also read the data back out from the MySQL server into our GUI.

Chapter 8, Internationalization and Testing, explains how to internationalize our GUI by displaying text on labels, buttons, tabs, and other widgets in different languages. We will start with a simple example and then explore how we can prepare our GUI for internationalization at the design level. We will also explore several ways to automatically test our GUI using Python's built-in unit testing framework.

Chapter 9, Extending Our GUI with the wxPython Library, introduces another Python GUI toolkit that currently does not ship with Python. It is called wxPython, and we will be using the Phoenix version of wxPython, which was designed to work well with Python 3.

Chapter 10, Building GUIs with PyQt5, shows you how to use the wonderful PyQt5 GUI programming framework. Tesla Motors uses this to build their GUI software, and, in this chapter, we will explore the beautiful world of drag and drop IDE GUI development using Python binding with Qt5, which, underneath the hood, is built upon C++. If you wish to get serious about Python GUI development, you need to study this chapter in addition to tkinter.

Chapter 11, Best Practices, explores different best practices that can help us to build our GUI in an efficient way and keep it both maintainable and extendable. Best practices are applicable to any good code, and our GUI is no exception when it comes to designing and implementing good software practices.

To get the most out of this book

To make optimum use of the content in this book, please bear the following points in mind:

  • All the recipes in this book were developed using Python 3.7 on a Windows 10 64-bit OS. They have not been tested on any other configuration. As Python is a cross-platform language, the code from each recipe is expected to run everywhere.
  • If you are using a Mac, it does come with built-in Python, yet it might be missing some modules such as tkinter, which we will use throughout this book.
  • We are using Python 3.7, and the creator of Python intentionally chose not to make it backward-compatible with Python 2. If you are using a Mac or Python 2, you might have to install Python 3.7 from www.python.org in order to successfully run the recipes in this book.
  • If you really wish to run the code in this book on Python 2.7, you will have to make some adjustments. For example, tkinter in Python 2.x has an uppercase T. The Python 2.7 print statement is a function in Python 3.7 and requires parentheses.
  • While the End of Life (EOL) for the Python 2.x branch has been extended to the year 2020, I would strongly recommend that you start using Python 3.7 and later.
  • Why hold on to the past, unless you really have to? Here is a link to the Python Enhancement Proposal (PEP) 373 that refers to the EOL of Python 2: https://www.python.org/dev/peps/pep-0373/.

Download the example code files

You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packt.com.
  2. Select the Support tab.
  3. Click on Code Downloads.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Python-GUI-Programming-Cookbook-Third-Edition. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Here is an overview of the Python modules (ending in a .py extension) for this chapter".

A block of code is set as follows:

action = ttk.Button(win, text="Click Me!", command=click_me)
action.grid(column=2, row=1)

Any command-line input or output is written as follows:

pip install pyqt5

Bold: Indicates a new term, an important word, or words that you see on screen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Click on the File menu and then click on New."

Warnings or important notes appear like this.
Tips and tricks appear like this.

Sections

In this book, you will find several headings that appear frequently (Getting ready, How to do it..., How it works..., There's more..., and See also).

To give clear instructions on how to complete a recipe, use these sections as follows:

Getting ready

This section tells you what to expect in the recipe and describes how to set up any software or any preliminary settings required for the recipe.

How to do it...

This section contains the steps required to follow the recipe.

How it works...

This section usually consists of a detailed explanation of what happened in the previous section.

There's more...

This section consists of additional information about the recipe in order to make you more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.

Get in touch

Feedback from our readers is always welcome.

General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at customercare@packtpub.com.

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/support/errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packt.com with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packt.com.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Python GUI Programming Cookbook. - Third Edition
Published in: Oct 2019Publisher: ISBN-13: 9781838827540
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Burkhard Meier

Burkhard Meier is a professional software test automation designer, developer, and analyst. He has more than 17 years' professional experience working for several software companies in California, USA. He is the author of Python GUI Programming Cookbook, First and Second Edition. This book is also available as a Packt video course. He is also the author of the Python Projects Packt video course. In his professional career, he developed advanced in-house testing frameworks written in Python 3. He also developed advanced test automation GUIs in Python, which highly increased the productivity of the software development testing team. When not dreaming in Python code, he reads programming books about design, likes to go for long walks, and reads classical poetry.
Read more about Burkhard Meier